views:

1176

answers:

1

I have a DataGridView in a VB.NET app that I have limited to cell selection only. The control has two columns, the first is not editable, the second is editable. I have a ContextMenuStrip that provides some additional functionality and I am able to make it appear when an editable cell receives a right click and is not in edit mode. Based on an example in a Microsoft forum, I am able to now show the context menu when an editable cell receives a right click while also in edit mode. That code is as follows:

Private Sub DataGridView1_EditingControlShowing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
    If TypeOf e.Control Is TextBox Then
        With DirectCast(e.Control, TextBox)
            .ContextMenuStrip = ContextMenuStrip1
        End With
    End If
End Sub

This, however, completely overwrites the system context menu of the editing control. How can I merge my Context Menu Strip (ContextMenuStrip1) with the system context menu?

For further information, I am using VS2008, but targeting the .NET 2.0 platform.

A: 

In the interest of short-term simplicity, what I have done is created a new context menu that contains similar menu items as the existing system-provided context menu with my additionally needed menu items. The Textbox control contains the functionality that each menu item initially provided, so I just make the necessary calls in the event handlers for each menu item.

The problem with this approach is that if Microsoft changes the system-provided menu in the future, I will have to add more menu items, more event handlers, and more disabling/enabling code. Long-term, it would be best to find the handle of the context menu and merge in my custom menu items.

Tom