MDI child forms

S

Steve Barnett

I've created a simple MDI application and have designated the Window menu to
keep track of the mdi children. When I first load an mdi child, it's caption
consists of "File: no file loaded" and this is what appears on the Window
menu.

Now, after the mdi child loads, I call a method to load a specifically named
file. At this point, the child changes it's caption to reflect the new file
name. Unfortunately, the Window menu does not update and remains fixed on
"File: no file loaded".

Is there some trick I'm missing to ensure that the Window menu gets updated
to reflect the new child window caption?

Incidentally, I've found that this problem does not happen in the
development environment, but always happens on the compiled application.
Opening a second child window usually sorts the problem out.

I've tried invalidating the Window menu item and tried refreshing the
MenuStrip, but without any success.

Thanks
Steve
 
R

Robbe Morris [C# MVP]

There should be a .Parent or .MDIParent property on your
child form. Set its caption to your value.

myform.MDIParent.Caption = "blah"
 
S

Steve Barnett

Sorry, not sure how this helps. It's not the caption of the parent window
that's wrong, it's the name that appears on the WindowList menu. Even though
I have a file loaded and the child window caption has been changed, when I
open the Window menu, the WindowList shows the "original" caption that the
child window had before I changed it.

Steve
 
S

Steve Barnett

Just in case anyone else struggles with this, I failed miserably to get the
window list managed for me automatically. In the end, I added a
DropDownOpening event handler in my MDI form load event:

MainMenu.MdiWindowListItem.DropDownOpening += new
EventHandler(this.UpdateWindowMenuList);

This causes the UpdateWindowMenuList method to run when I select the menu
assigned to contain the window list:

private void UpdateWindowMenuList(object sender, EventArgs e)
{
// The code to update the window list on the Window menu does not
appear
// to work - so I've cobbled this together.
Application.DoEvents();

foreach (ToolStripItem tsMenu in
MainMenu.MdiWindowListItem.DropDown.Items)
{
if (tsMenu is ToolStripMenuItem)
{
ToolStripMenuItem tsMenuItem = (ToolStripMenuItem)tsMenu;
if (tsMenuItem.IsMdiWindowListEntry)
{
// Work out the index of this form.
int iIndex = Int32.Parse(tsMenu.Text.Substring(1, 1)) - 1;

// Update the menu
tsMenuItem.Text = String.Format("&{0} {1}", iIndex + 1,
this.MdiChildren[iIndex].Text);
}
}
}
}

In this routine, I go look for each MDI child and adjust the caption in the
WindowList menu items.

It's probably not the best solution, but it seems to be working for me.

Steve
 
G

Guest

Steve,

Ran into the same problem. Tried about everything and in the end used your
solution. I've modified the code such that there won't be a problem when
there are more than nine open mdichildren.

here it is:

Private Sub mnuDocuments_DropDownOpening(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles mnuDocuments.DropDownOpening

Dim Idx As Integer = -1

For Each M As ToolStripItem In
MainMenu.MdiWindowListItem.DropDown.Items
If TypeOf M Is ToolStripMenuItem Then
With CType(M, ToolStripMenuItem)
If .IsMdiWindowListEntry Then
Idx += 1
.Text = (Idx + 1).ToString("&0") & " " &
MdiChildren(Idx).Text
End If
End With
End If
Next

End Sub

I've also left out the Appication.DoEvents stuff. Seems to work without it.

Greetings.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top