0

I have a form which upon its loading events retrieves data and populates some menu-items. I am adding menu-items using

Viewer.AudioTrackToolStripMenuItem.DropDownItems.Add(myValue)

The number of menu-items created are not always the same. Sometimes they are 2, other times they are 4 depending on the data being retrieved.

Since the menu-items are created during "run-time", how can I now intercept when a user click/select a menu-item (I mean I don't know the name of the control and its click-event). If the menu-items were known before-hand, I would simply create the menu-item in design-time and I would have my click-event but now I don't.

Any help would be appreciated. Thank you.

Edit: you got me on track but I still got some error-messages. Just to clarify, the code is as follows:

myValue = TextBetween(value1, "s|", "|e") 'myValue is the result of a function
                                          'where myValue is a string
myValue.click += New System.EventHandler(myValue_Click) 'ERROR
Me.AudioTrackToolStripMenuItem.DropDownItems.Add(myValue)

Now if I run the code I get the following errors:

1) 'click' is not a member of 'String'

2) Delegate 'System.EventHandler' requires an 'AddressOf' expression or lambda expression as the only argument to its constructor.

Could above errors be because my menu-items are named in accordance with a result from a variable (myValue)?

edit2: I got everything working by using the following code:

Dim menuItem As ToolStripMenuItem
myValue = TextBetween(value1, "s|", "|e")
menuItem = New ToolStripMenuItem(myValue)
Me.AudioTrackToolStripMenuItem.DropDownItems.Add(MenuItem.ToString, Nothing, AddressOf ToolStripMenuItem_Click)

then I added the following sub:

Private Sub ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim menuItem As ToolStripMenuItem = TryCast(sender, ToolStripMenuItem)
    If menuItem IsNot Nothing Then
          'item just clicked. 
          MessageBox.Show(menuItem.Text)
    End If
End Sub
moster67
  • 830
  • 3
  • 12
  • 30

2 Answers2

3

Define myvalue_click method to handle all myValue clicks. Add event handles programmatically, like:

this.myValue.Click += new System.EventHandler(this.myValue_Click);  
Kamyar
  • 18,639
  • 9
  • 97
  • 171
  • you got me on track but now I got another error. Se my edit - perhaps I was not clear enough. – moster67 Nov 01 '10 at 15:59
  • You should define event for DropDownItems. `this.DropDownItems.Click += new System.EventHandler(this.DropDownItems_Click);` Now in the event handler check which item has been clicked. – Kamyar Nov 01 '10 at 16:17
1

You can add the event at runtime too, e.g..

Since your myValue is a string, you'll first need to create a button for it:

ToolStripItem toolStripItem = new ToolStripItem();

toolStripItem.Text = myValue;
toolStripItem.Click += new EventHandler(toolStripItem_Click);

Viewer.AudioTrackToolStripMenuItem.DropDownItems.Add(toolStripItem);
Pieter van Ginkel
  • 29,160
  • 8
  • 71
  • 111