![](/img/trans.png)
[英]WPF: Get all children of a window and assign the same KeyDown event to them
[英]Assign Keydown event to all textBoxes
我試圖以一種形式將keyDown事件分配給所有textBox。
到目前為止,我的代碼:
void listenTextBox_KeyDownEvent(Control control)
{
foreach (Control ctrl in control.Controls)
{
if (ctrl is TextBox)
{
TextBox tb = (TextBox)ctrl;
tb.KeyDown += new EventHandler(textBox_KeyDown);
}
else
{
listenTextBox_KeyDownEvent(ctrl);
}
}
}
void textBox_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
TextBox tb = (TextBox)sender;
MessageBox.Show("Great Enter was hit");
}
}
但是我正在運行一個我不理解的錯誤:
'textBox_KeyDown'的重載不匹配委托'EventHandler'
有什么建議嗎?
嘗試改變
tb.KeyDown += new EventHandler(textBox_KeyDown);
對於
tb.KeyDown += new KeyEventHandler(textBox_KeyDown);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.