473,505 Members | 15,976 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ComboBox Code for Mouse and Keyboard

I am using Visual C# .NET 2003 to make a ComboBox accept
both mouse and keyboard selection. For mouse selection
code, I double-clicked ComboBox to get the
default "comboBox1_SelectedIndexChanged" event. This code
(see below) by itself works as expected with a mouse. But
when using keyboard arrows to navigate the ComboBox drop-
down, the SelectedIndexChanged event is triggered which
is what I don't want using the keyboard. Instead, I want
the selection to be made when user presses the "Enter"
key.

Also, to accept selection using ONLY keyboard, I coded
the "comboBox1_KeyDown" event. I want user to navigate
ComboBox drop-down list using arrow keys and on "Enter"
key, accept the selection. This code by itself works fine
(when "comboBox1_SelectedIndexChanged" event is commented
out). I am trying to make both of these events work
together but I am having a hard time figuring this out. I
tried playing with the "comboBox1_Click" event but that
didn't help. I also tried working with
the "SelectionChangeCommitted" event from a suggestion
but I'm still unsuccessful. Thanks in advance for any
help on this.

// Preliminaries...
this.comboBox1.DropDownStyle =
System.Windows.Forms.ComboBoxStyle.DropDownList;
this.comboBox1.Items.AddRange(new object[] {"Display form
A", "Display form B"});
this.comboBox1.KeyDown += new
System.Windows.Forms.KeyEventHandler
(this.comboBox1_KeyDown);
this.comboBox1.SelectedIndexChanged += new
System.EventHandler(this.comboBox1_SelectedIndexCh anged);

private void comboBox1_SelectedIndexChanged(object
sender, System.EventArgs e)
{
switch(comboBox1.SelectedItem.ToString())
{
case "Display form A":
MessageBox.Show("Display form A");
break;
case "Display form B":
MessageBox.Show("Display form B");
break;
}
}

private void comboBox1_KeyDown(object sender,
System.Windows.Forms.KeyEventArgs e)
{
if(e.KeyCode == Keys.Enter)
{
switch(comboBox1.SelectedItem.ToString())
{
case "Display form A":
MessageBox.Show("Display form A");
break;
case "Display form B":
MessageBox.Show("Display form B");
break;
}
}
}
Nov 15 '05 #1
0 5759

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
1449
by: ilPostino | last post by:
This question is for long term coders hense the extra posting on c# ;) I find coding for long periods and on big projects frustrating when trying to use keyboard AND mouse. Do any of you use just...
8
3411
by: Nolan Martin | last post by:
Is it just me or are there no good librarys out there to access the keyboard or mouse? I have been looking around for a while now and can only find ones that are bundled with a bunch of other...
4
36303
by: Sue Bricker | last post by:
Greetings. In Access, is there a way to open up a drop-down combobox with keys on the keyboard, instead of using the mouse to click on the down arrow of the combobox? I really prefer to use the...
0
2652
by: Hiroyuki Tanaka | last post by:
Hi All, I am trying to develop an application for a touch screen using buttons for the numeric pad with Completion ComboBoxes. At the moment I am having a problem sending the button presses to...
2
4316
by: pei_world | last post by:
I want to implement a key hit with enter to dropdown a combobox that is in the datagrid. in this case I need to override its original behaviours. I found some codes from the web. Does anyone know...
9
4138
by: Strahimir Antoljak | last post by:
Could anyone help me with the following: Is there a quick, or at least known way to force a ComboBox, having DropDownStyle= DropDownList, to recognize keyboard input and perform Item selection...
2
2904
by: Horst JENS | last post by:
Hi group, have problems with combobox-control in a form (access 2003). I added VBA-Code to on-Change event of the control. If the user choose an value from the control, the filter of the form...
0
2585
by: Andrus | last post by:
SWF ComboBox dropdown menu first item selection must open picklist. For this I use the following code: protected override void OnSelectedIndexChanged(System.EventArgs e) { if (SelectedIndex != 0...
5
2252
by: JohnRou | last post by:
I'm having a really weird problem with a combo box (drop down list) on a databound form. I load 3 values programmatically into the combo box "MALE" "FEMALE" and "UNKNOWN". When I run the app,...
0
7218
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7103
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
7021
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5614
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4701
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3188
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1532
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
755
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
409
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.