Skip to content

Infragistics Community Forum / Desktop / Ultimate UI for Windows Forms / How to display a context menu in an ultra grid

How to display a context menu in an ultra grid

New Discussion
SreeRamaSaran
SreeRamaSaran asked on Jul 8, 2011 3:31 PM

 Hi,

How to display a context menu in an ultra grid if and only if the user clicks on a particular row. And the row or cell should not be header or blank cell/row.

Regards,

SreeRam

Sign In to post a reply

Replies

  • 0
    Mike Saltzman
    Mike Saltzman answered on Aug 27, 2008 2:18 PM

    Hi SreeRam,

    You can't apply a context menu to a part of a control. What you would have to do is apply the context menu to the entire control depending on the location of the mouse. I would use the MouseEnterElement and MouseLeaveElement events of the grid to assign and remove the context menu. You can use the GetContext method on the UIElement being entered to get the row (or null) and either set or remove the context menu on the grid control. 

    • 0
      SreeRamaSaran
      SreeRamaSaran answered on Aug 28, 2008 3:47 PM

       Hi Mike,

      As you said we can't apply a context menu to a part of a control if we choose the "ContextMenuStrip" property for ultragrid control. I tried MouseDown event to assign or remove the context menu. Although the below code works fine if and only if you remove the "ContextMenuStrip" property for ultragrid control. Because it overrides the below code and displays the context menu whereever you click the mouse within the control.

       

      ********************************************************************************

      private void ugQueue_MouseDown(object sender, MouseEventArgs e)
              {
                  if (e.Button == MouseButtons.Right)
                  {
                      Point mousePoint = new Point(e.X, e.Y);
                      UIElement element = ((UltraGrid)sender).DisplayLayout.UIElement.ElementFromPoint(mousePoint);

                      UltraGridCell cell = element.GetContext(typeof(UltraGridCell)) as UltraGridCell;

                      if (cell != null)
                      {
                          if (Convert.ToByte(cell.Row.Cells["Status"].Value) == 0)
                          {
                              cmnuQueue.Show(ugQueue, mousePoint);
                              cell.Row.Selected = true;
                          }
                          else
                              return;
                      }
                  }
              }

      ********************************************************************************

      Thanks Mike.

      With Regards,

      SreeRam. 

      • 0
        Mike Saltzman
        Mike Saltzman answered on Aug 29, 2008 2:35 PM

        That's another good way to do it. I would recommend using MouseUp instead of MouseDown, though. If you try this with any Windows application, you can see that context menus are always display in the MouseUp. Displaying a context menu in MouseDown can cause problems because the control gets a MouseDown, but the MouseUp message is lost. 

      • 0
        Donald Bouchard
        Donald Bouchard answered on Jul 7, 2011 7:11 PM

        I would like to know if I right-clicked on the cell header.  How do I do this?

        My current code:

            Private Sub ugCommission_MouseUp(sender As Object, e As System.Windows.Forms.MouseEventArgsHandles ugCommission.MouseUp
         
                Dim cmItemNum As New ContextMenuStrip
                Dim cmCommDue As New ContextMenuStrip
         
                If e.Button = Windows.Forms.MouseButtons.Right Then
         
                    Dim ptCurrent As Point = New Point(e.X, e.Y)
                    Dim uiElement As Infragistics.Win.UIElement = CType(sender, UltraGrid).DisplayLayout.UIElement.ElementFromPoint(ptCurrent)
                    Dim ugCell As UltraGridCell = uiElement.GetContext(GetType(UltraGridCell))
                    Dim ugRow As UltraGridRow = uiElement.GetContext(GetType(UltraGridRow))
                    ' Thought ugUIElement would be it, but it is also nothing when I right click on the col header cell
                    Dim ugUIElement As UltraGridUIElement = uiElement.GetContext(GetType(UltraGridUIElement))             If ugCell IsNot Nothing Then                 ugCommission.Selected.Rows.Clear()                 ugRow.Selected = True                 If ugCell.Column.Key = "ITEM_NUM" Then                     _cmItemNum.Show(ugCommission, ptCurrent)                 ElseIf ugCell.Column.Key = "COMM_DUE" Then                     _cmCommDue.Show(ugCommission, ptCurrent)                 End If             End If         End If     End Sub
      • 0
        SreeRamaSaran
        SreeRamaSaran answered on Jul 8, 2011 2:33 PM

        Hi,

        As I understood that you want to know whether the user clicked on the column header or not? If this is correct, You can use the below code to detect whether the user clicks on the column header or not. If I misunderstood your scenario please elaborate your question so that I can help.

        Dim ugColl As UltraGridColumn = uiElement.GetContext(GetType(UltraGridColumn))

        If (ugCell Is Nothing And ugColl IsNot Nothing) Then
                ' your code goes here

        End If

        [quote user="dpbouchard"]

        I would like to know if I right-clicked on the cell header.  How do I do this?

        My current code:

            Private Sub ugCommission_MouseUp(sender As Object, e As System.Windows.Forms.MouseEventArgsHandles ugCommission.MouseUp
         
                Dim cmItemNum As New ContextMenuStrip
                Dim cmCommDue As New ContextMenuStrip
         
                If e.Button = Windows.Forms.MouseButtons.Right Then
         
                    Dim ptCurrent As Point = New Point(e.X, e.Y)
                    Dim uiElement As Infragistics.Win.UIElement = CType(sender, UltraGrid).DisplayLayout.UIElement.ElementFromPoint(ptCurrent)
                    Dim ugCell As UltraGridCell = uiElement.GetContext(GetType(UltraGridCell))
                    Dim ugRow As UltraGridRow = uiElement.GetContext(GetType(UltraGridRow))
                    ' Thought ugUIElement would be it, but it is also nothing when I right click on the col header cell
                    Dim ugUIElement As UltraGridUIElement = uiElement.GetContext(GetType(UltraGridUIElement))             If ugCell IsNot Nothing Then                 ugCommission.Selected.Rows.Clear()                 ugRow.Selected = True                 If ugCell.Column.Key = "ITEM_NUM" Then                     _cmItemNum.Show(ugCommission, ptCurrent)                 ElseIf ugCell.Column.Key = "COMM_DUE" Then                     _cmCommDue.Show(ugCommission, ptCurrent)                 End If             End If         End If     End Sub

        [/quote]

         

      • 0
        Mike Saltzman
        Mike Saltzman answered on Jul 8, 2011 2:40 PM

        Hi,

        I assume by "cell header" that you mean the column header.

        You can do this using basically the same code you have here. You just check for a ColumnHeader type when you call GetContext.

        By the way, you can simplify your code here a little bit by using the LastElementEntered property instead of messing around with points.

        
        

            Private Sub UltraGrid1_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles UltraGrid1.MouseDown
                Dim grid As UltraGrid = CType(sender, UltraGrid)

                If e.Button = Windows.Forms.MouseButtons.Right Then
                    Dim element As UIElement = grid.DisplayLayout.UIElement.LastElementEntered

                    Dim ugCell As UltraGridCell = element.GetContext(GetType(UltraGridCell))
                    Dim ugRow As UltraGridRow = element.GetContext(GetType(UltraGridRow))
                    Dim columnHeader As ColumnHeader = element.GetContext(GetType(ColumnHeader))

                    If columnHeader IsNot Nothing Then
                        MessageBox.Show(columnHeader.Caption)
                    End If
                End If
            End Sub

         

         

         

      • 0
        SreeRamaSaran
        SreeRamaSaran answered on Jul 8, 2011 3:31 PM

        Hi,

        As mike said you can also try with ColumnHeader. But the coding pattern remains same.

  • You must be logged in to reply to this topic.
Discussion created by
Favorites
Replies
Created On
Last Post
Discussion created by
SreeRamaSaran
Favorites
0
Replies
7
Created On
Jul 08, 2011
Last Post
15 years, 2 months ago

Suggested Discussions

Tags

Created by

Created on

Jul 8, 2011 3:31 PM

Last activity on

Feb 16, 2026 9:46 PM