tarik
tarik

Reputation: 11

MDI child form does not activate when clicked in client area

My VB.NET app supports several kinds of MDI child forms. Some kinds, but not others, are 'troublesome' -- they cause the focus mechanism to become weird. Once a 'troublesome' child form has been opened, NONE of the MDI child forms will become activated unless I click on either the title bar or the border. Clicking in the client area does not activate the child form -- its title bar remains gray, and it does not receive Activate or GotFocus events. Strangely, however, the clicked-on form actually DOES receive the focus, because its controls receive mouse and key events. Only the Z-Order and the highlighting do not change.

Once this problem develops, it persists even after the 'troublesome' form is closed, and it affects child forms which are opened afterward. However, if ALL of the child forms are closed, the problem clears up, and new child forms behave normally -- until the next 'troublesome' form appears.

I have no idea why one kind of child form is troublesome, and others are not.

Any suggestions would be greatly appreciated.

Upvotes: 1

Views: 2553

Answers (1)

Scott Numbers
Scott Numbers

Reputation: 11

I am a little late getting into this but I was experiencing the same symptoms described above. I too had a few forms that caused this issue and others that didn't.

I found a link that explains the problem and applied the concept to my forms.

http://www.pcreview.co.uk/forums/mdi-child-forms-functionality-does-not-work-correctly-windows-f-t2894221.html

It basically has to do with making sure that the forms MdiParent is set before any code that causes the form's window to be created (the API call to CreateWindow() or CreateWindoEX()). Anyway I found a few of my windows had code in the Constructors that caused the API window to be created. An example of this was a form with a WebBrowser control on it. I passed a URL into the form on the Constructor which in turned passed it into the WebBrowser.Navigate method. In this scenario, my MdiChild windows would not activate as expected. I changed the Constructor such that it saved the URL and also overrode the form's CreateHandle method to actually set the URL in the WebBrowser at the earliest possible time. The forms now activate as expected.

  private string _URL = "";

  public frmReportServer(String URL, String Title) : this() {
     _URL = URL;
     this.Text = Title;
  }

  protected override void CreateHandle() {
     base.CreateHandle();
     if (_URL != "") wbReports.Navigate(URL, false);
  }

Hope this helps...

Upvotes: 1

Related Questions