1
votes

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;
    }
1
is your text box multiline...Shekhar_Pro
I have more than one TextBoxes (one of them being MultiLine) and they are all presenting the same problem. I really want to capture the enter key event...Scudelari
Besides, the problem is really that the key is given in the keypreview to the other form...Scudelari
Anyone? Please, I really need this...Scudelari
Create a small project that exhibits this behavior and post it to a file sharing service or paste-bin.Hans Passant

1 Answers

0
votes

This problem is really whatever. The application is really complex, since the forms are loaded in a different application domain and it uses a custom DLL that performs async connections to the database and handles several things in the forms.

Regardless, the second form was sort of a "Dialog Box" that I wanted to have floating above the MDI form. But now, this "Dialog Form" was set not to use the MDI anymore and thus became a fully separated form. This way, it works....