[英]How to set Z-order of a Control using WinForms
我正在編寫一個自定義TextBox
,它在獲得焦點后會更改其邊框樣式。
由於添加邊框會導致控件與相鄰控件重疊,因此我暫時將文本框置於對話框的前面(使用textBox.BringToFront()
)。
但是,一旦編輯完成並且焦點丟失,我想將控件發送回 Z 順序中的原始 position。
這可能嗎(最好以簡單的方式!)
調用父Controls
集合的GetChildIndex
和SetChildIndex
方法。
沒有 VB 中的 Z 順序,但您可以使用GetChildIndex
和SetChildIndex
方法手動獲取和設置它們的索引。
這里有一個如何使用它的例子。 您可能需要保留每個控件索引的記錄,以便在完成后將其設置回它。
像這樣的事情可能是你所追求的:
// 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);
}
}
C 夏普
Control.SetValue(Panel.ZIndexProperty,0);
控制就是你的控制。 0 是 ZIndex 的索引。 0 是默認值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.