我在 WPF C# 中做简单的程序,我有很多 TextBoxes
- 每个TextBox
做同样的事情,我很懒惰为每个 TextBox
编写每个事件.
那么,有什么办法可以为所有 TextBox
提供服务吗?通过一个事件?
有一个短代码:
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;
}
谢谢! :)
最佳答案
将相同的处理程序附加到所有文本框并使用 sender
参数获取引发事件的文本框实例:
private void OnMouseLeft(object sender, MouseButtonEventArgs e)
{
TextBox textBox = (TextBox)sender;
textBox.Text = String.Empty;
textBox.Foreground = Brushes.Black;
}
关于c# - 所有文本框的一个事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17926160/