Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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:
C#
private void MainForm_KeyUp(object sender, KeyEventArgs e)
       {
          MessageBox.Show(e.KeyCode.ToString());
          if (e.KeyData == Keys.Enter)
          {
              MessageBox.Show("End");
          }
}

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
Posted
Updated 20-Apr-15 3:39am
v2
Comments
nav571 20-Apr-15 9:14am    
No one? :(
Sergey Alexandrovich Kryukov 20-Apr-15 9:46am    
What do you mean by "use" in "Frm2 can use in MainForm_KeyUp".
—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
 
Share this answer
 
v2
Comments
nav571 20-Apr-15 10:29am    
Thanks for your attention...
I read your words carefully, but perhaps I should explain a little more:
The listener of the mainForm ("mainForm_KeyUp")actives also when frm2 is shown (There is debug breakPoint :) ), my "little" problem is that "KeyEventArgs e" paramter contains enter and not 12345+enter (my magnetic card insert the "12345" numbers, every number is like a new KeyEventArgs, In the end, KeyEventArgs =enter)
Sergey Alexandrovich Kryukov 20-Apr-15 11:11am    
It is not supposed to "contain" 12345+Enter. It just makes no sense. You really need to explain your ultimate goals.
—SA
Hi,

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

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


And in child form you can access this method.

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

            parentForm = (Form1) this.Owner;

            parentForm.Form1_KeyUp(sender, e);

}
 
Share this answer
 
Comments
nav571 20-Apr-15 10:10am    
so, Form1_KeyUp() must be a static method?
Dusara Maulik 20-Apr-15 10:20am    
nop

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900