Single Event for Multiple Controls

HLeyes

Member
Joined
Feb 10, 2016
Messages
15
Programming Experience
10+
Let's say that I have 10 textboxes on my form, and by tabbing, I move from one to the next for data input. As each textbox receives focus, I want the background to turn a certain color for visual highlighting. I could set that property individually for each control as the cursor enters and leaves the textbox, but I am wondering if there is some kind of single event that I can set that would set that property on any textbox as it receives focus, and as the cursor leaves the textbox.

Thanks in advance,

HLeyes
 
Attach the same event handler for all the textboxes and use the 'sender' parameter to change backcolor for that control.
 
You can use the Properties window in the designer to create or select the same method to handle an event for multiple controls or components. Simply select all the controls or components, open the Properties window, click the Events button and then double-click the desired event to create a common event handler or select an existing method from the drop-down for that event. Inside the method, you can do something like this:
private void TextBoxes_Enter(object sender, EventArgs e)
{
    var tb = sender as TextBox;

    if (tb != null)
    {
        // Use tb here.
    }
}
 
Back
Top Bottom