By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,827 Members | 813 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,827 IT Pros & Developers. It's quick & easy.

Select an item in a combo box with enter key

P: n/a
Hi,

I am having a problem trying to select an item with the enter key.

I want to work the combo with the keyboard.

So when I use the down arrow to browse the list I want to then
hit the enter key as if selecting an item with the mouse.

How do I do this?

Can't seem to get it working.

Is this a setup of the combo issue?

rotsey
Jun 26 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Hi Rotsey,

Winfoms I guess?
If yes then why do you need to select it by Enter if on keyboard-mode SelectedIndexChanged
is called by any cursor movement?

Kind Regards, Alex Meleta
[TechBlog] http://devkids.blogspot.com

RHi,
R>
RI am having a problem trying to select an item with the enter key.
R>
RI want to work the combo with the keyboard.
R>
RSo when I use the down arrow to browse the list I want to then hit
Rthe enter key as if selecting an item with the mouse.
R>
RHow do I do this?
R>
RCan't seem to get it working.
R>
RIs this a setup of the combo issue?
R>
Rrotsey
R>
Jun 26 '07 #2

P: n/a
Hi,

You are automatically selecting the items as you navigate using the Arrow
keys. So hitting the Enter key doesn't have any speciifc relevance as the
item would already have been selected by the time you hit the Enter key.

Please see the code below which demonstrates this:

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
this.comboBox1.SelectedIndexChanged +=new
EventHandler(comboBox1_SelectedIndexChanged);
//
}

static void Main()
{
Application.Run(new Form1());
}

private void Form1_Load(object sender, System.EventArgs e)
{
comboBox1.Items.Add("A");
comboBox1.Items.Add("B");
comboBox1.Items.Add("C");
comboBox1.Items.Add("D");
comboBox1.Items.Add("E");

}

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
Debug.WriteLine(this.comboBox1.SelectedItem);
}

Hope this helps!
Thanks -
"Rotsey" <ma***********@RemoveThis.optusnet.com.auwrote in message
news:et*************@TK2MSFTNGP06.phx.gbl...
Hi,

I am having a problem trying to select an item with the enter key.

I want to work the combo with the keyboard.

So when I use the down arrow to browse the list I want to then
hit the enter key as if selecting an item with the mouse.

How do I do this?

Can't seem to get it working.

Is this a setup of the combo issue?

rotsey

Jun 26 '07 #3

P: n/a
what I have is a form that displays data from db and a filter
on employee name combo

I want to be able to arrow key through the list and then hit enter when
I find the employee and then only requery the form on enter

is this possible and how?

"Shine Xavier" <sh**********@gmail.comwrote in message
news:%2******************@TK2MSFTNGP06.phx.gbl...
Hi,

You are automatically selecting the items as you navigate using the Arrow
keys. So hitting the Enter key doesn't have any speciifc relevance as the
item would already have been selected by the time you hit the Enter key.

Please see the code below which demonstrates this:

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
this.comboBox1.SelectedIndexChanged +=new
EventHandler(comboBox1_SelectedIndexChanged);
//
}

static void Main()
{
Application.Run(new Form1());
}

private void Form1_Load(object sender, System.EventArgs e)
{
comboBox1.Items.Add("A");
comboBox1.Items.Add("B");
comboBox1.Items.Add("C");
comboBox1.Items.Add("D");
comboBox1.Items.Add("E");

}

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
Debug.WriteLine(this.comboBox1.SelectedItem);
}

Hope this helps!
Thanks -
"Rotsey" <ma***********@RemoveThis.optusnet.com.auwrote in message
news:et*************@TK2MSFTNGP06.phx.gbl...
>Hi,

I am having a problem trying to select an item with the enter key.

I want to work the combo with the keyboard.

So when I use the down arrow to browse the list I want to then
hit the enter key as if selecting an item with the mouse.

How do I do this?

Can't seem to get it working.

Is this a setup of the combo issue?

rotsey


Jun 26 '07 #4

P: n/a
Yes it is possible! You could consume the "KeyUp" event of the ComboBox for
doing this:
--------------------------------------------------------------------------------------------
The code below gives you a clear picture of the sequence in which the
relevant events are triggered.

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
this.comboBox1.SelectionChangeCommitted +=new
EventHandler(comboBox1_SelectionChangeCommitted);
this.comboBox1.KeyDown +=new KeyEventHandler(comboBox1_KeyDown);
this.comboBox1.KeyUp +=new KeyEventHandler(comboBox1_KeyUp);
//
}

static void Main()
{
Application.Run(new Form1());
}

private void Form1_Load(object sender, System.EventArgs e)
{
comboBox1.Items.Add("A");
comboBox1.Items.Add("B");
comboBox1.Items.Add("C");
comboBox1.Items.Add("D");
comboBox1.Items.Add("E");

}
private void comboBox1_SelectionChangeCommitted(object sender, EventArgs
e)
{
Debug.WriteLine(this.comboBox1.SelectedItem);
}

private void comboBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
Debug.WriteLine("ENTER KEY DOWN");
}
}

private void comboBox1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
Debug.WriteLine("ENTER KEY UP");
/*Do your DB Re-Query operation here as this gets triggered after the
SelectChangeCommitted event...*/
}
}
Hope this helps!
Thanks-
"Rotsey" <ma***********@RemoveThis.optusnet.com.auwrote in message
news:%2********************@TK2MSFTNGP05.phx.gbl.. .
what I have is a form that displays data from db and a filter
on employee name combo

I want to be able to arrow key through the list and then hit enter when
I find the employee and then only requery the form on enter

is this possible and how?

"Shine Xavier" <sh**********@gmail.comwrote in message
news:%2******************@TK2MSFTNGP06.phx.gbl...
>Hi,

You are automatically selecting the items as you navigate using the Arrow
keys. So hitting the Enter key doesn't have any speciifc relevance as the
item would already have been selected by the time you hit the Enter key.

Please see the code below which demonstrates this:

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
this.comboBox1.SelectedIndexChanged +=new
EventHandler(comboBox1_SelectedIndexChanged);
//
}

static void Main()
{
Application.Run(new Form1());
}

private void Form1_Load(object sender, System.EventArgs e)
{
comboBox1.Items.Add("A");
comboBox1.Items.Add("B");
comboBox1.Items.Add("C");
comboBox1.Items.Add("D");
comboBox1.Items.Add("E");

}

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
Debug.WriteLine(this.comboBox1.SelectedItem);
}

Hope this helps!
Thanks -
"Rotsey" <ma***********@RemoveThis.optusnet.com.auwrote in message
news:et*************@TK2MSFTNGP06.phx.gbl...
>>Hi,

I am having a problem trying to select an item with the enter key.

I want to work the combo with the keyboard.

So when I use the down arrow to browse the list I want to then
hit the enter key as if selecting an item with the mouse.

How do I do this?

Can't seem to get it working.

Is this a setup of the combo issue?

rotsey



Jun 26 '07 #5

P: n/a
Hi,

"Rotsey" <ma***********@RemoveThis.optusnet.com.auwrote in message
news:%2********************@TK2MSFTNGP05.phx.gbl.. .
what I have is a form that displays data from db and a filter
on employee name combo

I want to be able to arrow key through the list and then hit enter when
I find the employee and then only requery the form on enter
>
is this possible and how?
Yes, It's possible

You have to use Form.ProcessCmdKey.

Jun 26 '07 #6

P: n/a
Ok thanks I will give it ago

"Shine Xavier" <sh**********@gmail.comwrote in message
news:OR*************@TK2MSFTNGP06.phx.gbl...
Yes it is possible! You could consume the "KeyUp" event of the ComboBox
for doing this:
--------------------------------------------------------------------------------------------
The code below gives you a clear picture of the sequence in which the
relevant events are triggered.

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
this.comboBox1.SelectionChangeCommitted +=new
EventHandler(comboBox1_SelectionChangeCommitted);
this.comboBox1.KeyDown +=new KeyEventHandler(comboBox1_KeyDown);
this.comboBox1.KeyUp +=new KeyEventHandler(comboBox1_KeyUp);
//
}

static void Main()
{
Application.Run(new Form1());
}

private void Form1_Load(object sender, System.EventArgs e)
{
comboBox1.Items.Add("A");
comboBox1.Items.Add("B");
comboBox1.Items.Add("C");
comboBox1.Items.Add("D");
comboBox1.Items.Add("E");

}
private void comboBox1_SelectionChangeCommitted(object sender, EventArgs
e)
{
Debug.WriteLine(this.comboBox1.SelectedItem);
}

private void comboBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
Debug.WriteLine("ENTER KEY DOWN");
}
}

private void comboBox1_KeyUp(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
Debug.WriteLine("ENTER KEY UP");
/*Do your DB Re-Query operation here as this gets triggered after the
SelectChangeCommitted event...*/
}
}
Hope this helps!
Thanks-
"Rotsey" <ma***********@RemoveThis.optusnet.com.auwrote in message
news:%2********************@TK2MSFTNGP05.phx.gbl.. .
>what I have is a form that displays data from db and a filter
on employee name combo

I want to be able to arrow key through the list and then hit enter when
I find the employee and then only requery the form on enter

is this possible and how?

"Shine Xavier" <sh**********@gmail.comwrote in message
news:%2******************@TK2MSFTNGP06.phx.gbl. ..
>>Hi,

You are automatically selecting the items as you navigate using the
Arrow keys. So hitting the Enter key doesn't have any speciifc relevance
as the item would already have been selected by the time you hit the
Enter key.

Please see the code below which demonstrates this:

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
this.comboBox1.SelectedIndexChanged +=new
EventHandler(comboBox1_SelectedIndexChanged);
//
}

static void Main()
{
Application.Run(new Form1());
}

private void Form1_Load(object sender, System.EventArgs e)
{
comboBox1.Items.Add("A");
comboBox1.Items.Add("B");
comboBox1.Items.Add("C");
comboBox1.Items.Add("D");
comboBox1.Items.Add("E");

}

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
Debug.WriteLine(this.comboBox1.SelectedItem);
}

Hope this helps!
Thanks -
"Rotsey" <ma***********@RemoveThis.optusnet.com.auwrote in message
news:et*************@TK2MSFTNGP06.phx.gbl...
Hi,

I am having a problem trying to select an item with the enter key.

I want to work the combo with the keyboard.

So when I use the down arrow to browse the list I want to then
hit the enter key as if selecting an item with the mouse.

How do I do this?

Can't seem to get it working.

Is this a setup of the combo issue?

rotsey



Jun 26 '07 #7

P: n/a
On Tue, 26 Jun 2007 06:48:06 -0700, Ignacio Machin ( .NET/ C# MVP )
<machin TA <"laceupsolutions.com>"wrote:
>I want to be able to arrow key through the list and then hit enter when
I find the employee and then only requery the form on enter

is this possible and how?

Yes, It's possible

You have to use Form.ProcessCmdKey.
I disagree on at least two counts: 1) there are other ways to do it, and
2) Control.ProcessCmdKey() isn't even the best way to do it.

Personally, I'd sub-class the control and override the
Control.ProcessDialogKey() method.

ProcessCmdKey() is more commonly used for keyboard accelerator shortcuts
(Alt-<key>, Ctrl-<key>, etc.) while ProcessDialogKey() is intended for
situations in which a modifier key isn't relevant. As also has been
pointed out, one can subscribe to the KeyUp or KeyDown events (I prefer
KeyDown, because you will get one for each key repeat, but sometimes you
don't want to handle key repeats in which case KeyUp is preferable). You
can even subscribe to KeyPress if what you care about is the character
code generated rather than a specific key.

Not that you can't do it using ProcessCmdKey()...I just feel that there
are other, more appropriate methods.

Pete
Jun 26 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.