Click here to Skip to main content
15,891,473 members
Please Sign up or sign in to vote.
3.67/5 (3 votes)
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...

C#
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
Updated 5-Jul-12 1:56am
v2

set the
C#
TextBox[] t

bigger scope.
Define it in class level.

C#
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.
 
Share this answer
 
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.
Set the Id of each texdtbox
Like
C#
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;
 
Share this answer
 
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????
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:


C#
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
 
Share this answer
 
v2

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