Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

z-order C#

Status
Not open for further replies.

rdeigsler

Programmer
Feb 13, 2003
4
US
If I have a number of overlapping controls on a Windows form, how can I programmatically change the z-order? I need to change the order in which they are layered. Not just BringToFront or SendToBack but set them in a specific order.

Thanks
 
Did you look at the SetChildIndex method on the Controls collection off the Form object?

ms-help://MS.VSCC/MS.MSDNVS/cpref/html/frlrfsystemwindowsformscontrolcontrolcollectionclasssetchildindextopic.htm

Chip H.
 
Yes, but to no avail. When I see what the GetChildIndex is of the controls, they are all 0. SetChildIndex doesn't change the z-order.
 
I played around a little with this.

In the call:
this.Controls.AddRange(new System.Windows.Forms.Control[]
{
this.button1,
this.dataGrid1,
this.label1,
this.label4,
this.trackBar1});

the components are added to the frame in the order listed from front to back. That is - button1 is in front, trackBar1 is in the back.

This may not be very elegant but you could try using the CopyTo method to copy the existing Controls array, rearrange the elements externally, Controls.Clear and then add back the controls with Controls.AddRange.
---
dino
 
Let me know how it works. Depending on how complicated the form is, you may want to bracket the reformating calls with this.SuspendLayout() and this.ResumeLayout() so that it doesn't look too ugly to the user.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top