![](/img/trans.png)
[英]C# WinForms, Adding a usercontrol Dynamically during runtime and controls are invisible?
[英]How to rearrange a WinForms Controls during runtime?
在我的項目中,我想在運行時對控件進行排序,例如在DataGridView
,我們將如何使用display-index對網格中的字段進行排序。
在設計級別,我彼此相鄰添加了3個TextBox
和1個ComboBox
並且在運行時我想對其進行排序,例如,應該先顯示2個TextBox
,然后顯示ComboBox
,再顯示另一個TextBox
。
是否可以在運行時重新排列控件?
Windows窗體中的每個Control
都有一個Location
屬性。 您可以通過更改以下屬性來輕松更改控件的位置:
textBox1.Location = new Point(10, 50); // Puts the TextBox at coordinates (10,50)
坐標相對於控件容器的左上角(例如,表單本身)。
就您而言,您可以輕松地安排如下控件:
Control[] controls = new Control[] { textBox1, textBox2, comboBox3, textBox3 }; // These are your controls
int left = 20, top = 50; // or any other value
foreach (c in controls)
{
c.Location = new Point(left, top);
left += c.Width + 10; // space 10 pixels between controls
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.