Question

VB.NET
On the opening of a menu item (i.e. the top-level menu item), i have added ToolStripMenuItem (i.e. DropDownItem) to the menu item at runtime.

The ToolStripMenuItems added by me during runtime are the names of the forms active in the current project.

Whenever the ToolStripMenuItem with a particular form name is clicked, the form should be given focus.

How can i execute the desired code for the event of a dynamically added ToolStripMenuItem?

Private Sub WindowToolStripMenuItem_DropDownOpening(sender As Object, e As System.EventArgs) Handles WindowToolStripMenuItem.DropDownOpening
        WindowToolStripMenuItem.DropDown.Items.Clear()

        For Each Form In My.Application.OpenForms
            If Not Form.name = frmLogin.Name And Not Form.name = Me.Name Then
                Dim tmiForm = New ToolStripMenuItem()
                tmiForm.Name = Form.name
                tmiForm.Text = Form.text
                WindowToolStripMenuItem.DropDownItems.Add(tmiForm)
            End If
        Next

    End Sub


i want to give focus to a form based on the tmiForm's click event...
i tried searching on the web i only got results for C#

Was it helpful?

Solution 2

try this-

Private Sub clickeventhandler(sender As Object, e As EventArgs)
    For Each Form In My.Application.OpenForms
        If CType(sender, ToolStripMenuItem).Name = Form.Name Then
            Form.Focus()
            Exit Sub
        End If
    Next
End Sub

your previous code seems fine just add a single line.
After

WindowToolStripMenuItem.DropDownItems.Add(tmiForm)

write this-

AddHandler tmiForm.Click, AddressOf clickeventhandler

OTHER TIPS

Use AddHandler:

AddHandler tmiForm.Click, AddressOf ClickHandler

Here is how you can write your ClickHandler:

Public Sub ClickHandler(ByVal sender As Object, ByVal e As EventArgs)
  'for a condition based on a ToolStripMenuItem that fired it
  'If CType(sender, ToolStripMenuItem).Name ...
End Sub

I used a simpler approach. When you click on the menustrip icon, a small arrow appears in the upper-right-hand portion of the window. Click the arrow to open a menu properties window. You can set the visible properties to control what will be seen on the initial menu. You can also set or clear the visible attribute in code:

    Public Sub MenuManage(Wayside As Integer, Vehicle As Integer, _
    System As Integer, Tools As Integer, Reports As Integer, _
    Edit As Integer, Zoom As Integer)

    Main.WaysideToolStripMenuItem.Visible = Wayside
    Main.VehicleToolStripMenuItem.Visible = Vehicle
    Main.SystemToolStripMenuItem.Visible = System
    Main.ToolsToolStripMenuItem1.Visible = Tools
    Main.ReportsToolStripMenuItem.Visible = Reports
    Main.EditToolStripMenuItem.Visible = Edit
    Main.ZoomToolStripMenuItem.Visible = Zoom

    End Sub

Within the Load and FormClosed event code, control what is seen on the menu:

Call MenuManage(True, True, True, True, True, False, False)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top