Methods for linking menu items and toolbar buttons? 
Author Message
 Methods for linking menu items and toolbar buttons?

I'm exploring ways of optimizing my menus, toolbars, and command buttons. I
have several menu items (and their corresponding toolbar and command
buttons) that get enabled or disabled at various times based on several
program conditions and user actions.

I'm currently using property let procedures in a bas module (thanks to J
French for this tip a while back) to perform several actions when a global
variable changes. For example:

*** code ***

Public Property Let PatternLoaded(Loaded As Boolean)
   m_bPatternLoaded = Loaded
   With frmMain
      If Loaded Then
         .mnuPatternItems(MNU_PATTERNSTORE).Enabled = True
         .cmdPattern(STORE).Enabled = True
         .tbrMain.Buttons("StorePattern").Enabled = True
         .mnuPatternItems(MNU_PATTERNCLEAR).Enabled = True
         .tbrMain.Buttons("ClearAllPatterns").Enabled = True
      Else
         .mnuPatternItems(MNU_PATTERNSTORE).Enabled = False
         .cmdPattern(STORE).Enabled = False
         .tbrMain.Buttons("StorePattern").Enabled = False
         .mnuPatternItems(MNU_PATTERNCLEAR).Enabled = False
         .tbrMain.Buttons("ClearAllPatterns").Enabled = False
      End If
   End With
End Property

*** end code ***

Although this works well, there are other places where commands are
enabled/disabled, and obviously many more menu/button items and variables.

What is your favorite method for synchronizing menus and toolbar/command
buttons? It would be nice to just enable/disable a menu item, and have this
fire an event (a menu enabled event?) that could contain the code to control
the related buttons.

Any ideas?

Thanks...

Calan



Thu, 15 Sep 2005 18:54:54 GMT  
 Methods for linking menu items and toolbar buttons?
I used a similar way. Instead of a property, I used a public Sub.
The first thing to shorten this up is:

Public Property Let PatternLoaded(Loaded As Boolean)
   m_bPatternLoaded = Loaded
   With frmMain
     .mnuPatternItems(MNU_PATTERNSTORE).Enabled = Loaded
     .cmdPattern(STORE).Enabled = Loaded
     .tbrMain.Buttons("StorePattern").Enabled = Loaded
     .mnuPatternItems(MNU_PATTERNCLEAR).Enabled = Loaded
     .tbrMain.Buttons("ClearAllPatterns").Enabled = Loaded
   End With
End Property

Johan.



Quote:
> I'm exploring ways of optimizing my menus, toolbars, and command buttons.
I
> have several menu items (and their corresponding toolbar and command
> buttons) that get enabled or disabled at various times based on several
> program conditions and user actions.

> I'm currently using property let procedures in a bas module (thanks to J
> French for this tip a while back) to perform several actions when a global
> variable changes. For example:

> *** code ***

> Public Property Let PatternLoaded(Loaded As Boolean)
>    m_bPatternLoaded = Loaded
>    With frmMain
>       If Loaded Then
>          .mnuPatternItems(MNU_PATTERNSTORE).Enabled = True
>          .cmdPattern(STORE).Enabled = True
>          .tbrMain.Buttons("StorePattern").Enabled = True
>          .mnuPatternItems(MNU_PATTERNCLEAR).Enabled = True
>          .tbrMain.Buttons("ClearAllPatterns").Enabled = True
>       Else
>          .mnuPatternItems(MNU_PATTERNSTORE).Enabled = False
>          .cmdPattern(STORE).Enabled = False
>          .tbrMain.Buttons("StorePattern").Enabled = False
>          .mnuPatternItems(MNU_PATTERNCLEAR).Enabled = False
>          .tbrMain.Buttons("ClearAllPatterns").Enabled = False
>       End If
>    End With
> End Property

> *** end code ***

> Although this works well, there are other places where commands are
> enabled/disabled, and obviously many more menu/button items and variables.

> What is your favorite method for synchronizing menus and toolbar/command
> buttons? It would be nice to just enable/disable a menu item, and have
this
> fire an event (a menu enabled event?) that could contain the code to
control
> the related buttons.

> Any ideas?

> Thanks...

> Calan



Thu, 15 Sep 2005 19:55:59 GMT  
 Methods for linking menu items and toolbar buttons?
On Sun, 30 Mar 2003 10:54:54 GMT, "Calan"

Quote:

>I'm exploring ways of optimizing my menus, toolbars, and command buttons. I
>have several menu items (and their corresponding toolbar and command
>buttons) that get enabled or disabled at various times based on several
>program conditions and user actions.
<snip>
> It would be nice to just enable/disable a menu item, and have this
>fire an event (a menu enabled event?) that could contain the code to control
>the related buttons.

>Any ideas?

You could do the following
A Form and a Class follow :-

Option Explicit

Dim WithEvents MnuMgr As cMnuMgr

====== FORM1.FRM ========

Option Explicit

Dim WithEvents MnuMgr As cMnuMgr

Private Sub Form_Load()
    Set MnuMgr = New cMnuMgr
End Sub

Private Sub Command1_Click()
    MnuMgr.SetEnabled(mnuFile) = False
End Sub

Private Sub MnuMgr_ChangeState(Mnu As Menu)
    Dim B As Boolean

    Me.Print Mnu.Name

    B = Mnu.Enabled
    Select Case True
           Case (Mnu Is mnuFile) = True
                Command1.Enabled = B
    End Select
End Sub

======= cMnuMgr.CLS ======

Option Explicit

Public Event ChangeState(Mnu As Menu)

Public Property Let SetEnabled(Mnu As Menu, Value As Boolean)
   Mnu.Enabled = Value
   RaiseEvent ChangeState(Mnu)
End Property

===========================

The idea is that one sets the enabled property using :-
    MnuMgr.SetEnabled( mnuFile ) = False

Realistically it is not that much better than calling the routine
directly rather than the 'indirection' of using the Class

ie: using a Property within the Form
    or putting the whole thing somewhere else to isolate it

However the Select Case routine would probably simplify things for you

- as would the use of just one Flag ( B As Boolean )

I think I would just go for a simple routine, with the Select Case
stuff inside it.

Maybe it will give you some ideas ...



Thu, 15 Sep 2005 20:47:21 GMT  
 Methods for linking menu items and toolbar buttons?
If the menu options that are linked to command buttons are not top
level ones then there is no need to change their Enabled state until
you click on their parent menu.  For example, a File menu with a few
suboptions :

File
    New
    Open
    Save

If you want to go the New option, you have to click on File first.
Therefore in mnuFile_Click() put :

mnuNew.Enabled = cmdNew.Enabled

While you may set the state of your command buttons in many places, it
is a waste of code to set the menu at the same time.  Also, this way
the menu option is guaranteed to match the state of the relevant
command button whenever anyone actually goes to use it.

I picked up this tip from Francesco Balena's book Programming Visual
Basic 6.

Martin

Quote:

> I'm exploring ways of optimizing my menus, toolbars, and command buttons. I
> have several menu items (and their corresponding toolbar and command
> buttons) that get enabled or disabled at various times based on several
> program conditions and user actions.

> I'm currently using property let procedures in a bas module (thanks to J
> French for this tip a while back) to perform several actions when a global
> variable changes. For example:

> *** code ***

> Public Property Let PatternLoaded(Loaded As Boolean)
>    m_bPatternLoaded = Loaded
>    With frmMain
>       If Loaded Then
>          .mnuPatternItems(MNU_PATTERNSTORE).Enabled = True
>          .cmdPattern(STORE).Enabled = True
>          .tbrMain.Buttons("StorePattern").Enabled = True
>          .mnuPatternItems(MNU_PATTERNCLEAR).Enabled = True
>          .tbrMain.Buttons("ClearAllPatterns").Enabled = True
>       Else
>          .mnuPatternItems(MNU_PATTERNSTORE).Enabled = False
>          .cmdPattern(STORE).Enabled = False
>          .tbrMain.Buttons("StorePattern").Enabled = False
>          .mnuPatternItems(MNU_PATTERNCLEAR).Enabled = False
>          .tbrMain.Buttons("ClearAllPatterns").Enabled = False
>       End If
>    End With
> End Property

> *** end code ***

> Although this works well, there are other places where commands are
> enabled/disabled, and obviously many more menu/button items and variables.

> What is your favorite method for synchronizing menus and toolbar/command
> buttons? It would be nice to just enable/disable a menu item, and have this
> fire an event (a menu enabled event?) that could contain the code to control
> the related buttons.

> Any ideas?

> Thanks...

> Calan



Fri, 16 Sep 2005 17:21:03 GMT  
 Methods for linking menu items and toolbar buttons?
Calan,

My preferred way is to use some variation of the Observer pattern.
The idea is to create an observer that contains or has access to an
FSM (Finite State Machine) that indicates which controls are related
to which others, and for the form to create an instance of this
observer and add to it controls to be observed.  When the user
manipulates one of the observed controls, the observer automatically
sets states of associated controls.

--A



Sat, 17 Sep 2005 13:33:09 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Responding to Menu items and Toolbar buttons

2. Simulating toolbar button click through menu item?

3. How to insert images in the dropdown toolbar button menu items in VB.

4. Linking Private Subs to toolbars/menus

5. Control Keys vs Menu and Toolbar items

6. Adding Menu item to the Mail item menu from an outlook Addin

7. Adding Menu item to the Mail item menu from an outlook Addin

8. Adding a button tothe toolbar on task item

9. linking functions to a menu-item

10. Direct link from menu item to Help File

11. Invoking a menu items onaction method

12. Active/Inactive toolbar buttons/menu commands

 

 
Powered by phpBB® Forum Software