如何防止组合框中的向上和向下键上的SelectedItem更改 [英] How to Prevent SelectedItem Change On Up and Down Key in combobox

查看:75
本文介绍了如何防止组合框中的向上和向下键上的SelectedItem更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

在我的项目中,我使用的是导航功能,该功能用于通过Enter,Down和Up键在控件之间导航.

当我在ComboBox上使用它时,焦点移到了下一个控件,但是ComboBox的SelectedItem被更改了.

(示例:-如果组合框``a'',``b'',``c''中有3个主题,并且如果选择了``b'',则如果按向上键,焦点将转到ComboBox的上一个控件和ComboBox中的项目更改为"a",如果按下Down键,焦点将移至下一个控件,而ComboBox中的项目更改为"c".但是,如果按Enter键,焦点将移至下一个控件,selectedItem保持原样.

我正在尝试使用上移和下移键来移动上一个或下一个控件,以及从列表中按Alt-Down选择项的组合功能

这个怎么做?如果我在逻辑上必须这样做,那么是否存在任何属性,那又是怎么回事?

Hello,

In my project I am using a Navigate Function which is used to navigate between controls by Enter, Down and Up key.

When I use it on ComboBox the focus moves to next control, but SelectedItem of ComboBox is changed.

(Example:- If There are 3 itmes in combobox ''a'',''b'',''c'' and if ''b'' is selected, If I press Up, focus goes to previous control of ComboBox and item in ComboBox changed to ''a'' and if Down is pressed focus goes to next control and item in ComboBox changed to ''c''. However if I press enter focus goes to next control and selectedItem remain as it is.

I am trying to get functionality on Up and Down Key to Move Previous Or Next Control and on combination of Alt-Down select item from list

How to Do this? Is there any property is present if I have to this logically then what''s the way?

推荐答案

不要这样做!就这么简单.您的用户会讨厌它.不要覆盖常见的UI行为;标准导航太重要了,不能牺牲.

—SA
Don''t do it! This is as simple as that. You user will hate it. Do not override common UI behavior; standard navigation is too important to sacrifice.

—SA


您需要将Form的KeyPreview 属性设置为true.这将使所有键在到达任何子控件之前都进入窗体.除此之外,您还必须处理主窗体的KeyUpKeyDown 消息,并且在处理这些消息时,如果组合框处于焦点位置,则必须将其标记为Handled .

You need to set the KeyPreview property of Form to true. This will make all the keys come to the Form before reaching any of the child controls. In addition to that, you will have to handle KeyUp, KeyDown messages for your main Form as well and in the handling of those, you will have to mark those as Handled if your Combobox is in focus.

private void Form1_KeyDown(object sender, KeyEventArgs e)
{
    if (comboBox1.Focused)
        e.Handled = true;
}



这是MSDN的链接,其中通过示例详细解释了此KeyPreview属性:



Here is a link from MSDN which explains this KeyPreview property in detail with examples: Link here[^]


尝试一下.
Try this.
private void comboBox1_KeyUp(object sender, KeyEventArgs e)
        {
            e.Handled = true;
            previous_contol_id.Focus();
        }
        private void comboBox1_KeyDown(object sender, KeyEventArgs e)
        {
            e.Handled = true;
            next_contol_idbutton1.Focus();
        }


这篇关于如何防止组合框中的向上和向下键上的SelectedItem更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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