Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an easy way to add an event to all form objects in C#

Tags:

c#

.net

forms

I have a form with many labels and text boxes. I'd like to have the title highlighted red if any of the fields are modified. Is there an easy way to do this or do you need to add the event callback to each form object individually? Thanks!

like image 368
giroy Avatar asked Nov 13 '22 02:11

giroy


1 Answers

Off the top of my head you could do something like this in the form load event to add the events...

foreach (var control in this.Controls)
{
    if (control is Label)
    {
        ((Label)control).TextChanged += Controls_TextChanged;
    }
    else if (control is TextBox)
    {
        ((TextBox)control).TextChanged += Controls_TextChanged;
    }
}
like image 179
MrBlue Avatar answered Nov 14 '22 21:11

MrBlue