I have a form that has two text boxes. The first is visible, and the second one is not. How do I go about having the second text box become visible if the user tabs out of the first text box? I wrote a KeyDown event handler to check for the ENTER key, doubled up the code to check for the TAB key, but it doesn't work.
So currently, if a user presses the ENTER key, the second textbox and label become visible, and focus shifts to the second text box. However, if a user presses the TAB key, focus shifts to the first button at the bottom of the form and the second text box and label stay hidden. What I'm looking for is the same response from either the ENTER or the TAB.
I tried using the TextChanged event, but the second textbox becomes visible at the first key press, and not at just TAB or ENTER.

private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                this.password1 = textBox1.Text;
                this.textBox2.Visible = true;
                this.label2.Visible = true;
                this.textBox2.Focus();
            }
            else
                if (e.KeyCode == Keys.Tab)
                {
                    this.password1 = textBox1.Text;
                    this.textBox2.Visible = true;
                    this.label2.Visible = true;
                    this.textBox2.Focus();
                }
        }

This is probably super easy, but I just cant figure it out.
Thanks!

Jim

Recommended Answers

All 3 Replies

Use the Leave event.

Use the Leave event.

Thanks.
Now I feel stupid.

:)

@revjim44
I would like to add to this solved question that you don't have to feel stupid. You posed a decent question and you learned something from Momerath, who was way ahead before me to give you the answer :D

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.