1 Attachment(s)
[RESOLVED] Help with ContextMenuStrip on DataGridView
Hello,
I am formerly a VB6 programmer learning the .Net world of VB2008. I am confused and need assistance with the following.
I am attempting to get a ContextMenuStrip to work on a DataGridView. I want my program to add the ContextMenuStrip and fill in its menu items. I would then like a right-click on a menu item to take me to a unique click event to handle the specific menu action.
The example below is a simple form with a datagridview (1) and contextmenustrip (1). I could not get it to work without adding the ContextMenuStrip control manually.
While it works it’s not correct. I'd like help adding the ContextMenuStrip programmatically, associating it with the datagridview and then having unique event handlers for the unique menu items and their associated right click.
Thanks
Re: Help with ContextMenuStrip on DataGridView
Add the ContextMenuStrip to the form in the designer and select it as the value for the DataGridView's ContextMenuStrip property. You can then populate the menu at run time by calling the overload of the Add method that accepts a reference to a Click event handler.
http://msdn.microsoft.com/en-us/library/ms160942.aspx
1 Attachment(s)
Re: Help with ContextMenuStrip on DataGridView
Hello,
My application is such that I have to programmatically add the ContextMenuStrips. I did use the designer to construct a simple DGV and ContextMenuStrip.
I took some of that initialization code and put together another testing program shown below.
I still have problems understanding how to get the code written.
1). My menu items display but they are not restricted to the DGV as I expect.
2). I added the code "CMS.Items.AddRange(New System.Windows.Forms...." but don't know if that is the correct way to add the items.
3). Can one get specific handlers for each of the ToolStripMenuItems (1st, 2nd, 3rd & 4th) ?
Thanks
Re: Help with ContextMenuStrip on DataGridView
Hi Folks,
I sure would appreciate some help. I've searched VBWire and MSDN looking for examples. I'm not yet experienced enough to put it all together.
Thanks Robo
Re: Help with ContextMenuStrip on DataGridView
Ok I think I have the ContextMenuStrip menuitem right clicks figured out. Comments?
I set my ContextMenuStrip = DataGridView.ContextMenuStrip but my menu comes up anywhere on the form. It should be restricted to DGV correct? Any insights?
Thanks
Code:
Public Class Form1
Private CMS As ContextMenuStrip
Private TSM1 As ToolStripMenuItem
Private TSM2 As ToolStripMenuItem
Private TSM3 As ToolStripMenuItem
Private TSM4 As ToolStripMenuItem
Private DGV As DataGridView
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Call CMSLoad()
End Sub
Private Sub CMSLoad()
CMS = New ContextMenuStrip
DGV = New DataGridView
TSM1 = New ToolStripMenuItem
TSM2 = New ToolStripMenuItem
TSM3 = New ToolStripMenuItem
TSM4 = New ToolStripMenuItem
'
CMS.Items.AddRange(New System.Windows.Forms.ToolStripMenuItem() {TSM1, TSM2, TSM3, TSM4})
'
TSM1.Text = "First"
TSM2.Text = "Second"
TSM3.Text = "Third"
TSM4.Text = "Forth"
TSM1.Name = "First"
TSM2.Name = "Second"
TSM3.Name = "Third"
TSM4.Name = "Forth"
'
DGV.ContextMenuStrip = CMS
'
AddHandler TSM1.Click, AddressOf TSM1_Click
AddHandler TSM2.Click, AddressOf TSM2_Click
AddHandler TSM3.Click, AddressOf TSM3_Click
AddHandler TSM4.Click, AddressOf TSM4_Click
'
ContextMenuStrip = CMS
End Sub
Private Sub TSM1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Debug.Print("ToolStripMenu1")
End Sub
Private Sub TSM2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Debug.Print("ToolStripMenu2")
End Sub
Private Sub TSM3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Debug.Print("ToolStripMenu3")
End Sub
Private Sub TSM4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Debug.Print("ToolStripMenu4")
End Sub
End Class
Re: Help with ContextMenuStrip on DataGridView
In the CMSLoad method, this line:
Is assigning the ContextMenuStrip to the form's ContextMenuStrip property. So both the Form and the DataGridView are sharing the same one.
Also, in your form's Dispose method, you should remove the event handlers from the ToolStripItems.
Re: Help with ContextMenuStrip on DataGridView
Thank you John and ForumAccount for your suggestions. I have everything working (code below). The example uses a basic form with no controls.
Code:
Public Class Form1
Private CMS As ContextMenuStrip
Private TSM1 As ToolStripMenuItem
Private TSM2 As ToolStripMenuItem
Private TSM3 As ToolStripMenuItem
Private TSM4 As ToolStripMenuItem
Private DGV As New DataGridView
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.Controls.Add(Me.CMS)
Me.Controls.Add(Me.DGV)
With Me.DGV
.Tag = Text
.Size = New Size(200, 200)
.RowHeadersVisible = False
.RowTemplate.Height = 18
.ColumnCount = 2
.Columns(0).Width = 95
.Columns(1).Width = 95
End With
Call CMSLoad()
End Sub
Private Sub CMSLoad()
CMS = New ContextMenuStrip
'DGV = New DataGridView
TSM1 = New ToolStripMenuItem
TSM2 = New ToolStripMenuItem
TSM3 = New ToolStripMenuItem
TSM4 = New ToolStripMenuItem
'
CMS.Items.AddRange(New System.Windows.Forms.ToolStripMenuItem() {TSM1, TSM2, TSM3, TSM4})
'
TSM1.Text = "First"
TSM2.Text = "Second"
TSM3.Text = "Third"
TSM4.Text = "Forth"
TSM1.Name = "First"
TSM2.Name = "Second"
TSM3.Name = "Third"
TSM4.Name = "Forth"
'
'AddHandler CMS.Click, AddressOf CMS_clicked
AddHandler TSM1.Click, AddressOf TSM1_Click
AddHandler TSM2.Click, AddressOf TSM2_Click
AddHandler TSM3.Click, AddressOf TSM3_Click
AddHandler TSM4.Click, AddressOf TSM4_Click
'
'ContextMenuStrip = CMS
DGV.ContextMenuStrip = CMS
''
End Sub
Private Sub TSM1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Debug.Print("ToolStripMenu1")
End Sub
Private Sub TSM2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Debug.Print("ToolStripMenu2")
End Sub
Private Sub TSM3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Debug.Print("ToolStripMenu3")
End Sub
Private Sub TSM4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Debug.Print("ToolStripMenu4")
End Sub
End Class
Thanks