User Interface for .NET / User's Guide / Command Bars / Using Command Parents

Using Command Parents
 Command Parents Notifications

As a NCommandParent control is used mainly to visualize command instances, you can use the functionality provided by its children NCommand objects. You could, however, use the notifications provided by the parent about an event that was fired by a child NCommand. Each NCommand will delegate its notification to the instance returned of its TopLevelParent property. A top-level parent is the parent that contains the TopLevelCommand instance. In most cases such a top-level parent is either a NToolbar instance or a NMenuWindow for context-sensitive menus.

 

The following table describes notifications fired by a NCommandParent control:

Notification Description
 CommandClick Fired whenever a child command has been clicked.
 CommandPopup Fired whenever a child command is about to display its drop-down menu.
Any direct child or one of its nested children is considered a child command. Notifications are fired if the command itself has not handled the event.

The following code demonstrates how to capture the CommandClick notification:

C#
Copy Code
//create a NToolbar control
NToolbar toolbar = new NToolbar();
toolbar.CommandClick += new CommandEventHandler(OnCommandClick);

//create two commands and add them to the toolbar
NCommand command1, command2;

command1 = new NCommand();
command1.Properties.ID = 1;

command2 = new NCommand();
command2.Properties.ID = 2;

//add commands to the toolbar
toolbar.Commands.AddRange(new NCommand[]{command1, command2});

...

private void OnCommandClick(object sender, CommandEventArgs args)
{
    //get the id of clicked command
    int id = args.Command.Properties.ID;

    switch(id)
    {
  case 1:
   MessageBox.Show("Command 1 was clicked!");
   break;

  case 2:
   MessageBox.Show("Command 2 was clicked!");
   break;
    }
}
Visual Basic
Copy Code
'create a NToolbar control
Dim toolbar As NToolbar = New NToolbar()
AddHandler toolbar.CommandClick, AddressOf OnCommandClick

'create two commands and add them to the toolbar
Dim command1, command2 As NCommand 

command1 = New NCommand()
command1.Properties.ID = 1

command2 = New NCommand()
command2.Properties.ID = 2

'add commands to the toolbar
toolbar.Commands.AddRange(New NCommand(){command1, command2})

...

Private Sub OnCommandClick(object sender, CommandEventArgs args) Handles toolbar.CommandClick
    'get the id of clicked command
    Dim id As Integer = args.Command.Properties.ID

    Select Case id
        case 1
             MessageBox.Show("Command 1 was clicked!")
        case 2
            MessageBox.Show("Command 2 was clicked!")
    End Select
End Sub

The following code demonstrates the usage of CommandPopup notification:

C#
Copy Code
//create a NToolbar and add a drop-down command to it
NToolbar toolbar = new NToolbar();
toolbar.CommandPopup += new CommandEventHandler(OnCommandPopup);

//create a NCommand object which will always display a drop-down menu
NCommand command = new NCommand();
command.Properties.DropDownBehaviour = DropDownBehaviour.AlwaysDropDown;
command.Properties.ShowArrowStyle = ShowArrowStyle.Never;
command.Properties.ID = 0;

//add command to the toolbar
toolbar.Commands.Add(command);

...

private void OnCommandPopup(object sender, CommandEventArgs args)
{
    //get command id
    int id = args.Command.Properties.ID;
    if(id != 0)
    return;

    //as this notification is called just before a NCommand is about to display its drop-down menu
    //you may use it to dynamically populate the affected command's children
    //or change existing children's state - Checked, Enabled, Visible, etc.

    args.Command.Commands.Clear();

    //populate all floating panels
    NCommand comm;

    foreach(INDockingPanel panel in this.m_DockManager.GetPanels(DockState.Floating))
    {
        comm = new NCommand();
        comm.Properties.Text = panel.Text;
        comm.Checked = true;
        //add the newly created command to the affected one's children
        args.Command.Commands.Add(comm);
    }
}
Visual Basic
Copy Code
'create a NToolbar and add a drop-down command to it
Dim toolBar As NToolbar = New NToolbar()
AddHandler toolbar.CommandPopup, AddressOf OnCommandPopup

'create a NCommand object which will always display a drop-down menu
Dim command As NCommand = New NCommand()
command.Properties.DropDownBehaviour = DropDownBehaviour.AlwaysDropDown
command.Properties.ShowArrowStyle = ShowArrowStyle.Never
command.Properties.ID = 0

'add command to the toolbar
toolbar.Commands.Add(command)

...

Private Sub OnCommandPopup(ByVal sender As Object, ByVal args As CommandEventArgs) Handles toolbar.CommandPopup
    'get command id
    Dim id As Integer = args.Command.Properties.ID
    If id <> 0 Then
        return
    End If

    'as this notification is called just before a NCommand is about to display its drop-down menu
    'you may use it to dynamically populate the affected command's children
    'or change existing children's state - Checked, Enabled, Visible, etc.

    args.Command.Commands.Clear()

    'Populate all floating panels
    Dim comm As NCommand

    for Each panel As INDockingPanel In Me.m_DockManager.GetPanels(DockState.Floating)
        comm = New NCommand()
        comm.Properties.Text = panel.Text
        comm.Checked = True
        'add the newly created command to the affected one's children
        args.Command.Commands.Add(comm)
    Next
End Sub
 The NToolbar Component
The standalone NCommandParent which can be used in your application without being encapsulated by a NCommandBarsManager is the NToolbar. It is a generic command container, has rich design-time support and integrates easily in your WinForm projects.
See Also