all groups > vb.net upgrade > august 2003 >
You're in the

vb.net upgrade

group:

how to build menu dynamically


how to build menu dynamically Prabhat
8/14/2003 3:15:39 AM
vb.net upgrade:
Hi everybody. Can any one help me in the folowing problem?

I have a Treeview control on the form control of vb.net
where nodes and subnodes are there, similar to below:

-Employee
-----Per
-----------E1
-----------E2
-----Fin
-Report
-----R1
-----R2
-----R3
-----------R3.1
-----R4
-Help

The form does not have any menu in design mode. But If I
click on a button then one Menu should be build on the
form same as the treeview structure.

NOTE : The Structure of the treeview is user defined. Mean
the structure can have any number of nodes and subnodes.

Please help.
Thanks in advance for your kind co-operation.

RE: how to build menu dynamically Josh.Moody NO[at]SPAM online.microsoft.com
8/15/2003 9:58:39 PM
if you walk the tree recursively, you can dynamically create the menu.

basically, you want something like this:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim mnu As New MainMenu
Dim tn As TreeNode
Dim mi As MenuItem
For Each tn In TreeView1.Nodes
mi = New MenuItem
mnu.MenuItems.Add(mi)
Tree2Menu(tn, mi)
Next
Me.Menu = mnu
End Sub
Sub Tree2Menu(ByVal tn As TreeNode, ByVal mi As MenuItem)
mi.Text = tn.Text
Dim t2 As TreeNode
Dim m2 As MenuItem
For Each t2 In tn.Nodes
m2 = New MenuItem
mi.MenuItems.Add(m2)
Tree2Menu(t2, m2)
Next
End Sub

Hope that helps.

Josh Moody
VSU Team

--

This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

Note: For the benefit of the community-at-large, all responses to this
message are best directed to the newsgroup/thread from which they
originated.
--------------------
[quoted text, click to view]
how to build menu dynamically Der Meister
8/16/2003 10:13:59 PM
Here is an example of dynamically creating a main menu
and utilizing a context for popup menus.

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 MainMenu1 As
System.Windows.Forms.MainMenu
Friend WithEvents mnuPopup As
System.Windows.Forms.MenuItem
Friend WithEvents mnuItem1 As
System.Windows.Forms.MenuItem
Friend WithEvents mnuItem2 As
System.Windows.Forms.MenuItem
Friend WithEvents mnuItem3 As
System.Windows.Forms.MenuItem
<System.Diagnostics.DebuggerStepThrough()> Private
Sub InitializeComponent()
Me.MainMenu1 = New System.Windows.Forms.MainMenu()
Me.mnuPopup = New System.Windows.Forms.MenuItem()
Me.mnuItem1 = New System.Windows.Forms.MenuItem()
Me.mnuItem2 = New System.Windows.Forms.MenuItem()
Me.mnuItem3 = New System.Windows.Forms.MenuItem()
'
'MainMenu1
'
Me.MainMenu1.MenuItems.AddRange(New
System.Windows.Forms.MenuItem() {Me.mnuPopup})
'
'mnuPopup
'
Me.mnuPopup.Index = 0
Me.mnuPopup.MenuItems.AddRange(New
System.Windows.Forms.MenuItem() {Me.mnuItem1,
Me.mnuItem2})
Me.mnuPopup.Text = "Popup Menu"
Me.mnuPopup.Visible = False
'
'mnuItem1
'
Me.mnuItem1.Index = 0
Me.mnuItem1.Text = "Menu1"
'
'mnuItem2
'
Me.mnuItem2.Index = 1
Me.mnuItem2.MenuItems.AddRange(New
System.Windows.Forms.MenuItem() {Me.mnuItem3})
Me.mnuItem2.Text = "Menu2"
'
'mnuItem3
'
Me.mnuItem3.Index = 0
Me.mnuItem3.Text = "Menu3"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5,
13)
Me.ClientSize = New System.Drawing.Size(520, 278)
Me.Menu = Me.MainMenu1
Me.Name = "Form1"
Me.Text = "Form1"

End Sub

#End Region

Private myContextMenu = New ContextMenu()

Private Sub Form1_MouseDown(ByVal sender As Object, _
ByVal e As
System.Windows.Forms.MouseEventArgs) _
Handles MyBase.MouseDown
If e.Button = MouseButtons.Right Then
Dim loc As Point

Try
loc.X = e.X
loc.Y = e.Y

mnuPopup.Visible = True
myContextMenu.Show(Me, loc)
mnuPopup.Visible = False

Catch
MsgBox(Err.Description)
End Try
End If
End Sub

Private Sub Form1_Load(ByVal sender As Object, ByVal
e As System.EventArgs) Handles MyBase.Load
AddHandler mnuItem1.Click, AddressOf
Me.menuItem1_Click
AddHandler mnuItem2.Click, AddressOf
Me.menuItem2_Click
AddHandler mnuItem3.Click, AddressOf
Me.menuItem3_Click

' Create a main menu object.
Dim mainMenu1 As New MainMenu()

' Create empty menu item objects.
Dim menuItem1 As New MenuItem()
Dim menuItem2 As New MenuItem()

' Set the caption of the menu items.
menuItem1.Text = "&File"
menuItem2.Text = "&Edit"

' Add the menu items to the main menu.
mainMenu1.MenuItems.Add(menuItem1)
mainMenu1.MenuItems.Add(menuItem2)

' Add functionality to the menu items using the
Click event.
AddHandler menuItem1.Click, AddressOf
Me.menuItem1_Click
AddHandler menuItem2.Click, AddressOf
Me.menuItem2_Click

' Assign mainMenu1 to the form.
Me.Menu = mainMenu1

myContextMenu.MenuItems.Add(mnuPopup)
Me.ContextMenu = myContextMenu
End Sub

Private Sub menuItem1_Click(ByVal sender As Object,
ByVal e As System.EventArgs)
MessageBox.Show("You clicked the Menu #1.", "The
Event Information")
End Sub 'menuItem1_Click

Private Sub menuItem2_Click(ByVal sender As Object,
ByVal e As System.EventArgs)
MessageBox.Show("You clicked the Menu #2.", "The
Event Information")
End Sub

Private Sub menuItem3_Click(ByVal sender As Object,
ByVal e As System.EventArgs)
MessageBox.Show("You clicked the last
menu.", "The Event Information")
End Sub
End Class


[quoted text, click to view]
AddThis Social Bookmark Button