Click here to Skip to main content
13,634,702 members
Rate this:
 
Please Sign up or sign in to vote.
See more:
I would like to create multiple textbox at runtime using a button click event. And how can i get the textboxes id i.e. how can i access it????
I have implemented to create a multiple textbox at runtime but i can't access it.
Here is the code to create multiple textbox at runtime...

namespace multiple_textbox
{
    public partial class Form1 : Form
    {

        int a = 0;

        public Form1()
        {
            InitializeComponent();
        }

        private void btntxtbx_Click(object sender, EventArgs e)
        {
            a++;

            int count = 0 + a ;
            TextBox[] t = new TextBox[count];
            int x = 70, y = 50;
            for (int i = 0; i < count; i++)
            {
                t[i] = new TextBox();
                if(this.Width!=1152)
                {
                t[i].Location = new System.Drawing.Point(y +(i*65), x);
                t[i].Size = new Size(100, 50);
                t[i].Name = "txtbx" + i.ToString();
                this.Controls.Add(t[i]);
                this.ResumeLayout(false);
                y += 40;
                this.Refresh();
                }
            }

        }

    }
}
Posted 5-Jul-12 1:50am
Updated 5-Jul-12 1:56am
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

set the
TextBox[] t

bigger scope.
Define it in class level.

int a = 0;
     TextBox[] t;

     public Form1()
     {
         InitializeComponent();
     }

     private void btntxtbx_Click(object sender, EventArgs e)
     {
         a++;

         int count = 0 + a;
         t = new TextBox[count];
         int x = 70, y = 50;
         for (int i = 0; i < count; i++)
         {
             t[i] = new TextBox();
             if (this.Width != 1152)
             {
                 t[i].Location = new System.Drawing.Point(y + (i * 65), x);
                 t[i].Size = new Size(100, 50);
                 t[i].Name = "txtbx" + i.ToString();
                 this.Controls.Add(t[i]);
                 this.ResumeLayout(false);
                 y += 40;
                 this.Refresh();
             }
         }

     }

     private void button2_Click(object sender, EventArgs e)
     {
         if (t == null)
             return;

         TextBox t2 = t.Where(o => o.Name == "txtbx0")
             .FirstOrDefault() as TextBox;

         Console.WriteLine("txtBox: "+t2.Name);

     }


Hope this will help you.
  Permalink  
v2
Comments
Shankar23G 5-Jul-12 9:17am
   
i have error "NullReferenceException" Object reference not set to an instance of an object. how to resolve it???
valza 5-Jul-12 9:39am
   
I updated my solution 54 min ago....
button2 event checks the first item in array "txtbx0"
you cannot get txtbx10 if on form you have added 3 txtBoxes.
Then you need to implement some checking or catch exception or rewrite application structure.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Set the Id of each texdtbox
Like
t[i].ID = "TestBox" + i.ToString();

declare that textbox object outside the button click event.
Means:Globaly
Now try to access the textbox using their ID.

One suggestion:
You have written that
int count = 0 + a;

I think no need to write 0 + a instant of that simply write
int count = a;
  Permalink  
v2
Comments
Shankar23G 5-Jul-12 8:18am
   
I would like to call the textbox object like "txtbx1","txtbx.2"and so on.. how can i access it????
bhagirathimfs 5-Jul-12 8:45am
   
access means??
I think if you want to add anything
than use like this txtbox1.Text = "Hello"
Shankar23G 5-Jul-12 8:58am
   
yes i would like to get the txtbox1.Text values???
bhagirathimfs 5-Jul-12 9:04am
   
than write txtbox1.Text = "hello";
And declare the textbox class
i.e.TextBox[] t = new TextBox[count];
as global so that u can access that textboxes over all the page.
In your case the scope of the textbox is limited to the button click.
bhagirathimfs 5-Jul-12 9:56am
   
Is this fulfill your requirement????
valza 5-Jul-12 9:07am
   
Check my solution.
There you can get all TextBox properties including "Text" (by changing t2.Name to t2.Text)
bhagirathimfs 5-Jul-12 9:15am
   
yes valza also write
Refer his solution also
I suggest to assign the ID of the textbox and access the textbox by ID is good practice.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

First of all, most likely you will need to access the text boxes later. How? Simply make the reference TextBox[] t accessible during the life time you need. Most usually, you would need to make this array reference a field or a property of your form class. (Only give it a good semantic name; using one-character identifiers is always bad.)

You code has a number of problems to fix. Bad things is all your immediate constants hard-coded in the code: 70, 50, 1142, 65, 100, 50, "txtbx"… Declare then all explicit constants or put in resources. Use a separate panel with docking/padding for a parent of these controls. One apparent problem is the size. You should never assign the height; it should remain default. One apparent problem that you arrange the controls by index without knowing its size. You should do a very different thing; better arrange them vertically and calculate the height to determine the vertical step:


int gap = 4;
for (int i = 0; i < count; i++) {
    t[i] = new TextBox();
    //if (this.Width != 1152) silly check, if will give you always true or always false
    {
       t[i].Location = new System.Drawing.Point(y * (t[i].Height + gap), x); //height it taken into account
       //...
       this.Controls.Add(t[i]);
    }
}


Finally, remove Refresh and ResumeLayout. Sometimes you need ResumeLayout, but certainly out of the loop.

—SA
  Permalink  
v2

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web04-2016 | 2.8.180712.1 | Last Updated 5 Jul 2012
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

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