Click here to Skip to main content
16,016,613 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear Professionals,

I have a combo box in which, if up arrow is pressed when drop down is closed, I want the focus to move to the previous textbox which I have taken care in the KeyDown event of the combo box control.

Whenever up arrow is pressed, the focus moves to the previous textbox, but the selected item in the combo box changes to its previous item. Can I stop this default behavior ?

Please help. Thanks in advance.
The code to handle KeyDown
C#
private void cbTest_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Up)
    {
        if (cbTest.DroppedDown)
            return;
        else
        {
            txtTest.Focus();
        }
    }
}
Posted
Updated 1-Feb-15 22:43pm
v3
Comments
CHill60 2-Feb-15 4:29am    
Post the code you have used to move the focus
Priya-Kiko 2-Feb-15 4:38am    
The code to move the focus :

private void cbTest_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Up)
{
if (cbTest.DroppedDown)
return;
else
{
txtTest.Focus();
}
}
}

1 solution

You need to let the KeyDown event know that you don't want to do anything else with that key press.

Try adding e.Handled = true; i.e.
C#
private void cbTest_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Up)
    {
        if (cbTest.DroppedDown)
            return;
        else
        {
            txtTest.Focus();
            e.Handled = true;
        }
    }
}
 
Share this answer
 
Comments
Priya-Kiko 2-Feb-15 4:49am    
Thank you so much.

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