[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 749
  • Last Modified:

Tab Page ContextMenuStrip AddHandler

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

            tcWindows.TabPages.Add(tp)






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

    End Sub

Open in new window

0
tonelm54
Asked:
tonelm54
1 Solution
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
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
        tcWindows.TabPages.Add(tp)
    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

0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now