【发布时间】:2011-03-13 21:34:45
【问题描述】:
我正在编写一个自定义TextBox,它在获得焦点后会改变其边框样式。
由于添加边框会导致控件与相邻的控件重叠,因此我暂时将文本框置于对话框的前面(使用textBox.BringToFront())。
但是,一旦编辑完成并且失去焦点,我想将控件发送回其在 Z 顺序中的原始位置。
这可能吗(最好以简单的方式!)
【问题讨论】:
标签: c# winforms textbox focus z-order
我正在编写一个自定义TextBox,它在获得焦点后会改变其边框样式。
由于添加边框会导致控件与相邻的控件重叠,因此我暂时将文本框置于对话框的前面(使用textBox.BringToFront())。
但是,一旦编辑完成并且失去焦点,我想将控件发送回其在 Z 顺序中的原始位置。
这可能吗(最好以简单的方式!)
【问题讨论】:
标签: c# winforms textbox focus z-order
调用父级的Controls 集合的GetChildIndex 和SetChildIndex 方法。
【讨论】:
没有像 VB 中那样的 Z 顺序,但您可以使用 GetChildIndex 和 SetChildIndex 方法手动获取和设置它们的索引。
Here 有一个如何使用它的例子。不过,您可能需要记录每个控件索引,以便在完成后将其设置回它。
这样的东西可能就是你所追求的:
// 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);
【讨论】:
当与 FlowLayoutPanel 一起使用时,这将向上或向下移动控件
/// <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);
}
}
【讨论】: