6

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.

varg
  • 3,446
  • 1
  • 15
  • 17
fIwJlxSzApHEZIl
  • 11,861
  • 6
  • 62
  • 71
  • 1
    The answer to [a similar question](http://stackoverflow.com/a/515118/292067) should help you. – jswolf19 Oct 06 '12 at 00:12
  • I had no idea the Control class even existed, thanks! I'm taking this visual C# stuff one thing at a time. I've been doing a lot of Google searching and nothing comes up with a query similarly named to my question so hopefully now people can direct themselves to the answer with this. Thanks! – fIwJlxSzApHEZIl Oct 06 '12 at 00:19

1 Answers1

7
   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!");
        }
    }
fIwJlxSzApHEZIl
  • 11,861
  • 6
  • 62
  • 71
  • 1
    Be careful, because the way you've written the code ctrl + shift + click, ctrl + alt + click, and ctrl + shift + alt + click won't register. This may or may not be the behavior you want ^_^ – jswolf19 Oct 07 '12 at 14:00