• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1287
  • Last Modified:

Accessing TextBox controls on a Windows Form dynamically using name with variable

In ASP.NET, in the code-behind in C#, I can access any one of a number of Textboxes that have IDs of TextBox1, TextBox2, TextBox3, ... TextBox9, etc. by using the following code:

TextBox txt = (TextBox)Form1.FindControl("TextBox" + i);

I am developing a Windows Form application. What code do I use to reference a TextBox on a Windows Form in the code-behind?

Although I am using C#, a VB answer would suffice too.
0
dwoolley3
Asked:
dwoolley3
  • 4
  • 2
3 Solutions
 
CodeCruiserCommented:
You can directly access controls in code in winforms unless these controls are inside some repeater type controls like grid, list etc.
0
 
dwoolley3Author Commented:
Here is my code that is used in the code-behind of Form1.cs:

  private void AssignCell_to_TextBox(int BoxNum, char Value)
        {
            //TextBox txt = (TextBox)Form1.FindControl("TextBox" + BoxNum);
            switch (BoxNum)
            {
                case 1:
                    TextBox1.Text = Convert.ToString(Value);
                    break;
                case 2:
                    TextBox2.Text = Convert.ToString(Value);
                    break;
                case 3:
                    TextBox3.Text = Convert.ToString(Value);
                    break;

Open in new window


Since I have 81 of these textboxes, I'd like to simply access one of them based on BoxNum and assign the value of "Value" to it. What one line of code could I use?
0
 
CodeCruiserCommented:
You can use

TextBox txt = (TextBox)this.Controls.Find("TextBox" + BoxNum, true);
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
dwoolley3Author Commented:
Close, but it has an errror in VS saying: Cannot convert type 'System.Windows.Forms.Control[]' to 'System.Windows.Forms.TextBox'
0
 
käµfm³d 👽Commented:
Find returns an array. Just take the first item in that array!
0
 
dwoolley3Author Commented:
With some guidance and experimentation, I was able to produce the desired code:
            Control[] cntrl = this.Controls.Find("TextBox" + BoxNum, false);
            TextBox txt = (TextBox)cntrl[0];
            txt.Text = Value.ToString();

Open in new window

0
 
dwoolley3Author Commented:
While one post provided some code, it was not fully functional. The suggestion provided in another array helped me to tweak the code a bit. The code I provided is the exact code I needed.
0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now