Reputation: 8031
I have the following code:
Private holdAllDataFromFile As New list(Of String)
holdAllDataFromFile = ReadWrite.ReadAll(FILE_PATH) 'Uses custom class to read/write.
For Each item In holdAllDataFromFile.AsEnumerable
menuConnections.DropDownItems.Add(finalData(1).tostring ) 'save to menu
Next
At run time, i wish to be able to click on those menu items and have them respond to events, i'm not sure on how to access them programmatically since there could be 1, 4, 10 different menu items, all with different names.
I was looking at this post similar to mine, but i'm not sure if this is the right way for me to accomplish this. Any thoughts?
Thanks
Upvotes: 1
Views: 2584
Reputation: 81675
Stub out your click event code first:
Private Sub DataFile_Click(ByVal sender As Object, ByVal e As EventArgs)
MessageBox.Show("Clicked on " & DirectCast(sender, ToolStripMenuItem).Text)
End Sub
The "sender" parameter will be the ToolStripMenuItem you added.
Then your loop can be changed to add the event handler:
For Each item In holdAllDataFromFile
menuConnections.DropDownItems.Add(item, _
Nothing, _
AddressOk DataFile_Click)
Next
I changed "finalData(1).tostring" to just "item" since that is your looping variable. It's unclear where that finalData variable is coming from or what that has to do with the loop. I also got rid of AsEnumerable since holdAllDataFromFile is already a List(of String).
Upvotes: 1