?
Solved

Combobox enter key press

Posted on 2005-03-15
5
Medium Priority
?
988 Views
Last Modified: 2012-06-21
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).
0
Comment
Question by:Tom Knowlton
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 13546904
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
 
LVL 5

Accepted Solution

by:
RoninThe earned 2000 total points
ID: 13547453
hi knowlton,
have you tried setting combobox dropdown style to "DropDownList". I guess that should work.

Ron
0
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 13547473
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
 
LVL 5

Expert Comment

by:RoninThe
ID: 13547790
>>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
 
LVL 5

Author Comment

by:Tom Knowlton
ID: 13547812
>>>>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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question