Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 27-02-2009
Member
 
Join Date: Jan 2009
Posts: 38
How do I create Menu at runtime in VB?

Hi,

How do I create Menu at runtime in VB? Is this possible to create menu at runtime?

Regards!
Reply With Quote
  #2  
Old 27-02-2009
Member
 
Join Date: May 2008
Posts: 2,293
Re: How do I create Menu at runtime in VB?

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".

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
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.
Reply With Quote
  #3  
Old 27-02-2009
Member
 
Join Date: Apr 2008
Posts: 1,948
Re: How do I create Menu at runtime in VB?

Create a menu at design time with Index 0. At run time use the Load statement to create new instances of the menu.

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
Use Unload to remove menu items.

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
Reply With Quote
  #4  
Old 27-02-2009
Member
 
Join Date: May 2008
Posts: 35
Re: How do I create Menu at runtime in VB?

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
Reply With Quote
  #5  
Old 27-02-2009
Member
 
Join Date: Feb 2008
Posts: 1,848
Re: How do I create Menu at runtime in VB?

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"
Reply With Quote
  #6  
Old 27-02-2009
Member
 
Join Date: Jan 2008
Posts: 1,515
Re: How do I create Menu at runtime in VB?

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
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "How do I create Menu at runtime in VB?"
Thread Thread Starter Forum Replies Last Post
Runtime error 429: Activex Component Can't create object Hina Naz Vista Help 2 01-06-2010 04:00 AM
How to create horizontal menu using CSS Efigenio Software Development 6 29-03-2010 08:40 PM
How to create Menu using ASP? Solitario Software Development 4 05-02-2010 03:44 AM
How to create Menu Bar in JavaFX T0tal L0$$ Software Development 3 29-07-2009 03:28 PM
Solution for Runtime Error 429 : ActiveX Component Can't Create Object Shell Tips & Tweaks 2 25-12-2008 03:01 PM


All times are GMT +5.5. The time now is 08:09 AM.