Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
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:
C#
int alpha=65;
for(int i=0;i<26;i++)
{
    txtCipher(i+1).Text=((char)alpha).ToString();
    alpha++;
} 


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
Posted
Updated 10-Dec-10 1:01am
v3
Comments
JF2015 10-Dec-10 7:01am    
Edited to improve spelling.

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.
 
Share this answer
 
It is possible that for you need to add 26 text boxes into a List like bellow code.

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


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





Thanks
 
Share this answer
 
v2
name your textboxes as txtCipher_1, txtCipher_2,...etc.
C#
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(); ;
                }
            }


I tested and it works in .NET 3.5
 
Share this answer
 
v3
Comments
haseebsvirgo 10-Dec-10 10:57am    
actually i want to add
'A' in txtCiphered1
'B' in txtCiphered2
'C' in txtCiphered3.......'Z'intxtCiphered26 on form load so thats why i need a simple loop for that so that in the first iteration txtCiphered1 is selected and filled on 2nd iteration txtCiphered2 is selected and 'B' will be inserted like so on....
Saksida Bojan 10-Dec-10 11:24am    
It isn't that simple. txtCiphered1, txtCiphered2 are two difrent object with assigned variable witch you call it directly. But if you use Controls of a parent container (That can be tabControl, form or others) and cycle through controls. the abowe answer is correct. You used txtCipher(i+1) witch is invalid syntax. It is used as a calling to method/function. If you have added text box via designer, they automatcly populate Name proprties for reference. you can check name of textbox to see if you have correct textbox, but programaticly Name propertiy isn't used, but designer convinently populates it. ex: if (item as TextBox).Name == "txtCiphered1")
MCY 11-Dec-10 9:09am    
exactly. txtCipher(i+1) is a call for method txtCipher with input parameter i+1.
if you cannot change textbox names, use substring to get the number instead of split.
I do not mind having low votes, everyone is entitled to their opinion. But it would be nice to have some feedback, you know, what is the matter with the current code.
plz any one more ellaborate this plz
 
Share this answer
 
Comments
MCY 11-Dec-10 9:09am    
why not use comment instead of posting request in to form of an answer?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900