Title | Give MDI child forms different toolbars |
Keywords | MDI, MDIForm, toolbar |
Categories | Tips and Tricks, Controls |
|
|
The MDI form's TakeToolbar method installs a form's toolbar. It sets the MDI form's toolbar's ImageList to the ImageList on the form. It clears the MDI form's toolbar and uses the images in the ImageList to make buttons on the MDI form's toolbar.
This routine is used in two ways. First, when a child form activates, it displays the child's toolbar. Second, if no child is active, it displays the MDI form's toolbar.
|
|
Private ToolbarHolder As Form
' Give the toolbar to the indicated form.
Public Sub TakeToolbar(frm As Form)
Dim btn As Button
Dim img As ListImage
Dim i As Integer
' Save the form for later.
Set ToolbarHolder = frm
' Attach the Toolbar to the ImageList.
Set MDIToolbar.ImageList = frm.imlToolbarImages
' Create the new buttons.
MDIToolbar.Buttons.Clear
' Add the buttons.
i = 1
For Each img In frm.imlToolbarImages.ListImages
' Create the button with the desired picture.
Set btn = MDIToolbar.Buttons.Add _
(, img.Key, , tbrDefault, i)
btn.ToolTipText = img.Tag
btn.Description = img.Tag
i = i + 1
Next img
End Sub
' Give the toolbar back to the MDI form.
Public Sub ReleaseToolbar(frm As Form)
If Not (ToolbarHolder Is frm) Then Exit Sub
TakeToolbar Me
End Sub
|
|
Subroutine ReleaseToolbar returns the toolbar to the MDI form. This routine is called when a child form loses focus.
|
|
' Give the toolbar back to the MDI form.
Public Sub ReleaseToolbar(frm As Form)
If Not (ToolbarHolder Is frm) Then Exit Sub
TakeToolbar Me
End Sub
|
|
Whenever a child form activates, deactivates, or unloads, the program switches the toolbar.
|
|
Private Sub Form_Activate()
MDIForm1.TakeToolbar Me
End Sub
Private Sub Form_Deactivate()
MDIForm1.ReleaseToolbar Me
End Sub
Private Sub Form_Unload(Cancel As Integer)
MDIForm1.ReleaseToolbar Me
End Sub
|
|
That takes care of displaying the correct toolbar buttons. Now the program needs to take action when the user clicks on them.
The MDI form really owns the toolbar so its ButtonClick event fires when the user clicks a button. That routine calls the ToolbarClick method for the currently active child. If no child is active, the MDI form itself holds the toolbar so the routine performs the appropriate action for the button (show child 1 or show child 2).
|
|
Private Sub MDIToolbar_ButtonClick(ByVal Button As _
ComctlLib.Button)
Dim frm As Form
If ToolbarHolder Is Me Then
' There is no toolbar holder. Handle the
' button click here.
Select Case Button.Key
Case "NewChild1"
Set frm = New Child1
frm.Show
Case "NewChild2"
Set frm = New Child2
frm.Show
End Select
Else
' Make the toolbar holder handle the click.
ToolbarHolder.ToolbarClick Button
End If
End Sub
|
|
Finally, the child forms implement ToolbarClick subroutines to process toolbar clicks.
|
|
Public Sub ToolbarClick(ByVal Button As ComctlLib.Button)
Dim frm As Form
Select Case Button.Key
Case "NewChild1"
Set frm = New Child1
frm.Show
Case "NewChild2"
Set frm = New Child2
frm.Show
Case Else
MsgBox "Child1: " & Button.Key
End Select
End Sub
|
|
|
|