Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting CTRL + Click for DataGridView cell in the same event handler

So it's easy enough to check if a cell has been clicked with:

        DataGridView.CellClicked += cellClickedHandler;

And it's easy enough to check if a key has been pressed with:

        DataGridView.KeyDown += keyPressedHandler;

I'm wondering how I can go about combining those two functions into one? I would like to perform a specific action when a user control clicks a cell and as far as I can tell, the action handlers for these events are two unique, independent functions and the parameters passed to cellClickedHandler don't allow me to get the state of the keyboard and any key presses that may be firing in conjunction with the mouse click.

like image 627
fIwJlxSzApHEZIl Avatar asked Oct 05 '12 23:10

fIwJlxSzApHEZIl


1 Answers

   private void cellClicked(object sender, DataGridViewCellMouseEventArgs e)
    {
        if(e.Button == MouseButtons.Right) // right click
        {
            if (Control.ModifierKeys == Keys.Control)
               System.Diagnostics.Debug.Print("CTRL + Right click!");
            else
               System.Diagnostics.Debug.Print("Right click!");
        }
        if (e.Button == MouseButtons.Left) // left click
        {
            if (Control.ModifierKeys == Keys.Control)
                System.Diagnostics.Debug.Print("CTRL + Left click!");
            else
                System.Diagnostics.Debug.Print("Left click!");
        }
    }
like image 178
fIwJlxSzApHEZIl Avatar answered Oct 19 '22 19:10

fIwJlxSzApHEZIl