0

I have a Form with nine TextBox controls. Each one has a KeyPress event handler that fires on Enter/Return and more.

The fifth TextBox(Kategorie) and sixth (Ort) don't fire. The others do. The code is:

private void tb_Kategorie_KeyPress(object sender, KeyPressEventArgs e)
{
    MessageBox.Show("works");
    if (e.KeyChar == (char)Keys.Enter || e.KeyChar == (char)Keys.Return)
    {            
        tb_Ort.Focus();
    }
    else if (e.KeyChar == (char)Keys.Escape)
    {
        tb_Kategorie.Text = escSpeicher;
        tb_Kategorie.SelectAll();
    }
}

The event handler is set in the Designer and in designer.cs. The button and the code are not copy/pasted. Can someone tell me where the problem is?

DavidRR
  • 18,291
  • 25
  • 109
  • 191
  • 1
    Is this method linked to the event? You can see this in designer view of the form. Click on the textbox and check what's written in the KeyPress field in Properties window. – Marko Juvančič Dec 29 '14 at 19:59

3 Answers3

0

Try to set the Form.KeyPreview property to True

Sievajet
  • 3,443
  • 2
  • 18
  • 22
0

It is something with the Autocomplete Source. This is the same problem like in

Autocomplete on Combobox onkeypress event eats up the Enter key

It is not really solved, but a nice workaround

Community
  • 1
  • 1
0
// You Can Use Key Down method here
private void tb_Kategorie_KeyDown(object sender, KeyEventArgs e)
{
    MessageBox.Show("works");
    if (e.KeyCode==  Keys.Enter || e.KeyCode== Keys.Return)
    {

        tb_Ort.Focus();
    }
    else if (e.KeyCode==  Keys.Escape)
    {
        tb_Kategorie.Text = escSpeicher;
        tb_Kategorie.SelectAll();
    }
}

// try this

Bhavin
  • 240
  • 7
  • 19