all groups > dotnet windows forms > february 2008 >
You're in the dotnet windows forms group:
Dynamic Menus + Click Event
dotnet windows forms:
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
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. On Wed, 27 Feb 2008 19:37:09 -0800 (PST), jonmiller1125@gmail.com [quoted text, click to view] wrote: >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
[quoted text, click to view] On Feb 27, 11:42 pm, Jack Jackson <jjack...@cinnovations.net> wrote: > 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. > > On Wed, 27 Feb 2008 19:37:09 -0800 (PST), jonmiller1...@gmail.com > wrote: > > >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
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?
On Thu, 28 Feb 2008 17:37:57 -0800 (PST), jonmiller1125@gmail.com [quoted text, click to view] wrote: >On Feb 27, 11:42 pm, Jack Jackson <jjack...@cinnovations.net> wrote: >> 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. >> >> On Wed, 27 Feb 2008 19:37:09 -0800 (PST), jonmiller1...@gmail.com >> wrote: >> >> >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 > >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
Don't see what you're looking for? Try a search.
|
|
|