locked
text changed event for textboxes in c# windows forms RRS feed

  • Question

  •  

    hi
    i got 3 group boxes on my form which contains some 5-10 text boxes each so when the user makes any changes selects the cancel button it  will dispaly a confirm box saying yes,no if their are any changes it should cancel if no is presed else it should save if yes is pressed
    so my question is should i write textchanged event for all the text boxes or else is their any better way of  doing   it
    thanks
    Monday, January 7, 2008 5:37 PM

Answers

  • Hi, chaitu

    You can assign the same event handler function for all the TextBoxes on the PropertyGrid in IDE without writing code to add the handler. For example, you have create a handler function named "TextBox_TextChanged", the you can just go to the Properties explorer, select a TextBox on the form, and on the Events tab of the PropertyGrid, select the TextChanged item, and choose the "TextBox_TextChanged" from the dropdown list. Repeat this process for all the TextBoxes that need to do the same stuff when text changed.

    Best Regards.
    Zhi-xin Ye.

    Friday, January 11, 2008 9:34 AM

All replies

  •  

    private void BaseForm_Shown(object sender, EventArgs e)

    {

    foreach (Control ctl in this.Controls)

    {

    AddKeyPressEvent(ctl);

    }

    }

     

     

    private void AddKeyPressEvent(Control objControl)

    {

     

    if (objControl.GetType().FullName == "System.Windows.Forms.TextBox")

    {

    TextBox utl = (TextBox)objControl;

    utl.TextChanged += new EventHandler(utl_TextChanged);

    }

    foreach (Control ctl in objControl.Controls)

    {

    AddKeyPressEvent(ctl);

    }

    }

     

    void utl_TextChanged(object sender, EventArgs e)

    {

    // validation

    }

     

    }

    Tuesday, January 8, 2008 5:24 AM
  • Hi, chaitu

    You can assign the same event handler function for all the TextBoxes on the PropertyGrid in IDE without writing code to add the handler. For example, you have create a handler function named "TextBox_TextChanged", the you can just go to the Properties explorer, select a TextBox on the form, and on the Events tab of the PropertyGrid, select the TextChanged item, and choose the "TextBox_TextChanged" from the dropdown list. Repeat this process for all the TextBoxes that need to do the same stuff when text changed.

    Best Regards.
    Zhi-xin Ye.

    Friday, January 11, 2008 9:34 AM