Information générale
Titre:
Dynamic menu and submenu creation
Versions des environnements
Hello All,
I am very New to VB.NET and am trying the create a menu and submenu during runtime, please see the code below:
I think I got the menu creation part down but not the submenu part, can someone please help me with this!
You might be asking why I didn't use the form controls, I did it this way because I plan on putting the following code in a class
Option Explicit On
Public Class Form1
Private mbMenuValid As Boolean
Private mbSubMenuValid As Boolean
Private moContextMenu As System.Windows.Forms.ContextMenu
Public Sub AddItem(ByVal sItemName As String, ByVal bBeEnabled As Boolean, ByVal bShowCheckmark As Boolean, ByVal bBeChecked As Boolean)
Dim oContextMenu As System.Windows.Forms.MenuItem
If (Not mbMenuValid) Then
moContextMenu = New System.Windows.Forms.ContextMenu
mbMenuValid = True
End If
oContextMenu = moContextMenu.MenuItems.Add(sItemName)
oContextMenu.Enabled = bBeEnabled
oContextMenu.Checked = bBeChecked And bShowCheckmark
End Sub
Public Sub AddSeparator()
If (Not mbMenuValid) Then
moContextMenu = New System.Windows.Forms.ContextMenu
mbMenuValid = True
End If
moContextMenu.MenuItems.Add("-")
End Sub
'================================================================================================
This is the routine that is giving me trouble:
Public Sub AddSubMenu(ByVal sItemName As String, ByVal sSubItemName As String, ByVal bBeEnabled As Boolean, ByVal bShowCheckmark As Boolean, ByVal bBeChecked As Boolean)
Dim oContextMenu As System.Windows.Forms.MenuItem
Dim oSubContextMenu As System.Windows.Forms.ContextMenu
oSubContextMenu = New System.Windows.Forms.ContextMenu
oContextMenu = oSubContextMenu.MenuItems.Add(sSubItemName)
moContextMenu.MenuItems.Add(sItemName, oContextMenu)
End Sub
'================================================================================================
Public Function DisplayPopUp(oControl As Object) As Boolean
Dim oCursor As System.Drawing.Point
If (mbMenuValid) Then
oCursor = New System.Drawing.Point
moContextMenu.Show(oControl, oCursor)
Else
Call MsgBox("Error - Popup Menu Called With No Menu Defined!")
End If
DisplayPopUp = mbMenuValid
End Function
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
AddItem("1", True, True, True)
AddSeparator()
AddItem("2", True, True, True)
AddSeparator()
AddItem("3", True, True, True)
AddSeparator()
AddItem("4", True, True, True)
AddSeparator()
AddItem("5", True, True, True)
AddSeparator()
AddSubMenu("1", "a", True, False, False)
End Sub
Private Sub Form1_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseDown
If (e.Button = Windows.Forms.MouseButtons.Right) Then
DisplayPopUp(Me)
End If
End Sub
Private Sub TextBox1_MouseDown(sender As Object, e As System.Windows.Forms.MouseEventArgs) Handles TextBox1.MouseDown
If (e.Button = Windows.Forms.MouseButtons.Right) Then
DisplayPopUp(TextBox1)
End If
End Sub
End Class
Répondre
Voir le fil de ce thread
Voir le fil de ce thread à partir de ce message seulement
Voir tous les messages de ce thread
Voir tous les messages de ce thread à partir de ce message seulement