How to apply a Focus() function on Dynamically Created TextBox in windows forms?

Viewed 62

When dynamically creating textBoxes how can we make one of the textBoxes have the Focus() function on it?

namespace Dinamik_Arac
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            for (int i = 1; i <= 5; i++)
            {
                TextBox txt = new TextBox();
                Point txtKonum = new Point(300, i * 30);
                txt.Location = txtKonum;
                txt.Name = "TextBox" + i;
                txt.Text = i.ToString();
                this.Controls.Add(txt);
            }
        }
    }
}

Simply writing TextBox4.Focus() into the for loop is not working.

for (int i = 1; i <= 5; i++)
            {
                TextBox txt = new TextBox();
                Point txtKonum = new Point(300, i * 30);
                txt.Location = txtKonum;
                txt.Name = "TextBox" + i;
                txt.Text = i.ToString();
                if(i == 4)
                {
                    txt.Focus();
                }
                this.Controls.Add(txt);
            }

This code does not work either. enter image description here

As you can see in the picture there is no cursor on the 4th textBox.

2 Answers

Solved, just put the this.Controls.Add(txt); code before the if statement,

            {
                TextBox txt = new TextBox();
                Point txtKonum = new Point(300, i * 30);
                txt.Location = txtKonum;
                txt.Name = "TextBox" + i;
                txt.Text = i.ToString();
                this.Controls.Add(txt);
                if(i == 4)
                {
                    txt.Focus();
                }

            } 

I've been playing around with this problem looking for an alternative and more versatile approach, and I came up with this method for giving focus to your your 4th iteration of dynamically created textboxes:

string focusedTextBox = "TextBoxName";//in this case "Textbox4"
Control focusControl = this.Controls[focusedTextBox];
focusControl.Focus();

In your application, it would look like this:

private void button1_Click(object sender, EventArgs e)
{
    for (int i = 1; i <= 5; i++)
    {
        TextBox txt = new TextBox();
        Point txtKonum = new Point(300, i * 30);
        txt.Location = txtKonum;
        txt.Name = "TextBox" + i;
        txt.Text = i.ToString();
        this.Controls.Add(txt);                
    }
    Control focusControl = this.Controls["Textbox4"];
    focusControl.Focus();
}

The obvious major advantage to this approach is that it will work from other places in the program. The only thing that has to be taken into account when calling this from a seperate method is that if the control.name doesn't exist an exception will be thrown, so it would probably be a good idea to set up some sort of safeguard or exception handling for that usage.

Related