ComponentOne Menus and Toolbars for WinForms
Menus and Toolbars for WinForms Task-Based Help / Menu Tasks / Adding a Submenu
In This Topic
    Adding a Submenu
    In This Topic

    A well-structured and intuitive user interface is essential for enhancing user experience and navigation. This can be achieved by adding submenus to main menu. The following section provides step-by-step approach to add submenu to C1MainMenu component as shown in the below image:

    Submenu

    To add a submenu item to C1MainMenu at design time

    To add a new command link to the menu (C1CommandMenu or C1ContextMenu) linked by the currently selected command link, complete the following steps:

    Note: This item is disabled unless the current command link is linked to a C1CommandMenu or C1ContextMenu type command.
    1. Right-click on an existing menu item in the C1MainMenu and select Add Child Item from the context menu.
    2. Type Submenu1 in the Text textbox field and click OK.

    Run the application to see submenu created under the main menu component.

    To add a new command link to the menu (C1CommandMenu or C1ContextMenu) linked by the currently selected command link, complete the following steps:

    Note: This item is disabled unless the current command link is linked to a C1CommandMenu or C1ContextMenu type command.
    1. Right-click on an existing menu item in the C1MainMenu and select Add Child Item from the context menu.
    2. Type Submenu1 in the Text textbox field and click OK. The submenu now added, can be seen under the main menu component at design time.

    To add a submenu item to C1MainMenu programmatically

    To programmatically add submenu item, complete the following steps:

    1. Add the C1.Win.Command DLL from NuGet.org to your project. In the code window, include the namespace as shown in below:
      C#
      Copy Code
      using C1.Win.Command;
      

      VB
      Copy Code
      Imports C1.Win.Command
      
    2. In code window insert the following code to add a main menu and a corresponding submenu
      CS
      Copy Code
      //Add a C1CommandHolder to hold the menu and Create a new main menu.
      C1CommandHolder ch = C1CommandHolder.CreateCommandHolder(this);
      C1MainMenu mm = new C1MainMenu();
      
      //Add the main menu control to form. 
      this.Controls.Add(mm);
      
      //Create the main menu to hold the command. Set the text property for the new menu.
      C1CommandMenu mmenu = (C1CommandMenu)ch.CreateCommand(typeof(C1CommandMenu));
      mmenu.Text = "Menu 1";
      
      //Add commandlink to the new main menu. 
      mm.CommandLinks.Add(new C1CommandLink(mmenu));
      
      //Create a submenu item as command. Set the text property for submenu.
      C1Command submenu = ch.CreateCommand();
      submenu.Text = "Submenu1";
      
      //Add commandlink to submenu item.
      mmenu.CommandLinks.Add(new C1CommandLink(submenu));
      
      VB
      Copy Code
      'Add a C1CommandHolder to hold the menu and Create a new main menu.
      Dim ch As C1CommandHolder = C1CommandHolder.CreateCommandHolder(Me)
      Dim mm As New C1MainMenu
      
      'Add the main menu control to form.
      Me.Controls.Add(mm)
      
      'Create the main menu to hold the command. Set the text property for the new menu.
      Dim mmenu As C1CommandMenu = CType(ch.CreateCommand(GetType(C1CommandMenu)), C1CommandMenu)
      mmenu.Text = "Menu 1"
      
      'Add commandlink to the new main menu. 
      mm.CommandLinks.Add(New C1CommandLink(mmenu))
      
      'Create a submenu item as command. Set the text property for submenu.
      Dim submenu As C1Command = ch.CreateCommand()
      submenu.Text = "Submenu1"
      
      'Add commandlink to submenu item.
      mmenu.CommandLinks.Add(New C1CommandLink(submenu))
      

    Run the code and observe the output.

    1. Add the C1.Win.C1Command DLL from NuGet.org to your project. In the code window, include the namespace as shown in below:
      C#
      Copy Code
      using C1.Win.C1Command;
      

      VB
      Copy Code
      Imports C1.Win.C1Command
      
    2. In code window insert the following code to add a main menu and a corresponding submenu
      CS
      Copy Code
      //Add a C1CommandHolder to hold the menu and Create a new main menu.
      C1CommandHolder ch = C1CommandHolder.CreateCommandHolder(this);
      C1MainMenu mm = new C1MainMenu();
      
      //Add the main menu control to form. 
      this.Controls.Add(mm);
      
      //Create the main menu to hold the command. Set the text property for the new menu.
      C1CommandMenu mmenu = (C1CommandMenu)ch.CreateCommand(typeof(C1CommandMenu));
      mmenu.Text = "Menu 1";
      
      //Add commandlink to the new main menu. 
      mm.CommandLinks.Add(new C1CommandLink(mmenu));
      
      //Create a submenu item as command. Set the text property for submenu.
      C1Command submenu = ch.CreateCommand();
      submenu.Text = "Submenu1";
      
      //Add commandlink to submenu item.
      mmenu.CommandLinks.Add(new C1CommandLink(submenu));
      
      VB
      Copy Code
      'Add a C1CommandHolder to hold the menu and Create a new main menu.
      Dim ch As C1CommandHolder = C1CommandHolder.CreateCommandHolder(Me)
      Dim mm As New C1MainMenu
      
      'Add the main menu control to form.
      Me.Controls.Add(mm)
      
      'Create the main menu to hold the command. Set the text property for the new menu.
      Dim mmenu As C1CommandMenu = CType(ch.CreateCommand(GetType(C1CommandMenu)), C1CommandMenu)
      mmenu.Text = "Menu 1"
      
      'Add commandlink to the new main menu. 
      mm.CommandLinks.Add(New C1CommandLink(mmenu))
      
      'Create a submenu item as command. Set the text property for submenu.
      Dim submenu As C1Command = ch.CreateCommand()
      submenu.Text = "Submenu1"
      
      'Add commandlink to submenu item.
      mmenu.CommandLinks.Add(New C1CommandLink(submenu))
      

    Run the code and observe the output.

    See Also