所有文本框的事件之一 [英] One Event for all TextBoxes

查看:319
本文介绍了所有文本框的事件之一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

I'm在WPF C#做简单的计划,我有很多的文本框 - 每个文本框做同样的事情, I'm很懒得写每一个事件,每一个文本框
那么,有没有什么办法如何为所有的文本框由一个事件?

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?

有一个短代码:

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;
}

感谢您! :)

推荐答案

附加相同的处理所有的文本框,并使用发件人参数拿到这引起了事件的文本框的实例:

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;
}

这篇关于所有文本框的事件之一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆