One Event for all TextBoxes

c#, events, textbox, wpf

Solution

Attach same handler to all textboxes and use `sender` argument to get textbox instance which raised event:

private void OnMouseLeft(object sender, MouseButtonEventArgs e)
{
    TextBox textBox = (TextBox)sender;
    textBox.Text = String.Empty;
    textBox.Foreground = Brushes.Black;
}

Problem

I´m doing simple program in WPF C# and I have many `TextBoxes` - each `TextBox` do same thing and I´m very lazy to write each Event for each `TextBox`. So, Is there any way how to serve all `TextBox` by one Event? There is a short code: ``` private void OnMouseLeft(object sender, MouseButtonEventArgs e) { TextBox1.Text = string.Empty; TextBox1.Foreground = Brushes.Black; } private void OnMouseLeft1(object sender, MouseButtonEventArgs e) { TextBox2.Text = string.Empty; TextBox2.Foreground = Brushes.Black; } ``` Thank You! :)

Original source