开发者

"Z-Index" in WinForms

In CSS, we have a Property called z-index, what is the same in WinForms set for a Panel control to the "Z-In开发者_如何学Pythondex?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜