Question

Is there any way to arrange RectangleShape controls to send them to the back and to the front in VB.NET. I need to change their Z-axis order so I can change which rectangle is in front. Here's an image of my form:

enter image description here

I would like to send the small rectangular shape behind the blue rectangle. I'd like to be able to do it from the code at run-time.

Was it helpful?

Solution

The shape controls from the Visual Basic Power Packs Controls library are not added directly to the form's Controls collection. Instead, they are added to a special ShapeContainer control. Normally the container is called ShapeContainer1, but if not, you can find it's name by looking at your form's .Designer.vb file.

All of the shapes are stored in the ShapeContainer1.Shapes collection. In order to change the Z-order of the shapes, you need to change the order that they are referenced by that collection. In other words, you need to change their index. The one at index 0 is considered to be the one in front. Therefore, to send a shape to the front, you need to change its index to 0, like this:

ShapeContainer1.Shapes.SetChildIndex(RectangleShape1, 0)
ShapeContainer1.Refresh()    

You need to call Refresh after you change the index to force the shapes to be immediately redrawn.

If you do want to change them at design time, since there is no way to change the order via the designer, you have to manually change the .Designer.vb file for your form. For instance, you could change this line in your InitializeComponents method from this:

Me.ShapeContainer1.Shapes.AddRange(New Microsoft.VisualBasic.PowerPacks.Shape() {Me.RectangleShape2, Me.RectangleShape1})

To this:

Me.ShapeContainer1.Shapes.AddRange(New Microsoft.VisualBasic.PowerPacks.Shape() {Me.RectangleShape1, Me.RectangleShape2})
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top