C# .NET - create dynamic textbox

Asked By raj kumar on 23-Dec-10 08:12 AM
               TextBox tb2 = new TextBox();
         tb2.Size = new Size(150, 150);
         tb2.Location = new Point(147, 115);
         this.Controls.Add(tb2);

         TextBox tb3 = new TextBox();
         tb3.Size = new Size(150, 150);
         tb3.Location = new Point(147, 140);
         this.Controls.Add(tb3);

         tb3.Focus();


run time cursor starting point in textbox1(tb2).
but I need run time cursor point in textbox2(tb3)....................
Reena Jain replied to raj kumar on 23-Dec-10 08:20 AM
hi,

use this line to set the focus on second textbox

this.Controls[tb3 ].Focus();

here is the updated code for you
TextBox tb2 = new TextBox();
  tb2.Size = new Size(150, 150);
  tb2.Location = new Point(147, 115);
  this.Controls.Add(tb2);
 
  TextBox tb3 = new TextBox();
  tb3.Size = new Size(150, 150);
  tb3.Location = new Point(147, 140);
  this.Controls.Add(tb3);
 this.Controls[tb3].Focus();

hope this will help you
raj kumar replied to Reena Jain on 23-Dec-10 08:25 AM
executed 2 error:

Error   1   The best overloaded method match for 'System.Windows.Forms.Control.ControlCollection.this[int]' has some invalid arguments  

Error   2   Argument '1': cannot convert from 'System.Windows.Forms.TextBox' to 'int'

anil soni replied to raj kumar on 23-Dec-10 09:00 AM
Your code is perfect and it should work...even if it is not working then then tryy change the tab index of textboxes and should work.
Reena Jain replied to raj kumar on 23-Dec-10 09:33 AM
hi,

try to use this

here is the updated code for you
TextBox tb2 = new TextBox();
  tb2.Size = new Size(150, 150);
  tb2.Location = new Point(147, 115);
  this.Controls.Add(tb2);
 
  TextBox tb3 = new TextBox();
  tb3.Size = new Size(150, 150);
  tb3.Location = new Point(147, 140);
  this.Controls.Add(tb3);
 this.Controls[1].Focus();

 //this.Controls["tb3"].Focus(); this also should work
Hope this will help you now
Gorav Handa replied to raj kumar on 24-Dec-10 12:27 AM
Do this way:

public partial class Form1 : Form

    {

      TextBox tb2;

      TextBox tb3;

      public Form1()

      {

        InitializeComponent();

      }

      private void Form1_Load(object sender, EventArgs e)

      {

        tb2 = new TextBox();

        tb2.Size = new Size(150, 150);

        tb2.Location = new Point(147, 115);

        tb2.TabIndex = 0;

        this.Controls.Add(tb2);

        tb3 = new TextBox();

        tb3.Size = new Size(150, 150);

        tb3.Location = new Point(147, 140);

        tb2.TabIndex = 1;

        this.Controls.Add(tb3);

      }

      protected override void OnShown(EventArgs e)

      {

        this.tb3.Focus();

        base.OnShown(e);

      }

    }

rohan patel replied to raj kumar on 24-Dec-10 05:11 AM
 for (int i = 0; i < count; i++)
                {
                    TextBox tb = new TextBox();
                    tb.ID = "tb" + i;
                    Panel1.Controls.Add(tb);
                    textboxes.Add(tb);
                    tb.Text = Request.Form[tb.ClientID];
                }

http://patelrohan.blogspot.com/p/dotnet_21.html