如何在DroppedDown = false时阻止组合框中向上箭头的默认行为 [英] How to prevent the default behaviour of up arrow in a combo box when the DroppedDown=false

查看:100
本文介绍了如何在DroppedDown = false时阻止组合框中向上箭头的默认行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的专业人士,



我有一个组合框,如果在下拉关闭时按下向上箭头,我希望焦点移到上一个我在组合框控件的KeyDown事件中注意的文本框。



每当按下向上箭头时,焦点移动到上一个文本框,但所选项目在组合框中更改为其上一个项目。我可以停止这种默认行为吗?



请帮忙。提前致谢。

处理KeyDown的代码

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

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

推荐答案

您需要让KeyDown事件知道您不知道我不想用那个按键做任何其他事情。



尝试添加 e.Handled = true; ie

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.
private void cbTest_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Up)
    {
        if (cbTest.DroppedDown)
            return;
        else
        {
            txtTest.Focus();
            e.Handled = true;
        }
    }
}


这篇关于如何在DroppedDown = false时阻止组合框中向上箭头的默认行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆