I'm doing a Sudoku GUI in C#. All 81 textboxes are created at runtime. Their names are 11, 12, 13...19,21...98,99 respectively. At beginning the focus is in the textbox that has internally the name "11". I wish to navigate with the arrow keys. I.e. with the downkey I want to transfer the input focus to the textox "21". How can I do this? I didn't find a solution checking some C# books and the internet.
Thanks for any help.
Fritzli

Set KeyPreview=True and Handles KeyDown Event of Form

private void Form1_Load(object sender, EventArgs e)
        {
            int x=10,y=10;
            for (int i = 1; i <= 3; i++)
            {
                x=10;
                for (int j = 1; j <= 3; j++)
                {
                    TextBox t1 = new TextBox();
                    t1.Location = new Point(x, y);
                    t1.Size = new Size(10, 10);
                    x = x + 10;
                    Controls.Add(t1);
                }
                y = y + 10;
            }


        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {
            
            if (e.KeyCode == Keys.Up)
            {
                 
            }
            else
                if (e.KeyCode == Keys.Down)
                {

                }
        }
       
    }

PS: Read MSDN Pages about KeyPreview Property

I believe you mean how do you set focus to a control that doesn't exist at compile time because it shows an error that that control doesn't exist.

well its easier than you think.

private void setFocus(string txtbxName)
{
if (this.Controls.ContainsKey(txtbxName))
{
this.Controls[txtbxName].Focus();
}
}

this function takes a string that would be the name you give your text box. checks the class that owns the function for a control with that name, and if it has one, it will give it focus.

if your controls live on a container, like a panel or tabcontrol be sure to search their controls array, not the parent class. but its pretty straight forward.

it would seem to achieve such a thing, since tabstop works laterally only, that forward and down have the same effect cycling through tabstops. so you would have to do some checking, you would need to implement the previous post example, that would be subscribing to the key down events to catch the arrow keys, checking to see what control has focus, then move focus to the correct control via a similar function that I posted above. also be sure to set all the textboxes tabstop property to false, so not to cause a bug that will cause the the focus selection to become erratic. I have seen that before.

Happy Coding!

I believe you mean how do you set focus to a control that doesn't exist at compile time because it shows an error that that control doesn't exist.

well its easier than you think.

private void setFocus(string txtbxName)
{
if (this.Controls.ContainsKey(txtbxName))
{
this.Controls[txtbxName].Focus();
}
}


this function takes a string that would be the name you give your text box. checks the class that owns the function for a control with that name, and if it has one, it will give it focus.

if your controls live on a container, like a panel or tabcontrol be sure to search their controls array, not the parent class. but its pretty straight forward.

it would seem to achieve such a thing, since tabstop works laterally only, that forward and down have the same effect cycling through tabstops. so you would have to do some checking, you would need to implement the previous post example, that would be subscribing to the key down events to catch the arrow keys, checking to see what control has focus, then move focus to the correct control via a similar function that I posted above. also be sure to set all the textboxes tabstop property to false, so not to cause a bug that will cause the the focus selection to become erratic. I have seen that before.

Happy Coding!

Thanks a lot Diamonddrake.
I actually just needed this line:
----- this.Controls[txtbxName].Focus(); -------
I tried it and it was perfect.
Thanks again

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.