Results 1 to 3 of 3

Thread: Automating menu code

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2014

    Automating menu code

    Hello everone,

    I use a menu and the following code to open forms when a menu item is clicked :
    If Me.products Is Nothing OrElse Me.products.IsDisposed Then
                Me.products = New Products
                products .MdiParent = Me
                products .Show()
            End If
    The credit goes to :
    Thanks a lot.

    What i want to learn is is it possible to automate the code above or do i have to copy paste the code and change the instance names of forms for all menu items.

    ı managed to change the form name using variable but couldn't manage to change the class name according to a variable.

    Thank you verry much for your help in advance.


  2. #2
    Hyperactive Member
    Join Date
    Sep 2014

    Re: Automating menu code

    Add Each Of your forms to a collection, call them back from the collection, match button name to Key used to collection as shown below

        Private Sub MenuStrip1_ItemClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolStripItemClickedEventArgs) Handles MenuStrip1.ItemClicked
            Dim FormsCollection As New Collection
            FormsCollection.Add(New Home, "Home")
            FormsCollection.Add(New Evaluation_Manager, "Evaluation_Manager")
            Dim FormToShow As Form = FormsCollection(e.ClickedItem.Name)
                FormToShow.MdiParent = Me
        End Sub

  3. #3
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Sydney, Australia

    Re: Automating menu code

    You can write a single method that takes a form as an argument and contains that code and then just call that method from each menu item's Click event handler and pass the appropriate form. You could also write a single event handler that is executed for all menu items and creates a form instance using Reflection based on a Type. The Type could come from an array and could be retrieved by index based on the index of the menu item, e.g. Code:
    1. Public Class Form1
    3.     Private ReadOnly formTypes As Type() = {GetType(Form2), GetType(Form3), GetType(Form4)}
    4.     Private ReadOnly forms(2) As Form
    6.     Private Sub ToolStripMenuItems_Click(sender As Object, e As EventArgs) Handles Form4ToolStripMenuItem.Click,
    7.                                                                                    Form3ToolStripMenuItem.Click,
    8.                                                                                    Form2ToolStripMenuItem.Click
    9.         Dim index = Me.FormsToolStripMenuItem.DropDownItems.IndexOf(DirectCast(sender, ToolStripMenuItem))
    10.         Dim form = forms(index)
    12.         If form Is Nothing OrElse form.IsDisposed Then
    13.             Dim formType = formTypes(index)
    15.             form = DirectCast(Activator.CreateInstance(formType), Form)
    16.             forms(index) = form
    17.             form.MdiParent = Me
    18.             form.Show()
    19.         Else
    20.             form.Activate()
    21.         End If
    22.     End Sub
    24. End Class

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Click Here to Expand Forum to Full Width