Search code examples
vb.netcontextmenuinfragisticsultrawingrid

Show ContextMenuStripItem without clicking off the current cell


I'm using an UltraGrid which has a ContextMenuStrip with 2 items. These are shown when right-clicking an UltraGridCell.

However, in order to show them, the user has to first click off the cell to take it out of edit mode, then right click on it to show the ContextMenuStripItems.

This has become confusing and irritating to the user, so I was wondering if there is any way that it can be changed to show them when right clicking whilst still in edit mode?

I've tried this to take it out of edit mode after a key is pressed, but it doesn't work.

Private Sub ugComm_keyup(sender As Object, e As KeyEventArgs) Handles ugComm.KeyUp

  ugComm.UpdateData()
  If ugComm.ActiveCell.IsInEditMode = True Then
      ugComm.ActiveCell.Row.Update()
  End If

End Sub

I also tried something in the MouseClick that was suggested on the Infragistics forums, but again it didn't work.

Is there any way that a user right-clicking a cell that is in edit mode can bring up the ContextMenuStripItems rather than this menu? enter image description here

The above image shows what is currently show when right-clicking a cell in edit mode (The cell is the bottom right white cell). I don't want this to appear, but the CMS instead.

EDIT

I've tried the suggestions in the current answers, but neither of those worked for me. Possibly because the grids are a slightly older version?

My most recent effort was done with the following code:

Private Sub ugComm_MouseDown(sender As Object, e As MouseEventArgs) Handles ugComm.MouseDown

    If e.Button = Windows.Forms.MouseButtons.Right Then
        Me.cmCommRate.Show(mouseX, mouseY)
    End If
End Sub

But this wasn't triggered until the cell was no longer in edit mode.

NEITHER OF THE ANSWERS BELOW RESOLVE THE ISSUE. STILL NEEDS AN ANSWER


Solution

  • When any cell of the grid enters in edit mode a TextBox is drawn over the cell. The nice part here is this text box is reused for all the cells in the grid. When you right click on the cell in edit mode the default context menu, that comes from MS, shows. What you need to do is get this text box and assign it your context menu strip. You can do this by handling ControlAdded event of the grid like this:

        ' create a field to store the TextBox    
        Private cellTextBox As TextBox
    
        Private Sub grid_ControlAdded(sender As Object, e As ControlEventArgs) Handles grid.ControlAdded
            ' Check if added control is TextBox 
            If TypeOf e.Control Is TextBox Then
                ' If added control is TextBox store it in your private field and set its ContextMenuStrip
                If Me.cellTextBox Is Nothing Then
                    Me.cellTextBox = DirectCast(e.Control, TextBox)
                    Me.cellTextBox.ContextMenuStrip = Me.ctx
                End If
            End If
        End Sub