简体   繁体   English

如何在C#中更改我的一个文本框时启用按钮?

[英]How to enable a button to when one of my textbox changed in C#?

Today I got a problem in my development. 今天,我的开发遇到了问题。

I have a Windows Form like this : 我有这样的Windows窗体:

Windows表格

I need to enable the button "Appliquer" when the content of one of my textbox change. 当我的一个文本框的内容更改时,我需要启用按钮“贴花”。 I know that I can put the KeyPress event on each textbox and enable my button with that. 我知道我可以将KeyPress事件放在每个文本框上,并启用该按钮。 In this window it can be easy to do that because there is only 10 textbox but I have an other window with more of 100 textbox and I think there is a better solution. 在此窗口中可以轻松完成此操作,因为只有10个文本框,但我还有另一个窗口有100个以上的文本框,我认为有更好的解决方案。

I tried to put the Keydown event directly in my windows form but it doesn't work. 我试图将Keydown事件直接放在Windows窗体中,但是它不起作用。 So my question is, how can I do this. 所以我的问题是,我该怎么做。 If someone have an idea ? 如果有人有想法?

Thank you in advance ! 先感谢您 !

Thomas 汤玛士

Since you already have 100+ textboxes in your form. 由于您的表单中已经有100多个文本框。 I am assuming performance is not an issue for you. 我认为性能对您来说不是问题。

In your form constructor, call this method. 在表单构造函数中,调用此方法。 It will attach the event to all the textbox controls present in your form & inside sub controls such as groupbox, panel etc. (if you require) 它将事件附加到窗体中存在的所有文本框控件以及内部子控件(如组框,面板等)中(如果需要)

There could be better ways of iteration.. 可能有更好的迭代方法。

public Form1()//your constructor
        {
            InitializeComponent();

            AttachEvent(this);

        }
     void AttachEvent(Control CTrl)
            {
                foreach (Control c in CTrl.Controls)
                {
                    if (c is TextBox)
                    {
                        c.TextChanged += new EventHandler(c_TextChanged);
                        continue;
                    }
                    if (c.HasChildren)
                    {
                        AttachEvent(c);
                    }
                }
            }

            void c_TextChanged(object sender, EventArgs e)
            {
                //Your Code here btnGo.Enabled = !btnGo.Enabled;
            }

What you can do is to extend TextBox make a field ( accessible from the designer ) to bind that TextBox into some other control. 您可以做的是扩展TextBox的字段(可从设计器访问)以将该TextBox绑定到其他控件中。

public class MeTextBox
    : TextBox
{
    public override string Text
    {
        get { return base.Text; }
        set
        {
            if ( m_DependantControl != null )
            {
                m_DependantControl.Enabled = !string.IsNullOrWhiteSpace(value);
            }
            base.Text = value;
        }
    }

    Control m_DependantControl;

    [Browsable(true)]
    public Control DependantControl
    {
        get { return m_DependantControl; }
        set { m_DependantControl = value; }
    }
}

Now you can use MeTextBox as a regular TextBox . 现在,您可以将MeTextBox用作常规TextBox And if you want to make it control Enabled flag of some other Control you can just specify DependantControl property which will be accessible in the designer. 如果你想让它控制Enabled一些其他的标志Control你可以指定DependantControl财产,这将是在设计访问。

Fitting this into your example ( code ): 使它适合您的示例( 代码 ):

// assume you have a Button named btnConfirm
// and want to enable this button only when your `TextBox` has some text
MeTextBox mtb = new MeTextBox();
mtb.DependantControl = btnConfirm;

And if you do not want to make it in the code you can use designer directly. 而且,如果您不想在代码中使用它,则可以直接使用Designer。

To make it other way around ( one button dependant on many text boxes ) you can extend Button object : 要使其变为另一种方式(一个按钮取决于许多文本框),可以扩展Button对象:

public class MeButton
    : Button
{
    List<TextBox> m_DependantOn = new List<Control>();

    [Browsable(true)]
    public List<TextBox> DependantOn
    {
        get { return m_DependantOn; }
        set { RemoveEvents(); m_DependantOn = value; AssignEvents(); }
    }

    void RemoveEvents()
    {
        foreach(TextBox ctrl in m_DependantOn)
            ctrl.TextChanged -= WhenTextChanged;
    }

    void AssignEvents()
    {
        foreach(TextBox.ctrl in m_DependantOn)
            ctrl.TextChanged += WhenTextChanged;
    }

    void WhenTextChanged(object sender, TextChangedEventArgs e)
    {
        this.Enabled = true;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM