Click here to Skip to main content
15,867,453 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
i want to select a specified textbox like :

C#
int k=15;
select textBoxk //  trying to select the textBox15


how can i select textBox15 with sending the number 15 from outside?
Posted

Hi,

Try this:
C#
Dictionary<string, TextBox> textBoxDictionary = new Dictionary<string, TextBox>();
public Form1() // constructor
{
      InitializeComponent();
      textBoxDictionary.Add("textBox1", textBox1);
      textBoxDictionary.Add("textBox2", textBox2);
      textBoxDictionary.Add("textBox3", textBox3);
      ...
}

Now, to set the text of a specific text box, try this:
C#
int k = 15;
textBoxDictionary["textBox" + k.ToString()].Text = "some text";

Hope this helps.
 
Share this answer
 
v2
Comments
ahmetkocadogan 5-Jan-13 13:58pm    
thanks for the help
Thomas Daniels 6-Jan-13 9:39am    
You're welcome!
i think it must be :
C#
Dictionary<string,textbox> textBoxDictionary = new Dictionary<string,textbox>();

it says 'Type Expected' when i dont write TextBox.
am i right?
 
Share this answer
 
Comments
Thomas Daniels 5-Jan-13 13:07pm    
Yes, you're right. I updated my answer, and I added <small>TextBox</small>.
Hi
You can use from bellow code:

-------------------------------------------------------------
TextBox[] textBox = new TextBox[NumberOfTextBoxYouNeed];
textBox[YourDestinationNumber].Text = "Your Text";



I hope it's helpful
 
Share this answer
 
v2
Comments
ahmetkocadogan 5-Jan-13 14:59pm    
thanks, this is helpful and very useful
Reza Alipour Fard 5-Jan-13 15:07pm    
You're welcome. It is my pleasure. Best Wish for you.
ridoy 5-Jan-13 15:43pm    
+5
Reza Alipour Fard 5-Jan-13 15:53pm    
Thank you for attention
To set the value of the variable to the textbox, set textBoxk.Text = 15;
To set focus to a textbox, call textbox.Focus().
 
Share this answer
 
Comments
Thomas Daniels 5-Jan-13 12:56pm    
The OP means that he want to get textBox15 if <small>k</small> is 15, and the OP want to get textBox14 is <small>k</small> is 14, ...

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