我试图移动位于Form1中的所有标签。我可以移动特定的标签,但是如何循环和移动所有标签?感谢您的帮助和建议。
移动特定标签:
label1.Location = new Point(0, 0);
这将无法工作:
Form1 f1 = new Form1();
for (int i = 0; i < f1.Controls.Count; i++)
{
f1.Controls[i].Location = new Point(0, 0);
{
您可以遍历所有控件,但需要检查控件的类型以查看它是否实际上是标签。下面的代码应该可以正常工作,如果实际上不是标签,则as
关键字将为labelControl
nullctrl
//Form1 f1 = new Form1(); // Removed, using this means you're calling from within the control you want to change already.
foreach (var ctrl in this.Controls)
{
var labelControl = ctrl as Label;
if (labelControl == null)
{
continue;
}
labelControl.Location = new Point(0, 0);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句