【问题标题】:Assign Keydown event to all textBoxes将 Keydown 事件分配给所有文本框
【发布时间】:2016-11-03 20:35:38
【问题描述】:

我正在尝试以一种形式将 keyDown 事件分配给所有文本框。

到目前为止我的代码:

        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'

有什么建议吗?

【问题讨论】:

  • 好吧发现我的错误必须将EventHandler更改为KeyEventHandler

标签: c# events


【解决方案1】:

尝试改变

tb.KeyDown += new EventHandler(textBox_KeyDown);

tb.KeyDown += new KeyEventHandler(textBox_KeyDown);

【讨论】:

  • 这就是我评论的内容 ;) 但是感谢让未来的访问者思考清楚。所以+1
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-23
  • 2020-04-17
  • 1970-01-01
相关资源
最近更新 更多