[英]One Event for all TextBoxes
I'm在WPF C#做簡單的計划,我有很多TextBoxes
-每個TextBox
做同樣的事情,I'm很懶得寫每一個事件,每一個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;
}
private void OnMouseLeft(object sender, MouseButtonEventArgs e)
{
(sender as TextBox).Text = string.Empty;
(sender as TextBox).Foreground = Brushes.Black;
}
'sender'參數將是TextBox本身。 所以只需編寫一個函數並將它們全部附加到該函數。
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);
然后你只需通過強制轉發發件人來處理哪個文本框。
((TextBox)sender).Property = value;
將每個taxBox添加到同一個方法,然后單擊如圖所示單擊TextBox,我沒有這個,但它應該工作或至少讓你朝着正確的方向前進。 我跳它有幫助。
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;
}
}
嘗試這個所有文本框不允許數值僅文本..
$('input[type=text]') .keydown(function (e) {
if (e.shiftKey || e.ctrlKey || e.altKey) { e.preventDefault(); } else { var key = e.keyCode; if (!((key == 8) || (key == 32) || (key == 46) || (key >= 35 && key <= 40) || (key >= 65 && key <= 90))) { e.preventDefault(); } } });
TextBox T = (TextBox)sender;
您可以使用發件人
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.