Click here to Skip to main content
15,797,569 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to select a record (selected index) of a drop down list using enter key on keydown event or key press event.
here i am showing my works..

C#
private void cmbItem_KeyDown(object sender, KeyEventArgs e)
       {
           try
           {
               if (e.KeyCode == Keys.Enter)
               {
                   // how I should select a record of a dropdown list in here??

               }

           }
           catch (Exception ex)
           {
               logger.Error(ex);
           }

       }
Posted
Comments
[no name] 10-Oct-14 0:24am    
try this i think this will help you
combobox.items[combobox.selectedindex]
Prabhani Panamulla 10-Oct-14 6:30am    
no :(
Prabhani Panamulla 10-Oct-14 1:08am    
can anyone answer my question please?
Sinisa Hajnal 10-Oct-14 2:44am    
Fist, what have you tried?
Second, what happens when you hit Enter in the code above? That is, does it open the drop down instead of selecting?
Third, isn't irfan comment helpful? Why can't you use cmb.SelectedValue or index to get the current item?
BillWoodruff 10-Oct-14 5:42am    
You are wasting your time. The ComboBox simply does not behave in this fashion.

As already pointed out, the combobox dropdown index changed event arguments do not support the keypress KeyEventArgs. But what you can do is, add an event handler to the Selected index changed to invoke another subroutine that supports the key press KeyEventArgs.

Combobox Class: http://msdn.microsoft.com/en-us/library/system.windows.forms.combobox.selectedindexchanged%28v=vs.110%29.aspx[^]

Add Event Handler: http://msdn.microsoft.com/en-us/library/system.reflection.eventinfo.addeventhandler%28v=vs.110%29.aspx[^]

This link is an answer i provided on doing pretty much what I've suggested above:

How Can I Simulate The Down Key Using Sendkey In Vb.Net[^]

Keep in mind, because of the way you wish to do this, you will also need to make use of the SendMessage API which you can find out more about on Pinvoke.Net or in the above linked solution.

EDIT:

I would also like to point out that I advise against this approach as its impractical and unprofessional because there are specific event arguments created specifically for these controls which you are not using; and should be. Simply you do not need key press for this control because the user would be selecting an item firing the index changed event. So whatever you need to do in your keypress can be done in the index changed event.

Regards.
 
Share this answer
 
v2
In your code, I will assume that you're having a ComboBox with a name of myComboBox. Then the code would be like,

C#
// inside the if else block
// get the selectedItem as a ComboBoxItem 
ComboBoxItem selectedItem = (myComboBox.SelectedItem as ComboBoxItem);


This way, once you're done, the variable selectedItem will have the value of the ComboBoxItem object that was selected.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900