【问题标题】:Detecting CTRL + Click for DataGridView cell in the same event handler在同一事件处理程序中检测 CTRL + 单击 DataGridView 单元格
【发布时间】:2012-09-27 03:43:32
【问题描述】:

所以很容易检查一个单元格是否被点击过:

        DataGridView.CellClicked += cellClickedHandler;

而且很容易检查一个键是否被按下:

        DataGridView.KeyDown += keyPressedHandler;

我想知道如何将这两个功能合二为一?我想在用户控件单击单元格时执行特定操作,据我所知,这些事件的操作处理程序是两个独特的独立函数,传递给 cellClickedHandler 的参数不允许我获取状态键盘和任何可能与鼠标点击一起触发的按键。

【问题讨论】:

  • a similar question 的答案应该对您有所帮助。
  • 我什至不知道 Control 类的存在,谢谢!我一次只做一件事。我一直在做很多谷歌搜索,但没有找到与我的问题类似的查询,所以希望现在人们可以用这个找到答案。谢谢!

标签: c# .net winforms events


【解决方案1】:
   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!");
        }
    }

【讨论】:

  • 小心,因为您编写代码 ctrl + shift + click、ctrl + alt + click 和 ctrl + shift + alt + click 的方式不会注册。这可能是也可能不是您想要的行为^_^
猜你喜欢
  • 2012-09-27
  • 1970-01-01
  • 1970-01-01
  • 2015-09-25
  • 2015-12-22
  • 1970-01-01
  • 1970-01-01
  • 2017-10-03
  • 1970-01-01
相关资源
最近更新 更多