![](/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.