ckaknleapex
ckaknleapex

Reputation: 3

Why won't my Enter Key Events in WPF C# do anything?

My enter key events won't do anything...not even show a simple textbox when pressing Enter in a textbox.

I am new to c# and coding in general.

Interestingly, my visual studio won't let some things go through like MessageBox.Show... It makes me do System.Windows.MessageBox.Show. Just in case this is a clue to what the problem may be...

Here is what I have...

private void textBoxPartNumber_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
    {
        if (e.KeyCode == System.Windows.Forms.Keys.Enter)
        {
            //textBoxQuantity.Focus();
            System.Windows.MessageBox.Show("Testing 123");
            System.Windows.Forms.SendKeys.Send("{TAB}");                
            e.Handled = true;
            e.SuppressKeyPress = true;
        }
    }

Upvotes: 0

Views: 930

Answers (3)

Chris Dunaway
Chris Dunaway

Reputation: 11216

If you're using WPF then it appears your event signature is incorrect. Try something like this:

private void textBox_KeyDown(object sender, KeyEventArgs e)
{
    if (e.Key == Key.Enter)
    {
        //Do something
    }
}

The KeyEventArgs class and Key enum are in the System.Windows.Input namespace in the PresentationCore assembly.

Upvotes: 0

paparazzo
paparazzo

Reputation: 45096

TextBox property AcceptsReturn

<TextBox AcceptsReturn="true"/>

Upvotes: 2

Shaamil Ahmed
Shaamil Ahmed

Reputation: 378

Use

if (e.KeyCode == System.Windows.Forms.Keys.Return)

Instead :)

Upvotes: 1

Related Questions