This component is part of the Nuget package rixControls

It helps you to centralise the code needed to control whether buttons or other controls are enabled or disabled as the state changes in your application

It can optionally allow easy control of who can access a given control too

Multiple controls can be grouped too, so if you have menu items and buttons that do the same thing in your application, they are all controlled in one place

I’ve created a form to demonstrate the use of the Command Manager, it has three buttons and menu items, and two checkboxes which will be used to simulate state changing in your application:

CommandManager test form

When the Odd numbers check box is checked, we will enable the buttons ONE & THREE and the associated menu items too. Similarly, when the Even numbers check box is checked, we will enable button and menu item TWO

If you didn’t already, add rixControls to your project using the Nuget Console:

install-package rixControls

This is a .NET Framework 4.6.1 package

This is the code behind the form (VB) to make it work: (see bottom of article for C Sharp code)

Imports rixControls.rixCommandManager

Public Class frmCommandManager
  Private mCommandManager As CommandManager

#Region "Command Manager"
  Sub InitializeCommandManager()
    mCommandManager = New CommandManager
    mCommandManager.AddCommand("Oddness", AddressOf OnOdd, AddressOf UpdateOdd, New Object() {ToolStripButton1, ToolStripButton3, MenuONEToolStripMenuItem, MenuTHREEToolStripMenuItem})
    mCommandManager.AddCommand("Evenness", AddressOf OnEven, AddressOf UpdateEven, New Object() {ToolStripButton2, MenuTWOToolStripMenuItem})
  End Sub

  Sub OnOdd(c As Command)
    lblMessage.Text = "An odd button was clicked"
  End Sub

  Sub UpdateOdd(c As Command)
    c.Enabled = chkOddNumbers.Checked
  End Sub

  Sub OnEven(c As Command)
    lblMessage.Text = "An even button was clicked"
  End Sub

  Sub UpdateEven(c As Command)
    c.Enabled = chkEvenNumbers.Checked
  End Sub
#End Region

  Private Sub frmCommandManager_Load(sender As Object, e As EventArgs) Handles Me.Load
  End Sub
End Class

Here are the results in the main form:

Odd numbers selected
Even numbers selected

So far you have seen how buttons can be grouped and their logic for (a) execution and (b) accessibility have been centralised into simple code blocks

Another layer of control is allowing specific groups of users access to buttons…

user group access control

I’m going to create an enumeration in my code to hold what will be the currently logged in users’ permissions. This is the enum:

Public Enum Permissions
  NoAccess = 0
  JustAllow = 1
  ONEAccess = 2
  TWOAccess = 4
  THREEAccess = 8
End Enum

You can programmatically assign these permissions to users as bitwise values. The first two (zero and 1) are hard coded in the command manager and so you should always use those in your own enumerations

So, a user who is given permissions of 12 will have access to both THREEAccess [8] and TWOAccess [4] (8 + 4) levels. The individual enum values are given to the command groups in an overload of the AddCommand method, here’s the updated code:

  Sub InitializeCommandManager()
    mCommandManager = New CommandManager
    mCommandManager.AddCommand("ONE", AddressOf OnOdd, AddressOf UpdateOdd, New Object() {ToolStripButton1, MenuONEToolStripMenuItem}, Permissions.ONEAccess)
    mCommandManager.AddCommand("Evenness", AddressOf OnEven, AddressOf UpdateEven, New Object() {ToolStripButton2, MenuTWOToolStripMenuItem}, Permissions.TWOAccess)
    mCommandManager.AddCommand("THREE", AddressOf OnOdd, AddressOf UpdateOdd, New Object() {ToolStripButton3, MenuTHREEToolStripMenuItem}, Permissions.THREEAccess)
  End Sub

In this modified code, we are adding the one and three buttons/menu items separately so that we can properly assign permissions values to them

I added a numeric dropdown control to simulate the user access level:

Whenever the numeric up down control is changed, we’ll pretend that a new user is logging in and we’ll assign the new value to the CommandManager, here is the code:

  Private Sub NumericUpDown1_ValueChanged(sender As Object, e As EventArgs) Handles NumericUpDown1.ValueChanged
    mCommandManager.OperatorValue = NumericUpDown1.Value
  End Sub

That is it! The buttons now are only enabled according to (1) the logic in the Update code block which is able to inspect the state of your application objects to see if the button should be enabled or not and (2) if the current user has access to the permissions that were assigned to the command

c sharp version

I made this up in C Sharp too, here is the full form code:

using rixControls.rixCommandManager;
using System.Windows.Forms;

namespace rixControlsLocalTesterCSharp
    public partial class frmCommandManager : Form
        CommandManager mCommandManager;

        void InitialiseCommandManager()
            mCommandManager = new CommandManager();
            mCommandManager.AddCommand("ONE", OnOdd, UpdateOdd, new object[] {menuONEToolStripMenuItem, toolStripButton1}, (int)Permissions.ONEAccess);
            mCommandManager.AddCommand("TWO", OnEven, UpdateEvent, new object[] {menuTWOToolStripMenuItem, toolStripButton2}, (int)Permissions.TWOAccess);
            mCommandManager.AddCommand("THREE", OnOdd, UpdateOdd, new object[] {menuTHREEToolStripMenuItem, toolStripButton3}, (int)Permissions.THREEAccess);

        void OnOdd(Command c)
            lblMessage.Text = "An odd number button was pressed";
        void UpdateOdd(Command c)
            c.Enabled = chkOddNumbers.Checked;
        void OnEven(Command c)
            lblMessage.Text = "An even number button was pressed";
        void UpdateEvent(Command c)
            c.Enabled = chkEvenNumbers.Checked;

        public frmCommandManager()

        private void frmCommandManager_Load(object sender, System.EventArgs e)

        private void numericUpDown1_ValueChanged(object sender, System.EventArgs e)
            mCommandManager.OperatorValue =(ulong)numericUpDown1.Value;

Leave a Reply

Your email address will not be published. Required fields are marked *