Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
i want to select a specified textbox like :
 
int k=15;
select textBoxk //  trying to select the textBox15
 
how can i select textBox15 with sending the number 15 from outside?
Posted 5-Jan-13 7:48am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Hi,
 
Try this:
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:
int k = 15;
textBoxDictionary["textBox" + k.ToString()].Text = "some text";
Hope this helps.
  Permalink  
v2
Comments
ahmetkocadogan at 5-Jan-13 13:58pm
   
thanks for the help
ProgramFOX at 6-Jan-13 9:39am
   
You're welcome!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

i think it must be :
Dictionary<string,textbox> textBoxDictionary = new Dictionary<string,textbox>();
it says 'Type Expected' when i dont write TextBox.
am i right?
  Permalink  
Comments
ProgramFOX at 5-Jan-13 13:07pm
   
Yes, you're right. I updated my answer, and I added TextBox.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

Hi
 
You can use from bellow code:
 
-------------------------------------------------------------
TextBox[] textBox = new TextBox[NumberOfTextBoxYouNeed];
textBox[YourDestinationNumber].Text = "Your Text";
 

I hope it's helpful
  Permalink  
v2
Comments
ahmetkocadogan at 5-Jan-13 14:59pm
   
thanks, this is helpful and very useful
Reza Alipour Fard at 5-Jan-13 15:07pm
   
You're welcome. It is my pleasure. Best Wish for you.
ridoy at 5-Jan-13 15:43pm
   
+5
Reza Alipour Fard at 5-Jan-13 15:53pm
   
Thank you for attention
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

To set the value of the variable to the textbox, set textBoxk.Text = 15;
To set focus to a textbox, call textbox.Focus().
  Permalink  
Comments
ProgramFOX at 5-Jan-13 12:56pm
   
The OP means that he want to get textBox15 if k is 15, and the OP want to get textBox14 is k is 14, ...

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

  Print Answers RSS
0 OriginalGriff 7,215
1 DamithSL 5,114
2 Maciej Los 4,866
3 Sergey Alexandrovich Kryukov 4,747
4 Kornfeld Eliyahu Peter 4,514


Advertise | Privacy | Mobile
Web04 | 2.8.141223.1 | Last Updated 5 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100