dealing with control arrays
-
Any help in general would be appreciated, I need to build a control array at run time, so as an example I can do something like this
Private Sub createlblSlotArray()
Dim i As Short
ReDim lblSlot(3)
For i = 1 To 3
lblSlot(i) = New System.Windows.Forms.Label
With lblSlot(i)
.Tag = i
AddHandler .Click, AddressOf Me.lblSlot_click
End With
Next
Me.Controls.AddRange(lblSlot)
lblSlot(1).Location = New System.Drawing.Point(50, 50)
lblSlot(2).Location = New System.Drawing.Point(100, 70)
lblSlot(3).Location = New System.Drawing.Point(100, 90)
End SubI need to add a popup menu to each of these labels, when the user right clicks on a label, I need to know which one the right click was on, set a variable so it can be used later, then show the context menu. would I use the form1_click?
Private Sub Form1_Click(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Label1.MouseDown
' what to do here to get the label? this seems to be tied to only the first label?
End Subcan you show me an example of how to handle events from a control array of this nature, also, if I do not know how many context menu items I will have until run time, so I am not sure how to do that, the popup menu click event appear to be tied to the exact item that was selected.
Private Sub MenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem1.Click
' this is OK for menu item 1, how do I deal with new items I may add at run time?
End Subthanks in advance No-e
-
Any help in general would be appreciated, I need to build a control array at run time, so as an example I can do something like this
Private Sub createlblSlotArray()
Dim i As Short
ReDim lblSlot(3)
For i = 1 To 3
lblSlot(i) = New System.Windows.Forms.Label
With lblSlot(i)
.Tag = i
AddHandler .Click, AddressOf Me.lblSlot_click
End With
Next
Me.Controls.AddRange(lblSlot)
lblSlot(1).Location = New System.Drawing.Point(50, 50)
lblSlot(2).Location = New System.Drawing.Point(100, 70)
lblSlot(3).Location = New System.Drawing.Point(100, 90)
End SubI need to add a popup menu to each of these labels, when the user right clicks on a label, I need to know which one the right click was on, set a variable so it can be used later, then show the context menu. would I use the form1_click?
Private Sub Form1_Click(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Label1.MouseDown
' what to do here to get the label? this seems to be tied to only the first label?
End Subcan you show me an example of how to handle events from a control array of this nature, also, if I do not know how many context menu items I will have until run time, so I am not sure how to do that, the popup menu click event appear to be tied to the exact item that was selected.
Private Sub MenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem1.Click
' this is OK for menu item 1, how do I deal with new items I may add at run time?
End Subthanks in advance No-e
If you are handling the MenuItem click event, then you can get the control name using
MenuItemName.GetContextMenu().SourceControl
. In order to attach a click event to the dynamic menuitem, useMenuitemName.Click += new EventHandler(EventHandlerName)
.जय हिंद
-
If you are handling the MenuItem click event, then you can get the control name using
MenuItemName.GetContextMenu().SourceControl
. In order to attach a click event to the dynamic menuitem, useMenuitemName.Click += new EventHandler(EventHandlerName)
.जय हिंद
-
Thanks, but I not quite following you. When the user selects a menu item from context menu, I how do I determine what item they selected? What event do I look for and how do I grab the item? No-e
-
Any help in general would be appreciated, I need to build a control array at run time, so as an example I can do something like this
Private Sub createlblSlotArray()
Dim i As Short
ReDim lblSlot(3)
For i = 1 To 3
lblSlot(i) = New System.Windows.Forms.Label
With lblSlot(i)
.Tag = i
AddHandler .Click, AddressOf Me.lblSlot_click
End With
Next
Me.Controls.AddRange(lblSlot)
lblSlot(1).Location = New System.Drawing.Point(50, 50)
lblSlot(2).Location = New System.Drawing.Point(100, 70)
lblSlot(3).Location = New System.Drawing.Point(100, 90)
End SubI need to add a popup menu to each of these labels, when the user right clicks on a label, I need to know which one the right click was on, set a variable so it can be used later, then show the context menu. would I use the form1_click?
Private Sub Form1_Click(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Label1.MouseDown
' what to do here to get the label? this seems to be tied to only the first label?
End Subcan you show me an example of how to handle events from a control array of this nature, also, if I do not know how many context menu items I will have until run time, so I am not sure how to do that, the popup menu click event appear to be tied to the exact item that was selected.
Private Sub MenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MenuItem1.Click
' this is OK for menu item 1, how do I deal with new items I may add at run time?
End Subthanks in advance No-e
There are no control arrays in .NET and no need for them. You handle the Click event of the label, not the form. The ending part of the function header of the event handler just needs to be modified so it can handle the Click event of all of your label controls. You can do that by adding the name of each label and it's event name to the Handles clause:
Private Sub Labels\_Click(_blah, blah_) Handles Label1.Click, Label2.Click, Label3.Click
Then, if the handler code, the label that was clicked will be in the sender argument passed to your event handler. Alternatively, you can even skip the Handles clause and wire up the event handlers yourself using AddHandler.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008 -
There are no control arrays in .NET and no need for them. You handle the Click event of the label, not the form. The ending part of the function header of the event handler just needs to be modified so it can handle the Click event of all of your label controls. You can do that by adding the name of each label and it's event name to the Handles clause:
Private Sub Labels\_Click(_blah, blah_) Handles Label1.Click, Label2.Click, Label3.Click
Then, if the handler code, the label that was clicked will be in the sender argument passed to your event handler. Alternatively, you can even skip the Handles clause and wire up the event handlers yourself using AddHandler.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008Dave's one of the brightest minds on the boards here, but I'm going to disagree with him slightly. You can have an array of an labels and setup everything dynamically. Is this the preferred way of doing things the majority of the time? NO. Most of the time you don't need to do this; however, sometimes forms or controls need to built and set dynamically (IE: dynamic menus, reporting forms, etc). If you really need this functionality, create the array of labels and link them with the addhandler. As Dave mentioned, you can have one event that handles all the click events for the controls. Parse on the control name or whatever else you need to do to perform the correct function from within the event that is shared amongst all the label controls.
"There's no such thing as a stupid question, only stupid people." - Mr. Garrison
-
Dave's one of the brightest minds on the boards here, but I'm going to disagree with him slightly. You can have an array of an labels and setup everything dynamically. Is this the preferred way of doing things the majority of the time? NO. Most of the time you don't need to do this; however, sometimes forms or controls need to built and set dynamically (IE: dynamic menus, reporting forms, etc). If you really need this functionality, create the array of labels and link them with the addhandler. As Dave mentioned, you can have one event that handles all the click events for the controls. Parse on the control name or whatever else you need to do to perform the correct function from within the event that is shared amongst all the label controls.
"There's no such thing as a stupid question, only stupid people." - Mr. Garrison
Jon_Boy wrote:
but I'm going to disagree with him slightly.
All I said was there was no need for the control array, not the dynamic creation of controls. When the labels click events are all wired up to the same handler, he'll get the instance that was clicked in the
sender
argument in the handler. I think we're on the same page, but maybe not. I just can't see the difference.A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008 -
Jon_Boy wrote:
but I'm going to disagree with him slightly.
All I said was there was no need for the control array, not the dynamic creation of controls. When the labels click events are all wired up to the same handler, he'll get the instance that was clicked in the
sender
argument in the handler. I think we're on the same page, but maybe not. I just can't see the difference.A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008