Combobox enter key press

I feel dumb asking this question....but I won't let that stop me because I want to find out how to do this.

In a C# Windows Form....I have a ComboBox filled with data.  I can PICK an item from the list of items in the ComboBox and that works just fine.

But if I type in the item by hand  (in the combo box text area) and then press ENTER....nothing happens.

What event am I supposed to wire up to the combo box such that an ENTER key press does the same thing as picking an item.

NOTE:  Also...I want to be limited to typin in ONLY valid items that are already listed in the combo box.

NOTE:  The items in the combo box are a list of state abbreviations (WA, UT, WI, NY, etc).
LVL 5
Tom KnowltonWeb developerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Tom KnowltonWeb developerAuthor Commented:
Okay..............somehow I missed the KeyDown property..........once I found it I got the ComboBox to work the way I wanted.


I have noticed however.....that every time I press the ENTER key....I get a System Beep that sounds.....WHY??????



private void comboBoxState_SelectedIndexChanged(object sender, System.EventArgs e)
            {
                  try
                  {
                        if ( ! this.comboBoxState.Focused )
                        {
                              return;
                        }
                        else
                        {
                              //get the state
                              string tempState="";
                  
                              tempState = this.comboBoxState.Text;

                              FillAreaCodeBox(tempState);                  
                        }
                  }
                  catch(Exception ee)
                  {
                        System.Diagnostics.Debug.WriteLine(ee.ToString());
                  }
            }

            
            private void comboBoxState_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
            {
                  if((e.KeyCode.ToString() == "Enter"))
                  {
                        string tempState = this.comboBoxState.Text;
                        FillAreaCodeBox(tempState);
                  }
            }


            private void FillAreaCodeBox(string tempState)
            {
                  //show all area codes under that state in the Area Codes box
                  string SQL = "select AreaCode from tblAreaCodes where State = '" + tempState + "'";

                  this.listBoxAreaCodes.DataSource = genFuncs.GenerateDataSet(SQL, this.strDBConnection).Tables[0];

                  this.listBoxAreaCodes.DisplayMember = "AreaCode";
                  this.listBoxAreaCodes.ValueMember = "AreaCode";                                                
            }
0
RoninTheCommented:
hi knowlton,
have you tried setting combobox dropdown style to "DropDownList". I guess that should work.

Ron
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Tom KnowltonWeb developerAuthor Commented:
I tried changing the DropDown list Style to DropDownList.....

This seems to have taken care of it.


=====================================


Now.....I am still curious about why the system DING  when I pressed enter (under the other DropDown style?)
0
RoninTheCommented:
>>Also...I want to be limited to typin in ONLY valid items that are already listed in the combo box.

For this you'll have to set CausesValidation property to true(which is default)

>>Now.....I am still curious about why the system DING  when I pressed enter (under the other DropDown style?)

I don't have the exact answer to this question. When the DropDownStyle is dropdown..no selectionchanged event is fired when you type in the combobox text area..so when you press enter nothing happens..just a DING...
0
Tom KnowltonWeb developerAuthor Commented:
>>>>I don't have the exact answer to this question. When the DropDownStyle is dropdown..no selectionchanged event is fired when you type in the combobox text area..so when you press enter nothing happens..just a DING...


Understood.


This is no big deal.....I have the ListBox working how I want it to work now.........


If you do find the answer...please return here and post it.   Just for the sake of curiousity.


Thanks for your help!!!!

Tom
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.