Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Dynamic menu and submenu creation
Message
De
06/03/2013 16:36:27
 
 
À
Tous
Information générale
Forum:
ASP.NET
Catégorie:
Formulaires
Titre:
Dynamic menu and submenu creation
Versions des environnements
Environment:
VB 9.0
OS:
Windows 7
Divers
Thread ID:
01567663
Message ID:
01567663
Vues:
34
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
Fil
Voir

Click here to load this message in the networking platform