How to set Z-order of a Control using WinForms
Asked Answered
F

4

42

I'm writing a custom TextBox that upon gaining focus changes its border style.

As adding a border causes the control to overlap with those neighbouring it, I temporarily bring the text box to the front of the dialog (using textBox.BringToFront()).

However, once editing is complete and focus is lost, I would like to send the control back to its original position in the Z-order.

Is this possible (preferably in a simple way!)

Figurant answered 9/7, 2010 at 13:55 Comment(0)
E
50

Call the GetChildIndex and SetChildIndex methods of the parent's Controls collection.

Earthwork answered 9/7, 2010 at 14:3 Comment(4)
Great - Led astray by BringToFront() I looked in the wrong place for these methods: at the control's methods, not the parent's controls' methods. Thanks.Figurant
No need example, the answer it self it's enough!Corpuz
From reference: "The control with an index value of zero is at the top of the z-order, and higher numbers are closer to the bottom."Dinsdale
Interestingly enough, BringToFront() relies on SetChildIndex() if the control's parent is not nullTrampoline
R
31

There is no Z-order as there was in VB, but you can use the GetChildIndex and SetChildIndex methods to get and set their indexes manually.

Here there's an example of how to use it. You will probably need to keep a record of each controls index though so you can set it back to it when it's finished with.

Something like this is probably what you're after:

// Get the controls index
int zIndex = parentControl.Controls.GetChildIndex(textBox);
// Bring it to the front
textBox.BringToFront();
// Do something...
// Then send it back again
parentControl.Controls.SetChildIndex(textBox, zIndex);
Reta answered 9/7, 2010 at 14:3 Comment(2)
Everything changes when you dynamically make controls (in)visible. Set 'textbox.Visible = true' and it will have index 0. It complete ignores the order from the document outline. This took me a while to figure :)Organogenesis
Was trying to figure this out too. This comment helped me out. ThxSenhor
F
1

When used with the FlowLayoutPanel this will move a control up or down

    /// <summary>
    /// When used with the FlowLayoutPanel this will move a control up or down
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="UpDown"></param>
    private void C_On_Move(object sender, int UpDown)
    {
        //If UpDown = 1 Move UP, If UpDown = 0 Move DOWN
        Control c = (Control)sender;
        // Get the controls index
        int zIndex = _flowLayoutPanel1.Controls.GetChildIndex(c);
        if (UpDown==1 && zIndex > 0)
        {
            // Move up one
            _flowLayoutPanel1.Controls.SetChildIndex(c, zIndex - 1);
        }
        if (UpDown == 0 && zIndex < _flowLayoutPanel1.Controls.Count-1)
        {
            // Move down one
            _flowLayoutPanel1.Controls.SetChildIndex(c, zIndex + 1);
        }
    }
Firenze answered 4/4, 2018 at 17:1 Comment(0)
H
0

In C Sharp

Control.SetValue(Panel.ZIndexProperty,0);

Control is your control. 0 is index of ZIndex. 0 is default value.

Hosey answered 29/9, 2022 at 15:54 Comment(2)
Looks like WPF, not WinFormsFigurant
Make sure to use code formatting!Cancroid

© 2022 - 2024 — McMap. All rights reserved.