views:

211

answers:

1

In CSS, we have a Property called z-index, what is the same in Winfrom set for a Panel control to the "Z-Index?

+4  A: 

WinForms has a z-order, but you can't access it as a number. Instead, every control has a BringToFront method and a SendToBack method, which move the control to the top of the z-order or to the bottom, respectively.

Not sure exactly why it was exposed this way, although you rarely encounter situations where either BringToFront or SendToBack don't provide what you need.

Update: I'm wrong, you can access the z-order directly via a method on the control's container's Controls collection. Here's a simple method that wraps it:

public void SetControlZOrder(Control ctrl, int z)
{
    ctrl.Parent.Controls.SetChildIndex(ctrl, z);
}

I'm guessing they encapsulated this in BringToFront and SendToBack just to keep everything simple and easy to use. I applaud.

Update 2: I interpreted your comments to a different answer here to mean that you want to be able to take a control that is inside a panel and larger than the panel (so that part of it is hidden) and make it so that the control is in front of the panel and larger than it (so that you see the whole control).

You can do this by removing the control from the panel, shifting its position by the original panel's position, and adding it to the form's controls:

panel1.Controls.Remove(button1);
button1.Left += panel1.Left;
button1.Top += panel1.Top; 
this.Controls.Add(button1);

The Left and Top shifts are necessary because the button's position was originally relative to the panel, and will now be relative to the form. The shifts keep it in the original virtual position, so it appears to come out of the panel.

You would then have to deal with putting it back in the panel, which is just a reverse of the above code.

MusiGenesis
@sv88erik: I deleted my answer because this one is better, but here is the comment you wrote on it: `"the problem is that what then is that the controls inside the panel does not show more than the size of the panel. I make it so that when you click on the controls inside the panel will be larger, and then it goes without for the size of the panel .... "`. I am sure what you want do to is possible, but I'm not sure of the right way to do it. One option might be to make ALL controls be children of the Form (theControl.Parent = theForm). Then they will just overlap each other.
Charles