Reorder Z when creating visible controls

I am using C # Winforms with .NET Framework version 4.0

I have a panel that contains four labels attached to the top of the panel. The middle two marks are hidden.

--- Top ---
Label first (visible)
Label Two - (NOT visible)
Label three (NOT visible)
Label fourth - (visible)
--- Bottom ---

When the button below is pressed

private void btnShowLabels_Click(object sender, EventArgs e)
{
    this.lblTwo.Visible = true;
    this.lblThree.Visible = true;
}

labels two and three appear, but their z-order is confused, as shown below.

--- Top ---
Label first (visible)
Label three (visible)
Label second - (visible)
Label fourth - (visible)
--- Bottom ---

z- .

, BringToFront() , BringToFront() click, , .

+5
4

z, SetChildIndex .Visible . , lbl1 lbl4, , . , - "" "" , .

EDIT: -, , . , VisibleChanged:

void GenericDockedLabel_VisibleChanged(object sender, EventArgs e)
{
    this.Controls.SetChildIndex(lbl1, 3);
    this.Controls.SetChildIndex(lbl2, 2);
    this.Controls.SetChildIndex(lbl3, 1);
    this.Controls.SetChildIndex(lbl4, 0);
}

@Mark SetChildIndex

+4

Control.Visible = True Z-. , .

( ) :

foreach (Control ctrl in FlpDetails.Controls)
{
    IntPtr DummyHandle = ctrl.Handle;
}

. control.visible = true Z-.

-, -.

+6

( 5) , , (, , ) . , . . , , .

private void cboNumberOfPanels_SelectedIndexChanged(object sender, EventArgs e)
{
    int numberOfPanels;
    int.TryParse(cboNumberOfPanels.SelectedItem.ToString(), out numberOfPanels);

    pnlPanelDimensions1.Visible = numberOfPanels >= 1;
    pnlPanelDimensions2.Visible = numberOfPanels >= 2;
    pnlPanelDimensions3.Visible = numberOfPanels >= 3;
    pnlPanelDimensions4.Visible = numberOfPanels >= 4;
    pnlPanelDimensions5.Visible = numberOfPanels >= 5;
}

.SetChildIndex() , . , 1, pnlPanelDimensions1 . 3, pnlPanelDimensions2, pnlPanelDimensions3 , , pnlPanelDimensions1. .

- , :

    int numberOfPanels;
    int.TryParse(cboNumberOfPanels.SelectedItem.ToString(), out numberOfPanels);

    pnlPanelDimensions1.Visible = numberOfPanels >= 1;
    pnlPanelDimensions1.BringToFront();
    pnlPanelDimensions2.Visible = numberOfPanels >= 2;
    pnlPanelDimensions2.BringToFront();
    pnlPanelDimensions3.Visible = numberOfPanels >= 3;
    pnlPanelDimensions3.BringToFront();
    pnlPanelDimensions4.Visible = numberOfPanels >= 4;
    pnlPanelDimensions4.BringToFront();
    pnlPanelDimensions5.Visible = numberOfPanels >= 5;
    pnlPanelDimensions5.BringToFront();
0

:

BringToFront

VS: → →

-1

Source: https://habr.com/ru/post/1529048/


All Articles