5

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.

grid in initial mode before editing:

grid during/after editing:

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
Jim
  • 51
  • 3
  • ugh - I can't post images either... :( – Jim Sep 26 '13 at 18:46
  • 1
    use the toolbar in the editor to include an image. – StriplingWarrior Sep 26 '13 at 18:46
  • It won't let me save cuz I don't have 10 points... – Jim Sep 26 '13 at 18:48
  • 1
    I just gave your post a +1 so you can now post an image. – Brian Sep 26 '13 at 18:55
  • Does your `FormMode` property setter handle enabling/disabling the appropriate menu items? – Jim Mischel Sep 26 '13 at 19:01
  • 2
    The solution to all this is called [MVVM](http://stackoverflow.com/a/14382137/643085), and more specifically [Commands](http://wpftutorial.net/DelegateCommand.html) or [RoutedCommands](http://joshsmithonwpf.wordpress.com/2008/03/18/understanding-routed-commands/). (Un)fortunately none of that is supported in winforms, that's why you really need to consider moving to current UI technologies. – Federico Berasategui Sep 26 '13 at 19:01
  • Yes the FormMode property handles the enabling/disabling: – Jim Sep 26 '13 at 19:04
  • OK, I will check those out... thanks. Am I in the stone age here? ugh – Jim Sep 26 '13 at 19:06
  • @jim clearly. winforms is not recommended for any new projects. – Federico Berasategui Sep 26 '13 at 19:09
  • wow - OK, what is recommended? I am just a single developer in a corporate setting. Thanks. – Jim Sep 26 '13 at 19:16
  • 3
    @Jim HighCore is referring to Windows Presentation Foundation (WPF), the replacement for WinForms. Steep learning curve though. – Mathieu Guindon Sep 26 '13 at 19:21
  • 1
    http://stackoverflow.com/questions/11293859/can-i-assign-a-method-to-multiple-form-based-events/11294115#11294115 – Hans Passant Sep 26 '13 at 20:20
  • @retailcoder as I already mentioned many times here in S.O, I disagree on the statement that `WPF has a steep learning curve`. It is true that the framework is huge and that there are many new concepts, but that is no different or somehow "harder to learn" than the many horrendous hacks (such as P/Invoke and "owner draw" and whatnot) needed to do anything useful in winforms. – Federico Berasategui Sep 27 '13 at 15:49

1 Answers1

0

I'm new to StackOverflow. I do not have enough reputation to post a comment but I hope that I can help.

It looks like you have a GridView control and would like to use a GridView control's event in order to disable/enabled a command bar's buttons. I recommend trying the DataGridView_CellValueChanged event. Instead of calling UpdateBalance() as in the MSDN example, you would enable save and cancel buttons. If the CellValueChanged does not work for you, there are also CellBeginEdit and CellEndEdit.

Every command button should have a Click event that you should be able to use. I do not know if your save button is custom or standard. Regardless, a Click event should exist that you can use for disabling.

A full list of GridView control events are available here. Reviewing them might help. While I may not be a heavy .NET user, I have done command button disable/enable with User Forms in Excel using events. I'm sure you can do the same. Don't give up!

--Jeoffri

  • thanks. I am actually using DevExpress Grid Control, and I am using the cell value changed to set the FormMode property, but my question is more about if there was a way to do that automatically and I think what HighCore is referring to above is probably the best approach. – Jim Sep 27 '13 at 11:59