热键避免离开事件c#win。形成 [英] Hotkeys avoiding leave event c# win. form

查看:79
本文介绍了热键避免离开事件c#win。形成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以告诉我为什么按钮的热键跳过文本框的离开事件



最近,我遇到了一个奇怪的问题,因为我的< b>当前焦点位于文本框,我的按钮保存的热键是Alt + S (通过将& Save 写入按钮中的文本创建的热键)。现在每当我按下 Alt + S 时,焦点就会按下按钮,但文本框的离开事件无法触发。任何人都可以给我一个解释,为什么会发生这种情况或者这是否会发生?



提前谢谢

Can somebody tell me why hot keys of button skip textbox's leave event.

Recently, i encountered a strange problem as my current focus was on textbox and my button's hot key of save was Alt+S (hotkey created by writting &Save as text in button). Now whenever i press Alt+S the focus goes on button but textbox's leave event fails to fire. Can anybody give me an explanation why this is happening or is this suppose to happen?

Thanks in advance

推荐答案

我很惊讶当WinForms中的TextBox具有焦点时,在Button上使用助记键组合会触发,因为它捕获鼠标,但是我并不感到惊讶不会因为使用助记符而感到沮丧的朦胧记忆而放弃'离开事件。



所以,几年前我放弃了使用记忆法在窗体上的WinForm控件或容器控件中(对于菜单它们看起来工作正常)。



我现在使用这种代码:
I'm surprised that using a mnemonic key-combination on a Button would fire when a TextBox in WinForms has focus, because it "captures" the mouse, however I'm not surprised it doesn't fire the 'Leave Event in the sense that ties in with dim memories of being frustrated using mnemonics.

So, a few years ago I gave up on using mnemonics on WinForm Controls on a Form, or in Container Controls (for Menus they seem to work fine).

I use this kind of code now:
// detect Alt-S
private Keys ShiftAltS = Keys.Shift | Keys.Alt | Keys.S;

// define in Form Scope
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
    if (keyData.HasFlag(ShiftAltS))
    {
        // test here to see if a particular Control
        // or Controls have the current Focus
        // and use a Switch/Case statement or whatever
        // to select which Control you want Focus to go to

        // here's a sample of what I would do to redirect Focus
        // from TextBox to Button:
        if(ActiveControl == YourTextBox)
        {
          YourButton.Focus();
          YourButton.Capture(); // optional: will have some side-effects
          return true;
        }
    }
    return base.ProcessCmdKey(ref msg, keyData);
}

实际上,我使用的是一个更高级的版本,我打算今年写一篇关于今年的文章或提示/技巧。但是,我希望这段代码能够证明一个有用的替代方案,如果你没有任何助记符。

Actually, I use a fancier version of this that I intend to write an article or tip/trick about this year, here. But, this code, I hope, demonstrates a useful alternative if you don't get anywhere with mnemonics.


这篇关于热键避免离开事件c#win。形成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆