简体   繁体   English

在表单加载时调用 KeyDown 事件

[英]Calling a KeyDown Event on Form Load

I would want to launch a KeyDown Event on Form_Load however its taking me somewhere else in the Form_Load event.我想在Form_Load上启动KeyDown事件,但它会将我带到Form_Load事件中的其他地方。

Form_Load: Form_Load:

int static_int = 0;
private void Form1_Load(object sender, EventArgs e)
{
    if(condition == true)
    {
        txtInput.Text = "something";
        txtInput.Focus();
        SendKeys.Send("{Enter}");
        int somegeneratednubmer = 20;
        static_int = static_int + somegeneratednumber;
        //somemore code here
    }

}

KeyDown:键下:

private void txtInput_KeyDown(object sender, KeyEventArgs e)
{
  if(e.KeyCode == Keys.Enter)
  {
      static_int = 10;
      //somemore codes here too
  }

I would like to get the SUM of static_int and somegeneratednumber which is 30. However, after Debugging, I'm getting its initialized value of 0. From what I understood, after SendKeys.Send("{Enter}") the KeyDown event should proceed.static_intsomegeneratednumber的 SUM,它是 30。但是,在调试之后,我得到了它的初始化值 0。据我somegeneratednumber ,在SendKeys.Send("{Enter}")KeyDown事件应该继续。

Why is it not??为什么不是??
How would I get the correct result?我将如何得到正确的结果? I really should do the KeyDown event on Form_Load , a conditional event...我真的应该在Form_Load上执行KeyDown事件,这是一个条件事件......
or What am I doing wrong here?或者我在这里做错了什么?
Note: originally static_int is initialized on a Class注意:原来 static_int 是在一个类上初始化的

No, the KeyDown even will proceed at the earliest possible moment, which is when the appropriate message is executed from the form's message queue.不, KeyDown甚至会在最早的时刻进行,也就是从表单的消息队列中执行适当的消息时。 That cannot happen before the Load event finishes, because that also on the message queue.这不会在Load事件完成之前发生,因为这也在消息队列中。 Even if that weren't the case, SendKeys doesn't wait for the action to be processed.即使情况并非如此, SendKeys也不会等待处理操作。 It just sends the message and returns immediately.它只是发送消息并立即返回。

Another problem is that SendKeys sends the virtual keys to the currently active window.另一个问题是SendKeys将虚拟键发送到当前活动窗口。 That can never be your window, since your window isn't even shown yet!那永远不可能是您的窗口,因为您的窗口甚至还没有显示! When something behaves weird, a good first step is to read the documentation.当某些事情表现得很奇怪时,一个好的第一步是阅读文档。

So, why is the value of static_int zero, instead of 20 or 30 ?那么,为什么static_int的值为零,而不是2030 Well, the likeliest case is an unhandled exception, and I'm pretty sure that's exactly what happens when you do tbxInput.Focus .好吧,最可能的情况是未处理的异常,我很确定这正是您执行tbxInput.Focus时发生的情况。 The control doesn't quite exist yet, and it can't be made the input focus.该控件尚不存在,无法将其设为输入焦点。 If you have trouble understanding all this, you might want to find some book on the basics of how Windows windows work - there's nothing .NET can do about it, and it's places like this where the (very pretty) .NET abstraction leaks a lot.如果您无法理解所有这些,您可能想找一些关于 Windows 窗口工作原理的书 - .NET 对此无能为力,并且在这样的地方(非常漂亮的).NET 抽象泄漏了很多. If you're planning to do any Windows UI development, you really need to know at least the basics.如果您打算进行任何 Windows UI 开发,您确实需要至少了解基础知识。

However, that's completely unnecessary anyway.然而,无论如何这完全没有必要。 You don't have to execute a KeyDown event.您不必执行KeyDown事件。 Just make a method that's called from both the Load event handler and the KeyDown event handler.只需创建一个从Load事件处理程序和KeyDown事件处理程序中调用的方法。

try adding this event instead Form1 isn't loaded yet so no events yet.尝试添加此事件,而不是 Form1 尚未加载,因此尚无事件。

private void Form1_Shown(Object sender, EventArgs e) 
{
   SendKeys.Send("{Enter}");
}

But truly this design is wrong但这个设计确实是错误的

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

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