如何启用时间WinForm的按钮,按Tab键接收焦点 [英] How to enable a WinForm button in time to receive focus by tabbing

查看:222
本文介绍了如何启用时间WinForm的按钮,按Tab键接收焦点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Visual Studio 2010中,C#

Visual Studio 2010, C#

我有一个组合框下拉自动完成设置为 SuggestAppend AutoCompleteSource 是从 ListItems的。用户密钥数据到它,直到有正确的条目。 Utill的数据相匹配的列表项之一,一个按钮旁边的下拉框将被禁用。

I have a ComboBox with a DropDown, AutoComplete set to SuggestAppend and the AutoCompleteSource is from the ListItems. The user keys data into it until the have the correct entry. Utill the data matches one of the list items, a button next to the combobox is disabled.

如果用户点击自动完成功能接受当前建议Tab键。它也移动到已启用的标签序列中的下一个控件。当然,因为我希望它去的disbabled按钮,我需要尽快我验证项启用它。

If the user hits the tab key the autocomplete feature accepts current suggestion. It also moves on to the next control in tab sequence that is enabled. Of course since I want it to go to the disbabled button I need to enable it as soon as I validate the entry.

现在的问题是,没有我的事件已经试过, PreviewKeyDown 引发LostFocus 的SelectedIndexChanged 让我启用按钮时为它被proccessed和接收焦点。它总是转到在其中始终启用Tab顺序的下一个按钮。

The problem is that none of the events I've tried, PreviewKeyDown, LostFocus, SelectedIndexChanged allow me to enable the button in time for it to be proccessed and receive the focus. It always goes to the next button in tab order which is always enabled.

我即将准备离开启用按钮,并有如果太快按下它给一个错误,但我不想这样做的。我也不想进入有特殊模式的标志来跟踪时,这些控件接收焦点。验证似乎是很正常的事情,但我坚持。

I am about ready to leave the button enabled and have it give an error if pressed too soon but I don't want to do it that way. I also don't want to get into have special mode flags to keep track of when these controls receive focus. Validation seems to be a normal thing, but I'm stuck.

如果在的SelectedIndexChanged 当用户做出努力一场比赛,这将是很容易。当对话框将清除,也不当键入找到匹配它不火。

If the SelectedIndexChanged worked when the user made a match this would be easy. It doesn't fire when the box clears nor when a typed match is found.

推荐答案

您可以创建自己的组合框类封装此行为。事情是这样的:

You could create your own ComboBox class to encapsulate this behavior. Something like this:

using System;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            this.myComboBox1.TheButton = this.button1;

            this.myComboBox1.Items.AddRange( new string[] {
                "Monday",
                "Tuesday",
                "Wednesday",
                "Thursday",
                "Friday",
                "Saturday",
                "Sunday"
            } );

            button1.Enabled = false;
        }
    }

    public class MyComboBox : ComboBox
    {
        public Control TheButton { get; set; }

        public MyComboBox()
        {
        }

        bool IsValidItemSelected
        {
            get { return null != this.SelectedItem; }
        }

        protected override void OnValidated( EventArgs e )
        {
            if ( null != TheButton )
            {
                TheButton.Enabled = this.IsValidItemSelected;
                TheButton.Focus();
            }

            base.OnValidated( e );
        }

        protected override void OnTextChanged( EventArgs e )
        {
            if ( null != TheButton )
            {
                TheButton.Enabled = this.IsValidItemSelected;
            }

            base.OnTextChanged( e );
        }
    }
}

这篇关于如何启用时间WinForm的按钮,按Tab键接收焦点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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