Meowbits
Meowbits

Reputation: 586

VB.net - Derived class from MenuStrip

What I am trying to do is make a custom MenuStrip control with several items (Main Menu, Log Out, Exit, etc...) already attached. There would be methods to handle items being clicked. I think this would save me a some redundant code in the long run and I might learn a little something too.

The end product would basically be a custom MenuStrip control that I can throw on my forms and already have the functionality for the items within it.

So my question is, can this be done? I am a novice but if it can be done and is actually a good idea, then I want give it a shot.

Errors abound but this is what I was thinking...

Public Class MenuStripCustom
    Inherits MenuStrip

    Add MenuItem(MainMenuToolStripMenuItem)
    MainMenuToolStripMenuItem.Text = Main Menu

        Protected Sub MainMenuNav(e As System.EventArgs) _
            Handles MyBase.MainMenuToolStripMenuItem.Click
        MainMenu.Visible = True
        Me.Close()
    End Sub

End Class

Thanks!

Upvotes: 1

Views: 1043

Answers (1)

sj1900
sj1900

Reputation: 412

Yes, can be done no problems. Just create a new user control, and make it inherit from MenuStrip. Then put in code similar to the below for a user control called "UserControl1".

Public Class UserControl1
    Inherits MenuStrip

    Private WithEvents NavToolStrip As New ToolStripMenuItem("Nav")

    Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        Dim tsi As New ToolStripMenuItem
        Me.Items.Add(NavToolStrip)
    End Sub

    Private Sub NavToolStrip_Click(sender As Object, e As EventArgs) Handles NavToolStrip.Click
        MsgBox("Nav clicked")
    End Sub
End Class

Compile the code then you will be able to drag "UserControl1" from your toolbox onto your form.

Upvotes: 3

Related Questions