Key processing in MDI child forms
-
Sunday, February 14, 2010 11:02 PM
Hello,
in my MDI application I add child forms to the main form like this:
FormWaveform formWaveform = new FormWaveform(...);
formWaveform.MdiParent = this;
formWaveform.Show();If the user presses a key, I want the active child form to react and I implemented the corresponding methods as follows in the child's form class:
void FormWaveform_KeyDown(object sender, KeyEventArgs e) {....}
and
protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {...}
My problem is, that only the child form, which was first added, processes the messages. All other forms don't.
In the main form, I added an event handler which handles the changing of the active child; here I tried to change the properties of the active child and the passive children: no success.
In every case, only the first added child processes the key input - even if this form is not the active MDI child. All other MDI children can not process any message.
Can anyone give me a hint?
Thanks in advance,
Markus
Answers
-
Tuesday, February 16, 2010 8:38 PM
Hello Kira,
thank you for you answer.
I did the same test with a MDI parent form. In this form I put two child forms like this:
ChildForm childForm1 = new ChildForm(); childForm1.Name = "childForm1"; childForm1.Text = "childForm1"; childForm1.MdiParent = this; childForm1.Show(); ChildForm childForm2 = new ChildForm(); childForm2.Name = "childForm2"; childForm2.Text = "childForm2"; childForm2.MdiParent = this; childForm2.Show();In class ChildForm I put
private void ChildForm_KeyDown(object sender, KeyEventArgs e) { MessageBox.Show(this.Name); }And the result was - as you said, and one could expect - the name of the active child form.
So I asked myself why I got that strange behaviour. In my child forms, I have a ToolStripContainer, in which I put an inherited control from class Control. This control is added to the ToolStripContainer like this:
toolStripContainer1.ContentPanel.Controls.Add(_waveControl);
And now, I found out, when I add in the child form and write in the child form's constructor
this.ActiveControl = _waveControl;
the behaviour will be as expected. Every active child window reacts.
Otherwise, not defining the active control and if this line is missing, I get the problematic behaviour: only the first added child form reacts.
So thank you for the answer! Perhaps, anyone knows, why this one line of code solves the problem.
Thank you!
Markus- Marked As Answer by Kira QianModerator Wednesday, February 17, 2010 2:04 AM
All Replies
-
Monday, February 15, 2010 2:40 AMModeratorHi Markus,
I did a test with the situation as you said. When I handled the KeyDown event of MDI parent form and set the KeyPreview property of the form to true, I can detect all key action no matter which form is activated. It seems not like you said only the first child can detect key action. My test environment is Win7 .NET 3.5. Did I have something different from yours?
Sincerely,
Kira Qian
MSDN Subscriber Support in Forum
If you have any feedback on our support, please contact msdnmg@microsoft.com
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! -
Tuesday, February 16, 2010 8:38 PM
Hello Kira,
thank you for you answer.
I did the same test with a MDI parent form. In this form I put two child forms like this:
ChildForm childForm1 = new ChildForm(); childForm1.Name = "childForm1"; childForm1.Text = "childForm1"; childForm1.MdiParent = this; childForm1.Show(); ChildForm childForm2 = new ChildForm(); childForm2.Name = "childForm2"; childForm2.Text = "childForm2"; childForm2.MdiParent = this; childForm2.Show();In class ChildForm I put
private void ChildForm_KeyDown(object sender, KeyEventArgs e) { MessageBox.Show(this.Name); }And the result was - as you said, and one could expect - the name of the active child form.
So I asked myself why I got that strange behaviour. In my child forms, I have a ToolStripContainer, in which I put an inherited control from class Control. This control is added to the ToolStripContainer like this:
toolStripContainer1.ContentPanel.Controls.Add(_waveControl);
And now, I found out, when I add in the child form and write in the child form's constructor
this.ActiveControl = _waveControl;
the behaviour will be as expected. Every active child window reacts.
Otherwise, not defining the active control and if this line is missing, I get the problematic behaviour: only the first added child form reacts.
So thank you for the answer! Perhaps, anyone knows, why this one line of code solves the problem.
Thank you!
Markus- Marked As Answer by Kira QianModerator Wednesday, February 17, 2010 2:04 AM

