**I have to create an application that will dynamically add a menu strip with menu items to a form(ie NOT dragged and dropped onto the form) in Visual Basic. The menu strip should be created in the Form_Load event handler.
I have to use a TextBox for user input. If the user enters information into the TextBox and clicks on the Add Button, the text should be added to a ComboBox control.A Menu should then be created that must be added to the menu strip dynamically.
There should be a second TextBox that will be used to add a new menu item to the menu that is currently selected in the ComboBox.
When a menu item is selected, it must simply display a MessageBox with the items name (For example, adding a menu item called View would display a MessageBox with the message “View”)
This is what I have done so far, I need to know how to add the menu items to the selected Menu from the combobox. Please let me know about any other errors in the coding. Here is what I've got thus far:
**

Public Class Form1
    Private mnuMain As MainMenu
    Private mnuItem As MenuItem

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Declare main menu
        mnuMain = New MainMenu()

        'Add submenu with menu item
        mnuItem = New MenuItem()
        mnuItem.MenuItems.Add(New MenuItem(""))
        Me.Menu = mnuMain

        'Add event-handler to each MenuItems
        For Each m As MenuItem In mnuItem.MenuItems
            AddHandler m.Click, AddressOf HandleMenuItemClick
        Next
    End Sub

    'Event-handler to handle any and all MenuItem clicks
    Private Sub HandleMenuItemClick(ByVal sender As System.Object, ByVal e As System.EventArgs)
        'Throw exception if the sender is not a menu item
        If (Not (TypeOf (sender) Is MenuItem)) Then
            Throw New Exception("HandleMenuItemClick called incorrectly!")
        End If
        'Convert sender to menu item type
        Dim s As MenuItem = CType(sender, MenuItem)
        Select Case s.Text
            'Response will be a messagebox that displays the selected menu item's text
            Case txtMenuItem.Text
                MessageBox.Show(txtMenuItem.Text)
        End Select
    End Sub

    Private Sub btnAddMenu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddMenu.Click
        'Check if text is entered into textbox
        If txtMenu.Text = "" Then
            MessageBox.Show("Please enter text into textbox before pressing the add button")
            txtMenu.Text = ""
            Return
        ElseIf cmbMenu.Items.Contains(txtMenu.Text) Then
            MessageBox.Show("Menu already added")
            txtMenu.Text = ""
        Else

            cmbMenu.Items.Add(txtMenu.Text)
            mnuMain.MenuItems.Add(txtMenu.Text)
            txtMenu.Text = ""
        End If
    End Sub

    Private Sub btnAddMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAddMenuItem.Click
        'Checks if there is text in the textbox
        If txtMenuItem.Text = "" Then
            'Display a message to the user
            MessageBox.Show("Please enter text into text box before pressing the add button")
            txtMenuItem.Text = ""
            Return
        ElseIf cmbMenu.Items.Contains(txtMenuItem) Then
            MessageBox.Show("Item already exists in the dropdownlist")
            txtMenuItem.Text = ""
        Else
            '?????
        End If
    End Sub
End Class

Any help will be much appreciated

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.