0

Today I got a problem in my development.

I have a Windows Form like this :

Windows Form

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. 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.

I tried to put the Keydown event directly in my windows form but it doesn't work. So my question is, how can I do this. If someone have an idea ?

Thank you in advance !

Thomas

4
  • 4
    There is a TextChanged event and you can hook up many TextBoxes to the same event. To discern which triggered it you can cast the sender param to TextBox.. Commented Feb 21, 2017 at 11:07
  • You set up a form with 100+ TextBoxes and now you are too lazy to set one event for each of them ;P (just kidding) No, seriously: That 100+ Boxes Form, is it created dynamically through code? Then you can set the eventHandler through the same creation process. Probably to one single Handler, as TaW suggests. Commented Feb 21, 2017 at 11:10
  • @Fildor I'm using Visual Studio to design my forms. Commented Feb 21, 2017 at 11:25
  • Not sure if it's possible to set an event handler in bulk. Have you tried to select two Textfields (Ctrl+Click)? Can you access the Events Tab in the Property-Browser and set a Handler if you do? Commented Feb 21, 2017 at 11:31

2 Answers 2

1

Since you already have 100+ textboxes in your form. 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;
            }
Sign up to request clarification or add additional context in comments.

Comments

0

What you can do is to extend TextBox make a field ( accessible from the designer ) to bind that TextBox into some other control.

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. 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.

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.

To make it other way around ( one button dependant on many text boxes ) you can extend Button object :

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;
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.