关键新闻事件如何运作? [英] How the key press event works ?

查看:108
本文介绍了关键新闻事件如何运作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我正在使用c#windows窗体。如果我填写了所有文本框,那么我将按F4键将数据保存在db中。现在我想知道关键新闻事件的编码。请给我一些建议。







谢谢。



我尝试了什么:



我不知道按键事件。

解决方案

参见 Control.KeyPress事件(System.Windows.Forms) [ ^ ]和键盘输入如何工作 [ ^ ]。


KeyPress事件的问题在于它发生在每个键上,并且它是定向的在具有输入焦点的控件 - 这意味着当前接受打字的文本框,tha这是触发KeyPress事件的文本框 - 你通常不会在Form上获得它,这正是你想要的。并且KeyPress根本没有为F4解雇,虽然KeyDown和KeyUp是,但KeyPress只针对可打印字符被触发。



这是一种无法使用的方法 - Windows中的正常操作是让ENTER接受表单数据,这很容易做到,无论如何它都内置在WinForms中。 F4很不寻常,可能会让用户感到困惑,所以我强烈建议你不要使用它。

你可以这样做 - 但是你必须覆盖表单的ProcessCmdKey函数,你可能会让你的用户讨厌这个软件!


在WinForms中有另一种方法可以做到这一点而不必覆盖'ProcessCommandKey,将Form'KeyPreView设置为'true等等;这有点hackish,但我希望它会在这里完成讨论。



1.在表格上放一个MenuStrip:如果你不想要要使用MenuStrip显示某些菜单项,请将其可见属性设置为false。



2.以常规方式将MenuStripMenuItem添加到MenuStrip。将其ShortCutKeys设置为您要触发事件的组合键。将其可见属性设置为false。



3.将Click EventHandler添加到ToolStripMenuItem,并编写在Click发生时希望执行的代码。



示例:



我添加一个MenuStrip,'menuStrip11。我添加了一个ToolStripMenuItem,'AltF4ShortCut,键组合设置为'Alt和'F4。在表单的Designer视图中,您将看到:

  //   AltF4ShortCut  
//
.AltF4ShortCut.Name = AltF4ShortCut;
this .AltF4ShortCut.ShortcutKeys =((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Alt | System.Windows。 Forms.Keys.F4)));
.AltF4ShortCut.Size = new System.Drawing.Size( 52 24 );
.AltF4ShortCut.Text = 保存;
.AltF4ShortCut.Visible = false ;
.AltF4ShortCut.Click + = new System.EventHandler( this .AltF4ShortCut_Click);

现在,无论Control在运行时表单中有什么焦点,Click EventHandler都会触发;我可以将当​​前焦点设置为UserControl中Panel中的TextBox,它仍然会触发。



以下是Click EventHandler的外观:

  private   void  AltF4ShortCut_Click( object  sender,EventArgs e)
{
if (MessageBox.Show(
立即保存?
保存数据
MessageBoxButtons.OKCancel,
MessageBoxIcon.Question,
MessageBoxDefaultButton.Button1,
MessageBoxOptions.DefaultDesktopOnly

== DialogResult.OK)
{
// 要保存的代码在这里
}
}

我认为可以安全地假设带有快捷键组合的菜单项的WinForm应用程序为Keyboard事件安装了自己的低杠杆处理程序,甚至可能使用ProcessCmdKey over-ride使用的任何内容(可能是WndProc? )。


Hi I'm working on c# windows form. If i filled all the text boxes then i'll press F4 key to save the data in db . Now i want to know the coding of key press event. Pls suggest me some ideas.



Thank You.

What I have tried:

I had no idea about key press event.

解决方案

See Control.KeyPress Event (System.Windows.Forms)[^] and How Keyboard Input Works[^].


The problem with the KeyPress event is that it happens for every key, and it is directed at the control which has the "input focus" - which means that whichever textbox current accepts typing, that's the textbox for which the KeyPress event is fired - you wouldn't normally get it at the Form, which is really what you want. And KeyPress isn't fired at all for F4, although KeyDown and KeyUp are, KeyPress is only fired for printable characters anyway.

That's an unusable way to do it - the normal action in Windows is for ENTER to accept form data, and that's easy to do, it's all built in to WinForms anyway. F4 is unusual, and likely to confuse users so I strongly suggest that you don't go with it.
You can do it - but you'll have to override the ProcessCmdKey function for the form, and you will probably make your users hate the software!


There's another way to do this in WinForms without your having to override 'ProcessCommandKey, set Form 'KeyPreView to 'true, etc.; It's a little "hackish," but I hope it will round-out the discussions here.

1. Put a MenuStrip on the Form: if you don't want to use the MenuStrip to show some Menu Items, set its 'Visible Property to 'false.

2. Add a ToolStripMenuItem to the MenuStrip in the usual way. Set its ShortCutKeys to the key combination you want to trigger an event. Set its 'Visible Property to 'false.

3. Add a Click EventHandler to the ToolStripMenuItem, and write the code you wish executed when the Click happens.

Example:

I add a MenuStrip, 'menuStrip11. I add a ToolStripMenuItem, 'AltF4ShortCut with the key-combination set to 'Alt and 'F4. In the Designer view of the Form, you'll see:

// AltF4ShortCut
// 
this.AltF4ShortCut.Name = "AltF4ShortCut";
this.AltF4ShortCut.ShortcutKeys = ((System.Windows.Forms.Keys)((System.Windows.Forms.Keys.Alt | System.Windows.Forms.Keys.F4)));
this.AltF4ShortCut.Size = new System.Drawing.Size(52, 24);
this.AltF4ShortCut.Text = "Save";
this.AltF4ShortCut.Visible = false;
this.AltF4ShortCut.Click += new System.EventHandler(this.AltF4ShortCut_Click);

Now, the Click EventHandler will fire no matter what Control has focus in the run-time Form; I could have the current focus set to a TextBox in a Panel in a UserControl, and it will still fire.

Here's what the Click EventHandler might look like:

private void AltF4ShortCut_Click(object sender, EventArgs e)
{
    if (MessageBox.Show(
            "Save now ?",
            "Save Data",
            MessageBoxButtons.OKCancel,
            MessageBoxIcon.Question,
            MessageBoxDefaultButton.Button1,
            MessageBoxOptions.DefaultDesktopOnly
        )
        == DialogResult.OK)
    {
        //  code to save goes here
    }
}

I think it's safe to assume that a WinForm app with menu item with shortcut key-combinations defined installs its own lower-lever handler for Keyboard events, perhaps even using whatever the ProcessCmdKey over-ride uses under-the-hood (maybe WndProc ?).


这篇关于关键新闻事件如何运作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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