处理未处理的回车键c#

发布于 2024-09-25 18:32:53 字数 404 浏览 0 评论 0原文

我必须在 win 表单上处理 Enter(以及其他键),而不会产生错误声音,但前提是当前活动控件尚未处理它。

因此,当在 TextBox 或 DateTimePicker 中按下 Enter 时,我想用表单处理它(没有错误声音),但是如果按下它,例如在 DataGridView 中,我希望它按照 DataGridView 默认情况下的方式处理。

OnKeyUp 解决了我的问题,仅处理未处理的击键 (e.Handled) 和 ProcessCmdKey (这个)解决了声音问题,但两者都不能解决。

有什么建议吗?

I have to process enter (among other keys) on win form without it producing error sound, but only if the currently active control didn't process it already.

So, when enter is pressed while in a TextBox or DateTimePicker, i want to process it with a form (without error sound), but if it is pressed, for example, in DataGridView i want it to be handled the way DataGridView does by default.

OnKeyUp solves my problem with handling only unhandled keystrokes (e.Handled) and ProcessCmdKey (this) solves sound problem, but neither solves both.

Any suggestions?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

〗斷ホ乔殘χμё〖 2024-10-02 18:32:53

感谢这个非常有趣的问题。不幸的是,除了在主窗体上重写 ProcessCmdKey 之外,我似乎无法找到所有按键的全局事件处理程序 本文。此方法的唯一问题是传递到事件处理程序委托的参数没有定义哪个控件正在创建事件:(

因此,我唯一的想法是您需要将事件处理程序分配给应用程序中的每个控件。我我已经编写了一些代码,可以帮助您展示如何执行此操作,但我不确定为页面上的每个控件分配 KeyPress 事件处理程序可能会产生什么不利影响,但这是我看到的唯一可行的解​​决方案

。 :

private void Form1_Load(object sender, EventArgs e)
{
    AssignHandler(this);
}

protected void HandleKeyPress(object sender, KeyPressEventArgs e)
{
    if (e.KeyChar == (char)Keys.Enter && (sender != this.textBoxToIgnore || sender ! this.gridViewToIgnore))
    {
        PlaySound();  // your error sound function
        e.Handled = true;
    }
}

public void AssignHandler(Control c)
{
    c.KeyPress += new KeyPressEventHandler(HandleKeyPress);
    foreach (Control child in c.Controls)
    {
        AssignHandler(child);
    }
}

Kudos for the very interesting question. Unfortunately, I can't seem to find a global event handler for all key presses other than overriding ProcessCmdKey on the main form per this article. Only issue with this method is that the arguments passed into the event handler delegate don't define what control is creating the event :(

So, my only thought is that you need to assign your event handler to every single control in the application. I've written some code that should help show you how to do this. I'm not sure what the adverse effects may be of assigning a KeyPress event handler to every control on your page though, but it's the only feasible solution I see.

Code:

private void Form1_Load(object sender, EventArgs e)
{
    AssignHandler(this);
}

protected void HandleKeyPress(object sender, KeyPressEventArgs e)
{
    if (e.KeyChar == (char)Keys.Enter && (sender != this.textBoxToIgnore || sender ! this.gridViewToIgnore))
    {
        PlaySound();  // your error sound function
        e.Handled = true;
    }
}

public void AssignHandler(Control c)
{
    c.KeyPress += new KeyPressEventHandler(HandleKeyPress);
    foreach (Control child in c.Controls)
    {
        AssignHandler(child);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文