Click here to Skip to main content
15,667,355 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

I created some text boxes in C# at run time.
I want to change focus of text box by typing a letter from a text box to another text box (for a crossword puzzle):

it's code for check that just enter letter
must change focus in this method?how?
my form has a combo box that this items are (horizontal,vertica,deactive). when combo box index change, direction of change text boxes change too.(down or left or right or up)
private void textBox_KeyPress(object sender, KeyPressEventArgs e)
           // Allows only letters
           if (!(Char.IsLetter(e.KeyChar) || Char.IsControl(e.KeyChar)))
                   e.Handled = true;


in this part create text boxes
public void CreateTb()
            for (i = 0; i < 15; i++)
                for (j = 0; j < 15; j++)
                    CrossTb = new TextBox();
                    TbTemp[i, j] = CrossTb;
                    CrossTb.Multiline = true;
                    CrossTb.Size = new Size(w, h);
                    CrossTb.MaxLength = 1;
                    CrossTb.TextAlign = HorizontalAlignment.Center;
                    CrossTb.BackColor = Color.White;
                    CrossTb.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
                    CrossTb.Location = new Point((i + fx) * w, (j + fy) * h);
TbTemp[i, j].KeyPress += new KeyPressEventHandler(textBox_KeyPress);
                //TbTemp[10, 10].ForeColor = Color.Black;

It's my code to create textboxes.
where i must check key for go to another text box and how?
whenever that i test doesn't true :(

Thanks a lot for your help :)
Updated 31-May-10 8:58am

Add a handler for the TextBox KeyPress event.
Decide which text box is next to receive the keystroke and use the Focus() method.
For example, if there were two boxes only:
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)

private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
You will want a more generic method, but that is over to you - next right or down will depend on how you are organising your software.
Share this answer
You can call tab event through code to move focus from one textbox to the other.
Share this answer

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