C# 一个事件用于所有文本框
我正在用WPF C编写简单的程序,我有许多C# 一个事件用于所有文本框,c#,wpf,events,textbox,C#,Wpf,Events,Textbox,我正在用WPF C编写简单的程序,我有许多文本框——每个文本框都做同样的事情,我非常懒得为每个文本框编写每个事件。 那么,有没有办法通过一个事件为所有TextBox提供服务 有一个简短的代码: private void OnMouseLeft(object sender, MouseButtonEventArgs e) { TextBox1.Text = string.Empty; TextBox1.Foreground = Brushes.Black; } private vo
文本框
——每个文本框
都做同样的事情,我非常懒得为每个文本框
编写每个事件。
那么,有没有办法通过一个事件为所有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;
}
谢谢!:) 将相同的处理程序附加到所有文本框,并使用
发送方
参数获取引发事件的文本框实例:
private void OnMouseLeft(object sender, MouseButtonEventArgs e)
{
TextBox textBox = (TextBox)sender;
textBox.Text = String.Empty;
textBox.Foreground = Brushes.Black;
}
“sender”参数将是文本框本身。所以只需编写一个函数并将它们全部附加到该函数
private void OnMouseLeft(object sender, MouseButtonEventArgs e)
{
var textBox = (TextBox)sender;
textBox.Text = string.Empty;
textBox.Foreground = Brushes.Black;
}
可以将多个事件分配给同一事件处理程序。这些事件可以来自同一个控件和/或不同的控件
TextBox t = new TextBox();
t.MouseLeftButtonUp += new MouseButtonEventHandler(OnMouseLeft);
t.MouseLeftButtonDown += new MouseButtonEventHandler(OnMouseLeft);
TextBox t2 = new TextBox();
t2.MouseLeftButtonUp += new MouseButtonEventHandler(OnMouseLeft);
然后,您只需通过强制转换发送者来处理哪个文本框
((文本框)发送者)。属性=值 将每个taxBox添加到同一个方法中,然后单击文本框,如图所示,我没有这样做,但它应该可以工作,或者至少让您朝着正确的方向前进。我希望这会有帮助
textBox.MouseClick += new MouseEventHandler(textBox_MouseClick);
private void textBox_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
TextBox textBox = sender as TextBox;
textBox.Text = string.Empty;
textBox.Forground = Brushes.Black;
}
}
您可以使用sender对所有文本框不允许使用数字值的文本尝试此操作
$('input[type=text]')。向下键(函数(e){
if(e.shiftKey | | e.ctrlKey | | e.altKey){
e、 预防默认值();
}否则{
var key=e.keyCode;
如果(!((键==8)| |(键==32)| |(键==46)| |(键>=35&&key=65&&key
textBox.MouseClick += new MouseEventHandler(textBox_MouseClick);
private void textBox_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
TextBox textBox = sender as TextBox;
textBox.Text = string.Empty;
textBox.Forground = Brushes.Black;
}
}
TextBox T = (TextBox)sender;