【发布时间】:2014-12-08 02:47:13
【问题描述】:
我有两个文本框,每个都附加了自己的按键事件。
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
if(e.KeyChar == '\r')
{
e.Handled = true;
// some other stuff
}
}
private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
{
if(e.KeyChar == '\r')
{
e.Handled = true;
// some other stuff
}
}
有没有办法将按键事件动态连接到焦点文本框?
编辑:
类似:
void KeyPress(object sender, KeyPressEventArgs e)
{
foreach(Control c in this)
{
if(c == TextBox && c.Focused)
{
if(e.KeyChar == '\r')
{
// do something
}
}
}
}
【问题讨论】:
-
foreach(Control c in this) 永远不要那样做^^
-
Naaa,这只是伪代码^^
-
看我的回答,这就是办法
标签: c# focus controls keypress