Visual Studio Development Bookmark and Share   
 index > Visual C# Express Edition > Normal Focus behavior not working; Focus doesn't set to control's displayed Form after setting MdiParent
 

Normal Focus behavior not working; Focus doesn't set to control's displayed Form after setting MdiParent

I have a MdiParent Form with several MdiChild forms Shown.  First the child Form lets you pop the form out of the MdiContainer like this:
mParent = this.MdiParent;
MdiParent = null;

Everything seems fine.  Now I pop the form back in:

MdiParent = mParent;

Now focus stops working correctly for all the child Forms.   
I click on any Control in any Form, and it doesn't set Focus to it's own Form, Activate event isn't called and neither is MdiChildActivate.
Now you can still use the app by clicking the child Forms' Titlebar, but it bugs me,
where'd the focus wiring go?  Am I not supposed to be able to pop forms out?  Is there a better way?
Twitchers  Wednesday, October 14, 2009 4:11 PM
Hmya, this is certainly not a standard Windows MDI capability.  It happens to work because of the way Windows Forms deals with window style flags, it completely destroys the window and recreates it from scratch.  It is a frequent source of trouble.  Post a repro project that exhibits this problem to a file sharing service and I'll take a look at it.

Hans Passant.
nobugz  Wednesday, October 14, 2009 7:17 PM
Sorry I didn't find a place to post the code but
I found a workaround!  (Yay)  I'll ask our Microsoft guy later, since I keep thinking shouldn't be this hard, but for now this will have to do.
I fake the thing out by setting the MdiParent to another MdiContainer Form, which acts as the Child's border.
That way MdiParent = null never happens and no flags or event wiring are lost.
Here's the fixed up child form code:

 

public partial class ChildForm : Form {

 

  Form mParent;
  Form mFosterParent;

 

  public ChildForm() {
    InitializeComponent();
  }

 

  private void ChildForm_Load(object sender, EventArgs e) {
    mParent = this.MdiParent;
  }

 

  private void PopOut_Click(object sender, EventArgs e) {
    mFosterParent =
new MDIParent2();
    mFosterParent.Show();
    mFosterParent.Text =
this.Text;
    mFosterParent.Width =
this.Width + 4;
    mFosterParent.Height =
this.Height + 4;
    MdiParent = mFosterParent;
    this.Top = 0; this.Left = 0;
    this.FormBorderStyle = FormBorderStyle.None;
  }

 

  private void PopIn_Click(object sender, EventArgs e) {
    this.FormBorderStyle = FormBorderStyle.Sizable;
    this.WindowState = FormWindowState.Normal;
    MdiParent = mParent;
    MdiParent = mParent;
// Apparently I have to do this twice
    mFosterParent.Close();
    mFosterParent =
null;
  }
}

//I also had to modify the MdiContainer Resize event to make it look like one form

 

private void MDIParent2_Resize(object sender, EventArgs e) {
  if (MdiChildren.Length > 0) {
    this.MdiChildren[0].Height = this.Height - 38;
    this.MdiChildren[0].Width = this.Width - 12;
  }
}

Twitchers  Thursday, October 15, 2009 2:17 PM

You can use google to search for other answers

Custom Search

More Threads

• Problem getting the result of a group of 3 radioButtons
• converting an oledb to a string
• How to make ElapsedEventHandler public?
• HELLP!
• MDI child Form edit?
• Form inheritance
• Creat Thumbnail, and View Image on Click
• Compile Errors with strict.dtd?
• Applying multithread programming in windows form using c#.net (visual 2005)
• Displaying text as Marque