Home Forums WinForms controls Xceed Grid for WinForms Context menu strip Reply To: Context menu strip

User (Old forums)
Member
Post count: 23064

You can use the the MouseDown event at the cell level and verify if the cell is current, and if so let the ContextMenu open.  In all other circumstances, do not let the ContextMenu open, using the Opening event on the ContextMenuStrip.

e.g.:

    private void Form1_Load( object sender, EventArgs e )
    {
      gridControl1.ContextMenuStrip = contextMenuStrip1;
      contextMenuStrip1.Opening += new CancelEventHandler( contextMenuStrip1_Opening );

      foreach( Cell cell in dataRowTemplate1.Cells )
      {
        cell.MouseDown += new MouseEventHandler(cell_MouseDown);
      }
    }

    private bool onCurrentCell = false;

    void cell_MouseDown( object sender, MouseEventArgs e )
    {
      Cell cell = sender as Cell;
      if( cell != null && cell.IsCurrent )
      {
        onCurrentCell = true;
      }
      else
      {
        onCurrentCell = false;
      }
    }

    void contextMenuStrip1_Opening( object sender, CancelEventArgs e )
    {
      if( !onCurrentCell )
      {
        onCurrentCell = false;
        e.Cancel = true;
      }
    }
 

 

Imported from legacy forums. Posted by André (had 2173 views)