Are there any real good strategies out there for enabling and disabling menu items based on multiple conditions?
Example - I have an editable grid with 'Save' and 'Cancel' toolbar menu items (and others). When the grid is initially loaded, it checks to see if the user has permissions to edit the items. If the user has permissions hd can edit the grid. Initially the Save and Cancel are disabled because they are not needed. If the user makes edits, then I want them both to be enabled. Currently I do this with what I call 'FormMode' property. When the user starts editing, it puts the form/grid in 'Dirty' mode and the Save and Cancel buttons are enabled. If any editable control is changed, it sets this (FormMode) property to Dirty. If they hit the Save or Cancel buttons then the data is saved and the buttons are disabled again (they are not needed).
My question is, is there a better, more elegant way to handle this enabling/disabling by using events or other properties? I have this same scenario on dozens of forms and grids and it seems like there should be an easier way to deal with it. Can the menu items be 'aware' of the form/grid state and respond automatically? And could I reuse them over multiple forms?
I'm not sure if my question is clear - sorry if not. But it seems like I spend a lot of time getting the menu items to behave correctly depending on the 'mode' of the form. I like to have them only enabled when it is appropriate.
Here is the property setter:
Public Property GridDataMode() As Mayfran.Base.BaseUtilities.FormMode Implements IGridDataMode.GridDataMode
Get
Return _GridDataMode
End Get
Set(ByVal arg As Mayfran.Base.BaseUtilities.FormMode)
'if mode is different from previous mode, then continue:
If _GridDataMode <> arg Then
_GridDataMode = arg
Select Case _GridDataMode
Case FormMode.Initial
'nothing to do here
Case FormMode.Dirty, FormMode.NewRecord
barButtonItemSave.Enabled = Editable
barButtonItemCancel.Enabled = True
barButtonItemSelectAll.Enabled = False
barButtonItemDelete.Enabled = False
barButtonItemPrint.Enabled = False
barButtonItemRefresh.Enabled = False
Case FormMode.RecordLoaded
barButtonItemSave.Enabled = False
barButtonItemCancel.Enabled = False
barButtonItemSelectAll.Enabled = True
barButtonItemDelete.Enabled = Editable
barButtonItemPrint.Enabled = True
barButtonItemRefresh.Enabled = True
Case Else
Exit Select
End Select
RaiseEvent GridModeChanged(arg)
End If
End Set
End Property