0

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.

  • Well, there's no variable *called* `TextBox4`. Did you mean `txt.Focus()` ? – David Sep 12 '22 at 16:56
  • @David The name of the textBoxes are TextBox1, TextBox2,...,TextBox5 becauseof the for loop. So when I assign a function to them, I should use their name. – PhysicsSolvesAll Sep 12 '22 at 17:00
  • *"So when I assign a function to them, I should use their name."* - What exactly do you mean by that? *"assign a function to them"* - Are you trying to define a method on each object, or just **call** a method on them? *"I should use their name"* - Why? If you want to reference an object, you would use a variable or some other reference. Are you trying to dynamically **find** a control by its name? You can find it in the `this.Controls` collection then. – David Sep 12 '22 at 17:04
  • @David I want to call this Focus() method on the 4th instance of my textBox. Simply. If I just out txt.Focus() the code assigns the focus fonction on the 5th textBox, but I want it on the 4th textBox. – PhysicsSolvesAll Sep 12 '22 at 17:08
  • So... `if (i == 4) { txt.Focus(); }` ? Within the loop, check if this is "the 4th instance". If it is, call `.Focus()` on the `TextBox` object. – David Sep 12 '22 at 17:09
  • @David the if statement you wrote is not working I have tried it, I dont know why it does not work. – PhysicsSolvesAll Sep 12 '22 at 17:11
  • You need to do better than just "it's not working". What specifically are you trying and how specifically is it failing? – David Sep 12 '22 at 17:11
  • @David I put a picture of my code and its output, you can take a look at it if you wish, thank you. – PhysicsSolvesAll Sep 12 '22 at 17:16
  • Uhmm, you are trying to give focus to a TextBox that is not in any collection. What happens if you move the Controls.Add before the call to Focus()? – Steve Sep 12 '22 at 17:21
  • @Steve yeah that solves it. – PhysicsSolvesAll Sep 12 '22 at 17:24
  • @Steve yeah it seems like it solves it, but as you have said, it increases the number of textBoxes by one. – PhysicsSolvesAll Sep 12 '22 at 17:29

2 Answers2

1

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();
                }

            } 
-1

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.

Justin Edwards
  • 310
  • 1
  • 4
  • 7