我如何迭代控件Windows窗体应用程序? [英] How do I Iterate controls in a windows form app?
问题描述
所以这是一个愚蠢的问题。我在我写一个应用程序添加了一堆文本框的一个选项对话框。它们被命名为TextBox1中 - textbox12。有没有以编程方式访问的名称的方法吗?我只是想遍历他们在一个for循环。现在我单独访问每一个(战栗!)。我知道这是错误的方式。什么是正确的方法是什么?什么是最简单的方法是什么?
SO this is a dumb question. I have added a bunch of textboxes to an options dialog in an app I've written. They are named textbox1 - textbox12. Is there a way to access the the names programmatically? I'd just like to iterate over them in a for loop. Right now I am accessing each one individually (shudders!). I know this is the wrong way. What is the right way? what is the easy way?
感谢
推荐答案
我个人更喜欢以编程方式创建控件,然后把它们放在一个收藏。 。即
Personally I prefer to create the controls programatically and then put them in a collection. i.e.
IList<TextBox> textBoxes = new List<TextBox>();
…
for (int i = 0; i < 12; i += 1)
{
TextBox textBox = new TextBox();
textBox.Position = new Point(FormMargin, FormMargin + (i * (textBox.Height + TextBoxPadding)));
this.Controls.Add(textBox);
this.textBoxes.Add(textBox);
}
您可以那么就itterate在文本框以编程方式添加。我觉得这是更好地伸缩,当你有文本框的几个不同的群体,你需要做到这一点。
You can then just itterate over textBoxes to programatically add them. I find this scales better when you have several different groups of textboxes you need to do this with.
这篇关于我如何迭代控件Windows窗体应用程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!