如何通过编码更改文本框编号 [英] how to change the text box number through coding

查看:77
本文介绍了如何通过编码更改文本框编号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我的C#窗口窗体上有大约25个只读文本框.
我想在运行时通过一个for循环中的单个语句访问每个文本框,以便在运行时用字母(A-Z)填充这些文本框:

Hello everybody,

I have round about 25 text boxes on my c# window form that are read only.
I want to fill these text boxes with alphabets (A-Z) at run time by accessing each text box through a single statement in a for loop like:

int alpha=65;
for(int i=0;i<26;i++)
{
    txtCipher(i+1).Text=((char)alpha).ToString();
    alpha++;
} 



txtCipher是我每个文本框的名称,例如:
txtCipher1
txtCipher2
.
.
.
txtCipher26

谁能帮帮我吗?我会非常感谢您.

问候,
Haseeb



Here txtCipher is the name of my each textbox like:
txtCipher1
txtCipher2
.
.
.
txtCipher26

Can anyone please help me? I''ll be highly thankful to you.

Regards,
Haseeb

推荐答案

您可能可以这样做,但是它会很慢,因为您需要使用反射.如果是我,我将得到父容器,在所有子容器中循环,如果子容器是TextBox,则将值设置为我想要的值.
You can probably do that, but it''s going to be slow because you need to use reflection. If it were me, I''d just get the parent container, cycle through all of it''s children, and if the child is a TextBox, set the value to whatever I want.


可能是因为您需要将26个文本框添加到列表中,例如波纹管代码.

It is possible that for you need to add 26 text boxes into a List like bellow code.

 List<TextBox> textBoxList = new List<TextBox>();
TextBox txtChiper1=new TextBox();
TextBox txtChiper2=new TextBox();
textBoxList.Add(txtChiper1);
textBoxList.Add(txtChiper2);





int alpha=65;
for(int i=0;i<26;i++)
{
    textBoxList[i].Text=((char)alpha).ToString();
    alpha++;
}






谢谢






Thanks


将您的文本框命名为txtCipher_1,txtCipher_2等.
name your textboxes as txtCipher_1, txtCipher_2,...etc.
int cIndex;
            foreach (Control item in this.Controls)
            {
                if (item is TextBox)
                {
                    cIndex = Convert.ToInt16((item as TextBox).Name.Split('_')[1]);
                    (item as TextBox).Text = ((char)(64 + cIndex)).ToString(); ;
                }
            }



经过测试,它可以在.NET 3.5中运行



I tested and it works in .NET 3.5


这篇关于如何通过编码更改文本框编号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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