Tim
Tim

Reputation: 1344

Combo Box Enter Event WPF

I need to have an event fired whenever Enter is pressed inside of the combobox. This is a WPF C# 4.0 control, and I am unable to find a specific event handler to do this. I think I am missing something, as this seems like something that would be included. Is there preexisting code to accomplish this task?

I have also tried:

private void comboBox1_SelectionChanged(
    object sender,
    SelectionChangedEventArgs e)
{
     if (e.Equals(Key.Enter))
     {
         // Do Something
     }
}

Upvotes: 3

Views: 7306

Answers (2)

Mustafa Ekici
Mustafa Ekici

Reputation: 7480

 private void comboBox1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Return)
        {         
           // do stuff
        }
        else
        {

            // do stuff       
        }
    }

Upvotes: 5

Nacimota
Nacimota

Reputation: 23295

    private void comboBox1_KeyUp(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Enter)
        {
            // do stuff
        }
    }

or

    private void comboBox1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.Key == Key.Enter)
        {
            // do stuff
        }
    }

The difference being that KeyUp is when the key is released, KeyDown is when it is first pressed.

Upvotes: 4

Related Questions