Note that there are some explanatory texts on larger screens.

plurals
  1. PO.Net ToolStripMenuItem - how to access click-event for items generated while form is being loaded (in runtime)
    primarykey
    data
    text
    <p>I have a form which upon its loading events retrieves data and populates some menu-items. I am adding menu-items using </p> <pre><code>Viewer.AudioTrackToolStripMenuItem.DropDownItems.Add(myValue) </code></pre> <p>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.</p> <p>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.</p> <p>Any help would be appreciated. Thank you.</p> <p>Edit: you got me on track but I still got some error-messages. Just to clarify, the code is as follows:</p> <pre><code>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) </code></pre> <p>Now if I run the code I get the following errors:</p> <p>1) 'click' is not a member of 'String'</p> <p>2) Delegate 'System.EventHandler' requires an 'AddressOf' expression or lambda expression as the only argument to its constructor.</p> <p>Could above errors be because my menu-items are named in accordance with a result from a variable (myValue)?</p> <p><strong>edit2: I got everything working by using the following code:</strong></p> <pre><code>Dim menuItem As ToolStripMenuItem myValue = TextBetween(value1, "s|", "|e") menuItem = New ToolStripMenuItem(myValue) Me.AudioTrackToolStripMenuItem.DropDownItems.Add(MenuItem.ToString, Nothing, AddressOf ToolStripMenuItem_Click) </code></pre> <p>then I added the following sub:</p> <pre><code>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 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload