I am having a weird behaviour in a MDI form when the enter key is pressed in one of the forms when a Textbox is focused.
Basically, the parent form somehow sends the key to the other child MDI form, instead of sending it to the form where the TextBox in which the enter key was pressed.
To Debug, I set the KeyPreview to true on every form (the parent and the 2 childs) and begun to listen to the four key events (Preview, Up, Down, Press) and the behaviour of this is different if a normal key is pressed and if enter is in the problematic Textboxes.
C1 <- MDI Child with the TextBox; C2 <- Other MDI Child; P <- MDI Parent
If any other key is pressed: C1.KeyDown -> P.KeyDown -> C1.KeyPress -> P.KeyPress -> C1.KeyUp -> P.KeyUp As a result, the text appears in the TextBox of C1 where it is supposed to be.
If Enter in pressed: C2.KeyUp -> P.KeyUp As a result, the C2 is Focused.
WHY?!?!?!?!?!? :P
As a desperate attempt, I overrode the ProcessCmdKey in every form to discover what is going on, but the enter key press don't even pass by there.
I don't know if it is important, but this is the code I used to instanciate the C2 form (which is made before C1) and the code to instanciate the C1...
NOTE: C1 form is instanciated and shown by the parent form as a response to a custom event fired by the C2 form...
C2:
private void CalendarForm_Load(object sender, EventArgs e)
{
// Loop through all of the form's controls looking
// for the control of type MdiClient.
foreach (Control ctl in this.Controls)
{
if (ctl is MdiClient)
{
// Set the BackColor of the MdiClient control.
((MdiClient)ctl).BackColor = this.BackColor;
}
}
// Shows the background form
this._calendarContents.MdiParent = this;
this._calendarContents.Show();
//this._calendarContents.Dock = DockStyle.Fill;
}
C1:
private FloatingEventDetails _floatingEvent = null;
private void _calendarContents_ElementDoubleClicked(object sender, ElementDoubleClickedEventArgs e)
{
// Checks if the form is not open
if (this._floatingEvent == null)
{
// Opens the form
this._floatingEvent = new FloatingEventDetails();
this._floatingEvent.ModuleForm = this;
this._floatingEvent.ListOfImages = this.ElementTypeImageList;
this._floatingEvent.MdiParent = this;
// Begins to listen for Focus and LostFocus events
this._floatingEvent.GotFocus += new EventHandler(_floatingEvent_GotFocus);
this._floatingEvent.LostFocus += new EventHandler(_floatingEvent_LostFocus);
}
// Displays the form
this._floatingEvent.Show();
this._floatingEvent.BringToFront();
this._floatingEvent.Focus();
// Loads the Event in the details form
this._floatingEvent.EventId = e.EventId;
}