Tab Page ContextMenuStrip AddHandler

Posted on 2009-12-30
Last Modified: 2012-05-08
Good evening,
Im trying to add a ContextMenu strip to a tab page which adds different items, depending on the tab created.

I have 2 problems, the first is when I run the line 'tp.ContextMenuStrip.Items.Add("Close this")' I get 'Object reference not set to an instance of an object.'

And if I set it as a context menu and add a static menu (not what I wanna do), my handeler doesnt work.

Any ideas?
Dim tp As New TabPage

            tp.Text = "Transactions for '" & e.ClickedItem.Text & "'"

            tp.ContextMenuStrip.Items.Add("Close this")

            tp.ContextMenuStrip.Items.Add("Close all but this")

            tp.ContextMenuStrip.Items.Add("Close all")

            AddHandler tp.ContextMenuStrip.Click, AddressOf tabContextMenuStripClick


    Private Sub tabContextMenuStripClick(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

Open in new window

Question by:tonelm54
    1 Comment
    LVL 85

    Accepted Solution

    You need to create a ContextMenuStrip, populate it, and then assign it to the TabPage's ContextMenuStrip Property.

    You'll have to use AddHandler() to wire up EACH of the MenuItems so the ContextMenu is actually useful:
    Public Class Form1
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim tp As New TabPage
            tp.Text = "Transactions for '" & e.ClickedItem.Text & "'"
            Dim cms As New ContextMenuStrip
            Dim tsmi As ToolStripMenuItem
            tsmi = cms.Items.Add("Close this")
            AddHandler tsmi.Click, AddressOf tsmi_Click
            tsmi = cms.Items.Add("Close all but this")
            AddHandler tsmi.Click, AddressOf tsmi_Click
            tsmi = cms.Items.Add("Close all")
            AddHandler tsmi.Click, AddressOf tsmi_Click
            tp.ContextMenuStrip = cms
        End Sub
        Private Sub tsmi_Click(ByVal sender As Object, ByVal e As System.EventArgs)
            Dim tsmi As ToolStripMenuItem = CType(sender, ToolStripMenuItem)
            MessageBox.Show(tsmi.Text, "Selection Made")
        End Sub
    End Class

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
    If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    Here's a very brief overview of the methods PRTG Network Monitor ( offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now