0

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#

3 Answers3

3

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
Victor Zakharov
  • 25,801
  • 18
  • 85
  • 151
  • @ShraavanAcharya: of course, whatever is your planned action (set form focus, according to your question), put it there. – Victor Zakharov Mar 19 '13 at 14:47
  • i have added Private Sub clickhandler(sender As Object, e As EventArgs) inside this block i need to give a condition based on the tmiForm's name but it seems i cant pass any parameter to the funtion – Shraavan Acharya Mar 19 '13 at 14:52
  • @ShraavanAcharya: you can inherit ToolStripMenuItem and create a custom event with a custom click delegate, which would have your form as a parameter. [See this](http://stackoverflow.com/a/15322883/897326). – Victor Zakharov Mar 19 '13 at 14:58
  • @ShraavanAcharya: Actually no, in your case, just examine the `sender`. – Victor Zakharov Mar 19 '13 at 15:01
  • I am sorry i still dont get how it helps me... i am doing a basic project for college and we don't have that much advanced stuff... – Shraavan Acharya Mar 19 '13 at 15:05
  • cant you provide some smaller alternative? – Shraavan Acharya Mar 19 '13 at 15:06
  • For Each Form In My.Application.OpenForms
    If Form.Name = tmiForm.Name Then
    Form.focus()
    End If
    Next

    i want this code to be written. but tmiForm is a variable from Private Sub WindowToolStripMenuItem_DropDownOpening(sender As Object, e As System.EventArgs) Handles WindowToolStripMenuItem.DropDownOpening

    – Shraavan Acharya Mar 19 '13 at 15:11
  • @ShraavanAcharya: forget about that complex method, it's indeed an overkill for your needs. Just use `CType(sender, ToolStripMenuItem)` - see my edit. – Victor Zakharov Mar 19 '13 at 15:12
  • thanks a lot... never knew about ctype... :) and also we are not yet taught about event-handlers in college... sorry this might seem so silly to you :) – Shraavan Acharya Mar 19 '13 at 15:27
0

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
0

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)

Rod
  • 1