Hi,
How do I create Menu at runtime in VB? Is this possible to create menu at runtime?
Regards!
Printable View
Hi,
How do I create Menu at runtime in VB? Is this possible to create menu at runtime?
Regards!
VB's Menu editor is a great way to create, at designtime, a menu for your project. Often, however, you may want to add menu items at runtime. Fortunately, Visual Basic makes it easy to do so. In essence, VB menu items are nothing more than special control arrays. And just as with any control array, if you want to dynamically add menu items at runtime, you must place at least one control on your project at designtime. Once you do so, you can then use the Load command to create new objects. The following code shows an example of how this might work. For this example, I have added an invisible menu item named mnuShortCut to a form, and then added one submenu item to it, named mnuSubItem. I gave this submenu item an index of 0 and set its Caption to "Menu Item 1".
Notice that to display the shortcut menu, the code initiates the PopupMenu method during the form's MouseDown event. After testing to see if the right mouse button was pushed, the code then calls the shortcut menu, which our code created in the Load() event.Code:Private Sub Form_Load()
Dim x As Integer
For x = 1 To 4
Load mnuSubItem(x)
mnuSubItem(x).Caption = "Menu Item " & x + 1
mnuSubItem(x).Visible = True
Next x
End Sub
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, x As Single, Y As Single)
If Button = vbRightButton Then
Me.PopupMenu mnuShortCut
End If
End Sub
Private Sub mnuSubItem_Click(Index As Integer)
MsgBox Index
End Sub
Create a menu at design time with Index 0. At run time use the Load statement to create new instances of the menu.
Use Unload to remove menu items.Code:Private Sub CmdAdd_Click()
Dim new_index As Integer
' Load the new menu item.
new_index = mnuFileList.UBound + 1
Load mnuFileList(new_index)
mnuFileList(new_index).Caption = CaptionText.Text
mnuFileList(new_index).Visible = True
CaptionText.Text = ""
' Make the separator visible.
mnuFileSep.Visible = True
End Sub
Code:Private Sub CmdRemove_Click()
Dim txt As String
Dim ctl As Menu
' Find the menu item with this caption.
txt = CaptionText.Text
CaptionText.Text = ""
For Each ctl In mnuFileList
If ctl.Caption = txt Then Unload ctl
Next ctl
' Make the separator visible.
If mnuFileList.Count < 2 Then mnuFileSep.Visible = False
End Sub
At runtime to add a new menu to the menu control array you simply use the command: Load mnuFavsLinks(index) where index is the next number in the control array. For example I want three favorites under my Favorites menu. I want them to show up as Favorite 1, Favorite 2, and Favorite 3. This is the code I would use in the form load event:
Code:
1. Private Sub Form_Load()
2. mnuFavLinks(0).caption = "Favorite 1"
3. Load mnuFavLinks(1)
4. Load mnuFavLinks(2)
5. mnuFavLinks(1).Caption = "Favorite 2"
6. mnuFavLinks(2).Caption = "Favorite 3"
7. End Sub
Find where you want to append menu sub-items, then set that item's index property to 0 from the menu editor. Then, simply use the Load statement to add new menu items. You can traverse the newly added sub-items just like you would any other dynamically added items to a control array.
Code:Load mnuFile(1)
mnuFile(1).Caption = "I've just been added"
Code:Imports System
Imports System.Drawing
Imports System.Reflection
Imports System.Windows.Forms
Imports System.IO
public class MainClass
Shared Sub Main()
Dim form1 As Form = New Form1
Application.Run(form1)
End Sub
End Class
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.TextBox1 = New System.Windows.Forms.TextBox()
Me.SuspendLayout()
'
'TextBox1
'
Me.TextBox1.Font = New System.Drawing.Font("Verdana", 9.0!, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, CType(0, Byte))
Me.TextBox1.Location = New System.Drawing.Point(4, 6)
Me.TextBox1.Multiline = True
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.ScrollBars = System.Windows.Forms.ScrollBars.Vertical
Me.TextBox1.Size = New System.Drawing.Size(272, 186)
Me.TextBox1.TabIndex = 1
Me.TextBox1.Text = ""
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(280, 257)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.TextBox1})
Me.Name = "Form1"
Me.Text = "Menu Demo"
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim menu As New MainMenu()
Dim item As MenuItem
item = New MenuItem("File")
item.MenuItems.Add("New", New System.EventHandler(AddressOf Me.MenuClick))
item.MenuItems.Add("Open", New System.EventHandler(AddressOf Me.MenuClick))
item.MenuItems.Add("Exit")
menu.MenuItems.Add(item)
item = New MenuItem("Edit")
item.MenuItems.Add("Copy", New System.EventHandler(AddressOf Me.EditCopy))
item.MenuItems.Add("Cut", New System.EventHandler(AddressOf Me.EditCut))
item.MenuItems.Add("Paste", New System.EventHandler(AddressOf Me.EditPaste))
menu.MenuItems.Add(item)
item = New MenuItem("Format")
item.MenuItems.Add("Font")
item.MenuItems(0).MenuItems.Add("Verdana", New System.EventHandler(AddressOf Me.MenuClick))
item.MenuItems(0).MenuItems.Add("Tahoma", New System.EventHandler(AddressOf Me.MenuClick))
item.MenuItems(0).MenuItems.Add("Georgia", New System.EventHandler(AddressOf Me.MenuClick))
item.MenuItems.Add("Color", New System.EventHandler(AddressOf Me.MenuClick))
item.MenuItems.Add("Style", New System.EventHandler(AddressOf Me.MenuClick))
menu.MenuItems.Add(item)
Me.Menu = menu
Dim cMenu As New ContextMenu()
cMenu.MenuItems.Add("Copy", New System.EventHandler(AddressOf Me.EditCopy))
cMenu.MenuItems.Add("Cut", New System.EventHandler(AddressOf Me.EditCut))
cMenu.MenuItems.Add("Paste", New System.EventHandler(AddressOf Me.EditPaste))
Me.TextBox1.ContextMenu = cMenu
End Sub
Sub MenuClick(ByVal sender As Object, ByVal e As System.EventArgs)
MessageBox.Show("You selected the command " & sender.text & " of the File menu")
End Sub
Sub EditCopy(ByVal sender As Object, ByVal e As System.EventArgs)
MessageBox.Show(sender.Text)
End Sub
Sub EditCut(ByVal sender As Object, ByVal e As System.EventArgs)
MessageBox.Show(sender.Text)
End Sub
Sub EditPaste(ByVal sender As Object, ByVal e As System.EventArgs)
MessageBox.Show(sender.Text)
End Sub
Sub FormatFont(ByVal sender As Object, ByVal e As System.EventArgs)
MessageBox.Show(sender.Text)
End Sub
End Class