vbnet
  1. vbnet-menu-control

Working with Forms and Controls in VB.NET: Menu Control

The Menu control in VB.NET is used to create menus in Windows Forms applications. It allows users to select various options and functionalities in the application.

Syntax

'Menu control declaration
Dim menu As New MainMenu

'Menu item declaration
Dim menuItem1 As New MenuItem

Here, menu is the object of MainMenu class and menuItem1 is the object of MenuItem class. An event handler needs to be added to the menu item object for it to perform any action on selection.

'Event handler for menu item
Private Sub menuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    'code to be executed on selection
End Sub

'Adding event handler to menu item
AddHandler menuItem1.Click, AddressOf Me.menuItem1_Click

Example

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    'Create a new menu object
    Dim menu As New MainMenu

    'Create a new menu item object
    Dim menuItemFile As New MenuItem("File")

    'Create sub-menu items
    Dim menuItemNew As New MenuItem("New")
    Dim menuItemOpen As New MenuItem("Open")
    Dim menuItemSave As New MenuItem("Save")
    Dim menuItemExit As New MenuItem("Exit")

    'Add the menu items to the menu
    menu.MenuItems.Add(menuItemFile)

    'Add the sub-menu items to the file menu
    menuItemFile.MenuItems.Add(menuItemNew)
    menuItemFile.MenuItems.Add(menuItemOpen)
    menuItemFile.MenuItems.Add(menuItemSave)
    menuItemFile.MenuItems.Add(menuItemExit)

    'Add event handlers to the sub-menu items
    AddHandler menuItemNew.Click, AddressOf Me.menuItemNew_Click
    AddHandler menuItemOpen.Click, AddressOf Me.menuItemOpen_Click
    AddHandler menuItemSave.Click, AddressOf Me.menuItemSave_Click
    AddHandler menuItemExit.Click, AddressOf Me.menuItemExit_Click

    'Set the menu of the form
    Me.Menu = menu
End Sub

'Event handlers for the menu items
Private Sub menuItemNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    'Code to create a new file
End Sub

Private Sub menuItemOpen_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    'Code to open a file
End Sub

Private Sub menuItemSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    'Code to save the file
End Sub

Private Sub menuItemExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
    'Code to exit the application
    Me.Close()
End Sub

Output

The output of the above code will be a Windows Form with a menu bar at the top containing a "File" menu with sub-menu items "New", "Open", "Save", and "Exit".

Explanation

In the above example, we have created a new menu object and added a "File" menu to it. We have also created sub-menu items for "New", "Open", "Save", and "Exit" and added them to the "File" menu.

We have then added event handlers to the sub-menu items to perform specific actions on selection. Finally, we have set the menu of the form to the newly created menu object.

Use

The Menu control in VB.NET is used to create menus in Windows Forms applications. It allows users to select various options and functionalities in the application. It is commonly used to provide a standard way to navigate and access different features of the application.

Important Points

  • The Menu control in VB.NET is used to create menus in Windows Forms applications.
  • Menu items can be declared and added to the menu object using the MenuItem class.
  • Menu items can have event handlers to perform specific actions on selection.

Summary

In summary, the Menu control in VB.NET is a powerful tool for creating menus in Windows Forms applications. It provides an easy way to navigate and access different features of the application and allows for customization and event handling.

Published on: