c#子窗体的上移、下移、置顶和置底的方法

c#子窗体的上移、下移、置顶和置底的方法,涉及到控件的Z轴次序属性。例如,动态生成的子窗体,加入到panel控件中,当第一个加入panel控制集合的index索引值为0。新增加一个窗口,会出现在最上面,index索引值为0,在后面的窗体index索引值为index + 1。

使用窗体的SendToBack()、BringToFront()和SetChildIndex()来改变窗口在Z轴上的次序。

置顶使用BringToFront()方法。将控件带到Z轴顺序的前面。

置底使用SendToBack()方法。将控件带到Z轴顺序的后面。

上移、下移使用SetChildIndex()方法。先通过index = GetChildIndex()方法获取选中窗体所在Z轴的位置,通过index 加1下移 SetChildIndex(选中的窗体,index + 1);通过index减1上移,SetChildIndex(选中的窗体,index - 1)。设置完成后,系统会自动改变其它窗体在Z轴上的索引值。

Control.ControlCollection.SetChildIndex 方法

将集合中的指定子控件的索引设置为指定的索引值。

public virtual void SetChildIndex(
	Control child,
	int newIndex
)

说明:

调用 SetChildIndex 时,将把 child 参数所引用的 Control 移动到 newIndex 指定的位置,并将重新调整 Control.ControlCollection 中的其他 Control 引用的顺序以适应此次移动。 索引值为零的控件位于 Z-顺序的顶部,数字越大距底部越近。



猜你喜欢

转载自blog.csdn.net/lbc2100/article/details/80729079