DevComponents.com

How to Create Menu Using Code

You can create menus in either design-time using the DotNetBar Windows Forms designer or in run-time by writting the the code to do so. You can always create the menu in design-time and explore the InitializeComponent method that is automatically created on the form and which will have all the code that is needed to create the menu that you actually see on the form.

Creating menu from code

To create the menu from code follow these steps:

  1. Create new Bar object and set its MenuBar and Stretch properties to true
  2. Add the Bar object to the DotNetBarManager.Bars collection using the Add method
  3. Set the DockSide property of the Bar object to eDockSide.Top
  4. Create ButtonItem object and assign name and text to it
  5. Add that ButtonItem object to the Bar.Items collection using Add method
  6. To create popup menu items for newly created menu item add new ButtonItem objects to the ButtonItem SubItems collection using the Add method.
  7. Repeat steps 4-6 to create additional main menu items and/or popup menu items associated with them

See following code for sample implementation. Also review the MenuFromCode sample shipped with DotNetBar for working sample and full source code.


VB

        Imports DevComponents.DotNetBar
.
.
.
Dim bar As Bar
Dim menu, submenu As ButtonItem

bar = New Bar("Menu Bar")
bar.MenuBar = True
bar.Stretch = True
dotNetBarManager1.Bars.Add(bar)
bar.DockSide = eDockSide.Top

menu = New ButtonItem("bFile", "&File")
bar.Items.Add(menu)
submenu = New ButtonItem("bOpen", "&Open")
menu.SubItems.Add(submenu)
submenu = New ButtonItem("bClose", "&Close")
menu.SubItems.Add(submenu)
submenu = New ButtonItem("bExit", "&Exit")
submenu.BeginGroup = True
menu.SubItems.Add(submenu)

menu = New ButtonItem("bEdit", "&Edit")
bar.Items.Add(menu)
submenu = New ButtonItem("bCut", "&Cut")
menu.SubItems.Add(submenu)
submenu = New ButtonItem("bCopy", "&Copy")
menu.SubItems.Add(submenu)
submenu = New ButtonItem("bPaste", "&Paste")
menu.SubItems.Add(submenu)
submenu = New ButtonItem("bClear", "&Clear")
submenu.BeginGroup = True
menu.SubItems.Add(submenu)
bar.RecalcLayout()


C#

         using DevComponents.DotNetBar;
.
.
.
Bar bar=new Bar("Menu Bar");
bar.MenuBar=true;
bar.Stretch=true;
dotNetBarManager1.Bars.Add(bar);
bar.DockSide=eDockSide.Top;

ButtonItem menu, submenu;
menu=new ButtonItem("bFile","&File");
bar.Items.Add(menu);
submenu=new ButtonItem("bOpen","&Open");
menu.SubItems.Add(submenu);
submenu=new ButtonItem("bClose","&Close");
menu.SubItems.Add(submenu);
submenu=new ButtonItem("bExit","&Exit");
submenu.BeginGroup=true;
menu.SubItems.Add(submenu);

menu=new ButtonItem("bEdit","&Edit");
bar.Items.Add(menu);
submenu=new ButtonItem("bCut","&Cut");
menu.SubItems.Add(submenu);
submenu=new ButtonItem("bCopy","&Copy");
menu.SubItems.Add(submenu);
submenu=new ButtonItem("bPaste","&Paste");
menu.SubItems.Add(submenu);
submenu=new ButtonItem("bClear","&Clear");
submenu.BeginGroup=true;
menu.SubItems.Add(submenu);
bar.RecalcLayout();

Would you like to...

Print this page Print this page

Email this page Email this page

Post a comment Post a comment

Subscribe me

Add to favoritesAdd to favorites

User Opinions (7 votes)

100% thumbs up 0% thumbs down

How would you rate this answer?

Helpful
Not helpful
Thank you for rating this answer.

Related Questions

No related questions were found.

Attachments

No attachments were found.