表单如何在父表单的KeyUp事件中使用? [英] How a form can use in KeyUp event of the parent form?

查看:62
本文介绍了表单如何在父表单的KeyUp事件中使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好!

我有一个问题,首先,我有一个父表单(mainForm),并且在这里有一个KeyUp事件(private void MainForm_KeyUp(object sender,KeyEventArgs e))



其次,我的子格式(Frm2)比openDialog命令打开,

**现在我需要Frm2可以在MainForm_KeyUp中使用。**

真的,Frm2来到MainForm_KeyUp函数,但KeyEventArgs e = Keys.Enter

而不是我所有的数字。

评论:KeyEventArgs由磁卡读卡器填充。

这是代码:

hello!
I have a problem,First, I have a parent form ("mainForm") and in this there is a KeyUp event(private void MainForm_KeyUp(object sender, KeyEventArgs e))

Second, I have child form(Frm2) than opened by openDialog command,
**Now I need that Frm2 can use in MainForm_KeyUp.**
Really, Frm2 comes to MainForm_KeyUp function, but KeyEventArgs e = Keys.Enter
and not all my numbers.
A comment: the KeyEventArgs Filling up by magnetic Card reader.
This is the code:

private void MainForm_KeyUp(object sender, KeyEventArgs e)
       {
          MessageBox.Show(e.KeyCode.ToString());
          if (e.KeyData == Keys.Enter)
          {
              MessageBox.Show("End");
          }
}



当我从mainForm中读取读卡器时,我得到:12345END,但来自Frm2,我得到结束。为什么KeyEventArgs参数没有更多的键?我也必须从frm2获得12345END





我非常感谢您的试用帮助!!!

Soh


Wheמ I swip the card in the reader from the mainForm, I get : "12345END", but from the Frm2, I get "END". why the KeyEventArgs parameter not have more keys? I have to get "12345END" also from frm2


I'm really thankful for your trial to help!!!
Soh

推荐答案

首先,这里没有任何表格是儿童,而且您的主表单不是您的其他表格的父表格。 />


如果您调用 System.Windows.Forms.Form.ShowDialog ,您的表单将显示在模式中状态,所以用户应该只在这个应用程序中使用这个表单(所以,它不是 system-modal ,你可以切换到其他应用程序,激活他们的窗口),不能调用其他形式的事件。



请参阅: https://msdn.microsoft.com/en-us/library/system.windows.forms.form.showdialog(v = vs.110)的.aspx [ ^ ]。



目前尚不清楚它是什么以及为什么Frm2来到MainForm_KeyUp。这是它的样子。您确实提供了任何证据,MainForm_KeyUp 是一个事件处理程序添加到任何的 KeyUp 事件实例的调用列表中形式。这只是方法名称的建议,因为它看起来像是从设计器中获得的自动生成的代码。它本身并没有说什么;使用+ = event运算符完成向调用列表添加委托。永久使用自动生成的方法名称是违反良好的编程风格,因为这样的名称违反(良好的)Microsoft命名约定。



还有另一个处理键盘事件的问题在窗体上:您可能有一些控件可以获得键盘焦点,因此不会将 KeyDown 事件调度到窗体本身。如果您需要为属性 Form.KeyPreview 指定true并处理事件 PreviewKeyDown 或覆盖方法 OnPreviewKeyDown

https://msdn.microsoft.com/en-us/library/system.windows.forms.control.previewkeydown(v = vs.110)的.aspx [< a href =https://msdn.microsoft.com/en-us/library/system.windows.forms.control.previewkeydown(v=vs.110).aspx\"target =_ blanktitle =New Window > ^ ]。



https://msdn.microsoft.com/en-us/library/system.windows.forms.control.previewkeydown(v = vs.110)的.aspx [ ^ ],

https://msdn.microsoft.com/en-us/library/system.windows.forms.control.onpreviewkeydown(v=vs .110).aspx [ ^ ]。



我怀疑处理 KeyUp 并且检查Enter具有任何实际意义。



问题不够明确;你需要先解释你正在做的目的;所以,如果你进一步澄清这个问题,你可以得到更多的帮助。



-SA
First of all, none of the forms are children here, and your main form is not a parent of your other form.

If you call System.Windows.Forms.Form.ShowDialog, your form is shown in the modal state, so the user is supposed to work only with this form in this application (so, it is not system-modal, you can switch to other applications, activate their windows), cannot invoke events of other forms.

Please see: https://msdn.microsoft.com/en-us/library/system.windows.forms.form.showdialog(v=vs.110).aspx[^].

It is not clear what is it and why "Frm2 comes to MainForm_KeyUp". Here is how it look. You did provide any evidence that MainForm_KeyUp is an event handler added to the invocation list of the KeyUp event instance of any of the forms. This is just suggested by the name of the method, as it looks like an auto-generated code you got from the designer. It does not tell anything by itself; adding a delegate to an invocation list is done using += event operator. And using auto-generated names of methods permanently is against good programming style, as such names violate (good) Microsoft naming conventions.

There is another problem of handling keyboard events on the forms: you probably have some controls which grab keyboard focus, so the KeyDown event is not dispatched to the form itself. If you need assign true to the property Form.KeyPreview and handle the event PreviewKeyDown or override the method OnPreviewKeyDown:
https://msdn.microsoft.com/en-us/library/system.windows.forms.control.previewkeydown(v=vs.110).aspx[^].

https://msdn.microsoft.com/en-us/library/system.windows.forms.control.previewkeydown(v=vs.110).aspx[^],
https://msdn.microsoft.com/en-us/library/system.windows.forms.control.onpreviewkeydown(v=vs.110).aspx[^].

I doubt that handling KeyUp and checking up Enter makes any practical sense at all.

The question is not clear enough; you would need to start with explaining the purpose of what you are doing; so, you can get more help if you further clarify the question.

—SA






您需要将父级表格中的KeyUp事件设为私人。



Hi,

You need to set KeyUp event as private in parent form.

public void Form1_KeyUp(object sender, KeyEventArgs e)
{
      MessageBox.Show(e.KeyValue.ToString());
}





以儿童形式,您可以使用此方法。





And in child form you can access this method.

private void Form2_KeyUp(object sender, KeyEventArgs e)
{
            Form1 parentForm = new Form1();

            parentForm = (Form1) this.Owner;

            parentForm.Form1_KeyUp(sender, e);

}


这篇关于表单如何在父表单的KeyUp事件中使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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