简体   繁体   中英

select combobox item by pressing enter key in c# windows aplication

In c#, I want to select combo box items by keyboard and when i press enter after selecting one that item should be selected. how to do it?

try something like this ..., this will explain how to change the items using mouse and key board ,....

I found this method worked fine in all the conditions. But I m not sure if anything more accurate than this method is available.

    bool IsMouse = false;

    private void cmbMy_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {
        if (IsMouse)
        {
            //Write the logic if selection is changed by mouse
        }
        else
        {
            //Write the logic if selection is changed by keyboard
        }

        IsMouse = false;
    }

    private void cmbMy_IsMouseCapturedChanged(object sender, DependencyPropertyChangedEventArgs e)
    {
        IsMouse = true;
    }

In isMouseCapturedChanged event of combo box i made a bool variable true and when selection changed of the combo box im checking the bool doing the required task and then setting isMouse to false.

Or you need to bulid your own custom combobox ..

You need build a custom ComboBox class and override the Control.ProcessKeyEventArgs Method .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM