Question Determine selected item from context menu

Tailorware

New member
Joined
Feb 12, 2012
Messages
2
Programming Experience
5-10
I want to add a context menu to a datagrid in VB2008 and I need to find out which menu item the user selected.

I don't want to use the ContextMenuStrip that's available in the toolbox since it doesn't allow for dynamic placement depending on what row is active in the grid (I haven't found a Top and Left property in the ContextMenuStrip)

Here's what I've got so far, I'm aware of the fact that the MsgBox uses a non-existing property, it is meant to show my intention.

Given the available properties in the ContextMenu object, I would think that there would be something like a SelectedItemCaption property but a search of the object tree yields no results. What should I use?

Thanks in advance,
Henk


VB.NET:
dim grdContextMenu as ContextMenu
grdContextMenu = New ContextMenu
grdContextMenu.MenuItems.Add("Insert")
grdcontextMenu.MenuItems.Add("Update")
grdcontextMenu.MenuItems.Add("Delete")
If e.Button = Windows.Forms.MouseButtons.Right Then
   grdcontextMenu.Show(grdCursisten, New Point(MousePosition.X - 25, MousePosition.Y - 50))
   MsgBox(grdcontextMenu.SelectedItemCaption)
End If
 
First things first, you should not be using the ContextMenu class. Along with the ToolBar and MainMenu classes, it is old news. Since .NET 2.0, you should be preferentially using the ToolStrip, MenuStrip and ContextMenuStrip classes, which are the ones you'll find in the ToolBox.

In fact, you really should be adding the menu to your form in the designer rather than in code. You can add all the items and create a separate Click event handler for each one, so there's no need to try to work out which one was clicked in code. Also, you shouldn't be using code to position and show the menu. When you add the menu to the form in the designer, you can also assign it to the ContextMenuStrip property of the form and/or specific controls in the designer. The menu will then be automatically displayed when and where you right-click.
 
First things first, you should not be using the ContextMenu class. Along with the ToolBar and MainMenu classes, it is old news. Since .NET 2.0, you should be preferentially using the ToolStrip, MenuStrip and ContextMenuStrip classes, which are the ones you'll find in the ToolBox.

In fact, you really should be adding the menu to your form in the designer rather than in code. You can add all the items and create a separate Click event handler for each one, so there's no need to try to work out which one was clicked in code. Also, you shouldn't be using code to position and show the menu. When you add the menu to the form in the designer, you can also assign it to the ContextMenuStrip property of the form and/or specific controls in the designer. The menu will then be automatically displayed when and where you right-click.

I see. Two more questions, if I may.

As you may have guessed from the fact that I'm using an oldnews thing, I don't have that much experience in .NET. In fact, the project I'm working on is my first major attempt in .NET How can I make sure that a contextmenu (I'll follow your advice and use the ContextMenuStrip) pops up at row X, column Y of a populated grid?

As to event handlers, can you recommend a primer on event handler creation? I'm currently using an ugly way to handle MouseClick events. On entering the textbox, I select the entire text in this way:

VB.NET:
Private Sub txtFirstName_MouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles txtFirstName.MouseClick, txtLastName.MouseClick, txtAddress_MouseClick

        Select Case LCase(Me.ActiveControl.Name)
            Case "txtFirstName"
                txtCursistAchternaam.SelectAll()
            Case "txtLastName"
                txtLastName.SelectAll()
            Case "txtAddress"
                txtAddress.SelectAll()
            EndCase
         End Select

 End Sub
 
How can I make sure that a contextmenu (I'll follow your advice and use the ContextMenuStrip) pops up at row X, column Y of a populated grid?
Context menus shows where user right click, or if programmatically displayed with the Show method.
I'm currently using an ugly way to handle MouseClick events
sender parameter is the event source:
CType(sender, TextBox).SelectAll()
 
Back
Top