Dynamic Menus + Click Event

J

jonmiller1125

Hello,

I am trying to create a contextmenustrip with drop downs dynamically.
I have everything visually working. Each menu appears across the
screen as well as each of their corresponding sub (drop down) menus.
My problems has become that I need to put some sort of onclick event
on these drop down items so that they actually do something. I can't
for the life of me figure out how to do this. Any advice? Please
help ! I have posted my code below.

Private fruitContextMenuStrip(30) As ContextMenuStrip
Private fruitToolStripDropDownButton(30) As
ToolStripDropDownButton


Private Sub Form3_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the
'DataSet11.tblProcessStepType' table. You can move, or remove it, as
needed.

Me.TblProcessStepTypeTableAdapter.Fill(Me.DataSet11.tblProcessStepType)
'TODO: This line of code loads data into the
'DataSet11.tblProcessStep' table. You can move, or remove it, as
needed.

Me.TblProcessStepTableAdapter.Fill(Me.DataSet11.tblProcessStep)


' This code example demonstrates how to handle the Opening
event.
' It also demonstrates dynamic item addition and dynamic
' SourceControl determination with reuse.

Dim strMenu1(30) As String
Dim i As Integer
Dim dr As DataRow
Dim dr2 As DataRow

For Each dr In Me.DataSet11.tblProcessStep.Rows
dr2 = Me.DataSet11.Tables.Item("tblProcessStep").Rows(i)
strMenu1(i) = dr2("ProcessStep")
i = i + 1
Next

For i = 0 To UBound(strMenu1)
If strMenu1(i) <> "" Then
fruitContextMenuStrip(i) = New ContextMenuStrip()
End If
Next

' Create a new ContextMenuStrip control.
'fruitContextMenuStrip = New ContextMenuStrip()

' Attach an event handler for the
' ContextMenuStrip control's Opening event.
For i = 0 To UBound(strMenu1)
If strMenu1(i) <> "" Then
AddHandler fruitContextMenuStrip(i).Opening, AddressOf
cms_Opening
End If
Next
' Create a new ToolStrip control.
Dim ts As New ToolStrip()

' Create a ToolStripDropDownButton control and add it
' to the ToolStrip control's Items collections.

For i = 0 To UBound(strMenu1)
If strMenu1(i) <> "" Then
fruitToolStripDropDownButton(i) = New
ToolStripDropDownButton(strMenu1(i), Nothing, Nothing, strMenu1(i))
End If
Next

For i = 0 To UBound(strMenu1)
If strMenu1(i) <> "" Then
ts.Items.Add(fruitToolStripDropDownButton(i))
End If
Next

' Dock the ToolStrip control to the top of the form.
ts.Dock = DockStyle.Top

' Assign the ContextMenuStrip control as the
' ToolStripDropDownButton control's DropDown menu.
For i = 0 To UBound(strMenu1)
If strMenu1(i) <> "" Then
fruitToolStripDropDownButton(i).DropDown =
fruitContextMenuStrip(i)
End If
Next

' Add the ToolStrip control to the Controls collection.
Me.Controls.Add(ts)
End Sub

Sub cms_Opening(ByVal sender As Object, ByVal e As
System.ComponentModel.CancelEventArgs)

Dim strMenu(2, 30) As String
Dim strMenu2(2, 30) As String
Dim i As Integer
Dim j As Integer
Dim dr As DataRow
Dim dr2 As DataRow

i = 0
For Each dr In Me.DataSet11.tblProcessStep.Rows
dr2 = Me.DataSet11.Tables.Item("tblProcessStep").Rows(i)
strMenu(0, i) = dr2("ProcessStep")
strMenu(1, i) = dr2("P_ProcessStepID")
i = i + 1
Next
i = 0

For Each dr In Me.DataSet11.tblProcessStepType.Rows
dr2 =
Me.DataSet11.Tables.Item("tblProcessStepType").Rows(i)
strMenu2(0, i) = dr2("ProcessStepType")
strMenu2(1, i) = dr2("F_ProcessStepID")
i = i + 1
Next


For i = 0 To UBound(strMenu, 2)
If strMenu(0, i) <> "" Then
' Acquire references to the owning control and item.
Dim c As Control =
fruitContextMenuStrip(i).SourceControl
Dim tsi As ToolStripDropDownItem =
fruitContextMenuStrip(i).OwnerItem

' Clear the ContextMenuStrip control's
' Items collection.
fruitContextMenuStrip(i).Items.Clear()

' Populate the ContextMenuStrip control with its
default items.
For j = 0 To UBound(strMenu2, 2)
If strMenu(1, i) = strMenu2(1, j) Then
fruitContextMenuStrip(i).Items.Add(strMenu2(0,
j))
End If
Next

End If
Next

' Set Cancel to false.
' It is optimized to true based on empty entry.
e.Cancel = False
End Sub
 
J

Jack Jackson

You need to handle a click event using AddHandler. There are two ways
to do this.

You can add a handler for the Click event of each item you add to the
ToolStrip, or you can add a handler for the ToolStrip's ItemClicked
event.

In either case you can use a single handler routine, and use the
sender and event arguments to determine what was clicked.
 
J

jonmiller1125

You need to handle a click event using AddHandler. There are two ways
to do this.

You can add a handler for the Click event of each item you add to the
ToolStrip, or you can add a handler for the ToolStrip's ItemClicked
event.

In either case you can use a single handler routine, and use the
sender and event arguments to determine what was clicked.

Thanks, I ended up having a handler for each item. I have 1 more
thing to get though. How would I return the text of the parent menu?

Thanks again!
 
J

Jack Jackson

Thanks, I ended up having a handler for each item. I have 1 more
thing to get though. How would I return the text of the parent menu?

Thanks again!

Look at the Parent, Owner and OwnerItem properties to see if one of
those gives you the parent.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top