我怎样才能通过他们的号码解决Windows窗体中的文本框? [英] How can I address textboxes in windows forms by their number?

查看:116
本文介绍了我怎样才能通过他们的号码解决Windows窗体中的文本框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我想要有大约12个文本框(稍后我还将添加12个图片框),这些框中将包含产品的名称。然而,尽管目录中没有超过12种产品的选择,但可能会少一些。



为了节省空间,我认为我会创建一个循环,检查每一个循环是否有产品留在目录中,如果有的话,它将产品分配给一个文本框[i]。

所以基本上,我会就像能够将textBox1作为textBox [1],textBox2作为textBox [2]等等一样。



我试过这样做,就像

  System.Windows.Forms.TextBox [] array = {textBox1,textBox2,textBox3}; 

但是我似乎只能从初始化中创建这样一个对象,并且在创建时似乎在其他任何地方都无法访问,即使是相同的形式,所以我可以调用该函数在表单初始化时仅显示一次,并且每次有人购买任何东西时我都要调用display方法。

解决方案

如果您的文本框编号一致,您可以通过它们所在表单或容器的Controls属性来访问它们。您可以直接使用它们的名称:

  for(int i = 0; i <12; i ++)
{
this.Controls [textBox + i.ToString()]。Text =Something;
}


So I want to have around 12 text boxes (later on I will also add 12 picture boxes below) that will have the name of the product in them. However, while there is no option for there to be more than 12 products in the catalog, there can be less.

To save space, I thought I will create a cycle, that checks every loop if there are products left in the catalog, and if there is, it assigns the product to a textbox[i].

So basically, I would like to be able to address textBox1 as textBox[1], textBox2 as textBox[2] and so on.

I tried doing it like

System.Windows.Forms.TextBox[] array = { textBox1, textBox2, textBox3 };

But it seems like I can only create such an object in from initialization, and when created there it seems to be unacessible anywhere else, even in the same form, so I can call the function to display only once when the form is initialized, and I would like to call the display method every time someone buys anything

解决方案

If your textboxes are numbered consistently you access them through the Controls property of the form or container they're in. You can use their name directly:

for(int i = 0; i<12;i++)
{
     this.Controls["textBox"+i.ToString()].Text = "Something";
}

这篇关于我怎样才能通过他们的号码解决Windows窗体中的文本框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆