简体   繁体   English

在按键触发时调整大小

[英]Resize on the key firing

I have a classic Form1 class that inherits the Form class.我有一个继承 Form 类的经典 Form1 类。 The problem is I press the buttons and no events get fired.问题是我按下按钮并没有触发任何事件。
Inside the Form1 there is a webBrowser which occupies the whole Form1.在 Form1 里面有一个 webBrowser,它占据了整个 Form1。

Here is my code:这是我的代码:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        WindowState = FormWindowState.Maximized;
        KeyPreview = true;
        PreviewKeyDown += wb_PreviewKeyDown; 
    }

     private void wb_PreviewKeyDown(object sender, PreviewKeyDownEventArgs e)
    {
        Console.WriteLine("EDOOOOOOOOO");   
        if (e.Control && e.KeyCode == Keys.C)
        {
            // Do something funny!
        }
    }
}

The wb_PreviewKeyDown is not fired whatever button I push.无论我按下什么按钮,都不会触发 wb_PreviewKeyDown。

If you press a key on the form, the event will get fired.如果您按下表单上的某个键,该事件将被触发。
However, if you have a TextBox or something on the form, and you press a key on that, the event will NOT fire, because now the TextBox has received the input.但是,如果您在表单上有一个 TextBox 或其他东西,并且您按下了一个键,该事件将不会触发,因为现在 TextBox 已收到输入。

This can easily be fixed by setting the property KeyPreview = true on the form.这可以通过在表单上设置属性KeyPreview = true来轻松解决。
You can set this in the designer, or in code (see the answer of @Isma)您可以在设计器或代码中进行设置(请参阅@Isma 的答案)

EDIT: Since you have a WebBrowser control on your form, you need to do it somewhat different, see this Handling key events on WebBrowser control编辑:由于您的表单上有一个WebBrowser控件,您需要做一些不同的事情,请参阅此处理 WebBrowser 控件上的关键事件

It involves using the PreviewKeyDown event of the WebBrowser它涉及使用WebBrowserPreviewKeyDown事件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM