Vb[2008] Disabling menu items based on if MDI forms are open


R

Rob W

Greetings,

I have a MDI text editor with 5 menus File, Edit, Tools, Windows and Help

I have written code for every time the user hovers on one of the sub menus
Edit, Tools or Windows i.e. the ToolStripMenuItem if MDIChildren.count is
zero then loop around all the ToolStripMenuItem.dropdownlist items and sets
enabled to false.

I then thought it was very inefficient to check this every time the user
when to use a menu option.

I decided it would be best to ENABLE the menu options (Default on load is
menu items are disabled) when the first MDIchild form is created.

Then on the last MDIchild being closed to DISABLE the items.

I then looked at the _MdiChildActivate event which triggers every time a
MdiChild is open, closed or activated.

I can't however use a count or length to determine when the last MdiChild
has been close as the code is triggered before it closes down.

Any ideas how I can determine when the last MDIChild is being closed to
allow me to disable the menuitems ??

Thanks for those who helped me with OOP programming reading properties from
other classes.

Thanks
Rob
 
Ad

Advertisements

J

joecool1969

Greetings,

I have a MDI text editor with 5 menus  File, Edit, Tools, Windows and Help

I have written code for every time the user hovers on one of the sub menus
Edit, Tools or Windows i.e. the ToolStripMenuItem if MDIChildren.count is
zero then loop around all the  ToolStripMenuItem.dropdownlist items andsets
enabled to false.

I then thought it was very inefficient to check this every time the user
when to use a menu option.

I decided it would be best to ENABLE the menu options (Default on load is
menu items are disabled) when the first MDIchild form is created.

Then on the last MDIchild being closed to DISABLE the items.

I then looked at the _MdiChildActivate event which triggers every time a
MdiChild is open, closed or activated.

I can't however use a count or length to determine when the last MdiChild
has been close as the code is triggered before it closes down.

Any ideas how I can determine when the last MDIChild is being closed to
allow me to disable the menuitems ??

Thanks for those who helped me with OOP programming reading properties from
other classes.

I accomplished this by setting the enabled property for the menu items
that expect an active mdi child to be false. Then in the child form:

Private Sub DTCDesignChildForm_Activated(ByVal sender As Object,
ByVal e As System.EventArgs) Handles Me.Activated

Dim myParent As FormMain = Me.MdiParent

If myParent.MdiChildren.Length = 1 Then
myParent.ToolStripSaveButton.Visible = True
myParent.ToolStripButtonExit.Visible = True
myParent.ValidateDTCToolStripMenuItem.Enabled = True
myParent.TestBackfillToolStripMenuItem.Enabled = True
End If
Me.WindowState = FormWindowState.Maximized

End Sub

and:

Private Sub DTCDesignChildForm_FormClosing(ByVal sender As Object,
ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles
Me.FormClosing

Dim myParent As FormMain = Me.MdiParent

If myParent.MdiChildren.Length = 1 Then
myParent.ToolStripSaveButton.Visible = False
myParent.ToolStripButtonExit.Visible = False
myParent.ValidateDTCToolStripMenuItem.Enabled = False
myParent.TestBackfillToolStripMenuItem.Enabled = False
End If

End Sub

I went through the apparent thing you are going through, that is
developing your first complex .NET MDI application, a few years ago,
and remember figuring out the same things you have been asking about.
That's why I have been ressponding to many of your questions.

One thing I found that you may not have found yet us this event
handler you're going to want to put in the main parent form:

Private Sub WindowsToolStripMenuItem_DropDownOpening(ByVal sender
As Object, ByVal e As System.EventArgs) Handles
WindowsToolStripMenuItem.DropDownOpening

Dim i As Integer = -1
Dim myItem As ToolStripItem

For Each myItem In MenuStrip.MdiWindowListItem.DropDown.Items
If TypeOf myItem Is ToolStripMenuItem Then
With CType(myItem, ToolStripMenuItem)
If .IsMdiWindowListEntry Then
i += 1
.Text = (i + 1).ToString("&0") & " " &
MdiChildren(i).Text
End If
End With
End If
Next

End Sub

Private Sub WindowsToolStripMenuItem_DropDownOpening(ByVal sender
As Object, ByVal e As System.EventArgs) Handles
WindowsToolStripMenuItem.DropDownOpening

Dim i As Integer = -1
Dim myItem As ToolStripItem

For Each myItem In MenuStrip.MdiWindowListItem.DropDown.Items
If TypeOf myItem Is ToolStripMenuItem Then
With CType(myItem, ToolStripMenuItem)
If .IsMdiWindowListEntry Then
i += 1
.Text = (i + 1).ToString("&0") & " " &
MdiChildren(i).Text
End If
End With
End If
Next

End Sub

WindowsToolStripMenuItem is the main parent form's MdiWindowListItem
in my app.
 
Ad

Advertisements

R

Rob W

Thanks progressing nicely now :)

Interesting to investigate when to best access elements from which form
(Parent or child).
Enjoying myself :)

Greetings,

I have a MDI text editor with 5 menus File, Edit, Tools, Windows and Help

I have written code for every time the user hovers on one of the sub menus
Edit, Tools or Windows i.e. the ToolStripMenuItem if MDIChildren.count is
zero then loop around all the ToolStripMenuItem.dropdownlist items and
sets
enabled to false.

I then thought it was very inefficient to check this every time the user
when to use a menu option.

I decided it would be best to ENABLE the menu options (Default on load is
menu items are disabled) when the first MDIchild form is created.

Then on the last MDIchild being closed to DISABLE the items.

I then looked at the _MdiChildActivate event which triggers every time a
MdiChild is open, closed or activated.

I can't however use a count or length to determine when the last MdiChild
has been close as the code is triggered before it closes down.

Any ideas how I can determine when the last MDIChild is being closed to
allow me to disable the menuitems ??

Thanks for those who helped me with OOP programming reading properties
from
other classes.

I accomplished this by setting the enabled property for the menu items
that expect an active mdi child to be false. Then in the child form:

Private Sub DTCDesignChildForm_Activated(ByVal sender As Object,
ByVal e As System.EventArgs) Handles Me.Activated

Dim myParent As FormMain = Me.MdiParent

If myParent.MdiChildren.Length = 1 Then
myParent.ToolStripSaveButton.Visible = True
myParent.ToolStripButtonExit.Visible = True
myParent.ValidateDTCToolStripMenuItem.Enabled = True
myParent.TestBackfillToolStripMenuItem.Enabled = True
End If
Me.WindowState = FormWindowState.Maximized

End Sub

and:

Private Sub DTCDesignChildForm_FormClosing(ByVal sender As Object,
ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles
Me.FormClosing

Dim myParent As FormMain = Me.MdiParent

If myParent.MdiChildren.Length = 1 Then
myParent.ToolStripSaveButton.Visible = False
myParent.ToolStripButtonExit.Visible = False
myParent.ValidateDTCToolStripMenuItem.Enabled = False
myParent.TestBackfillToolStripMenuItem.Enabled = False
End If

End Sub

I went through the apparent thing you are going through, that is
developing your first complex .NET MDI application, a few years ago,
and remember figuring out the same things you have been asking about.
That's why I have been ressponding to many of your questions.

One thing I found that you may not have found yet us this event
handler you're going to want to put in the main parent form:

Private Sub WindowsToolStripMenuItem_DropDownOpening(ByVal sender
As Object, ByVal e As System.EventArgs) Handles
WindowsToolStripMenuItem.DropDownOpening

Dim i As Integer = -1
Dim myItem As ToolStripItem

For Each myItem In MenuStrip.MdiWindowListItem.DropDown.Items
If TypeOf myItem Is ToolStripMenuItem Then
With CType(myItem, ToolStripMenuItem)
If .IsMdiWindowListEntry Then
i += 1
.Text = (i + 1).ToString("&0") & " " &
MdiChildren(i).Text
End If
End With
End If
Next

End Sub

Private Sub WindowsToolStripMenuItem_DropDownOpening(ByVal sender
As Object, ByVal e As System.EventArgs) Handles
WindowsToolStripMenuItem.DropDownOpening

Dim i As Integer = -1
Dim myItem As ToolStripItem

For Each myItem In MenuStrip.MdiWindowListItem.DropDown.Items
If TypeOf myItem Is ToolStripMenuItem Then
With CType(myItem, ToolStripMenuItem)
If .IsMdiWindowListEntry Then
i += 1
.Text = (i + 1).ToString("&0") & " " &
MdiChildren(i).Text
End If
End With
End If
Next

End Sub

WindowsToolStripMenuItem is the main parent form's MdiWindowListItem
in my app.
 

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