【问题标题】:Cell Double Click event is not running. only Cellclick event gets executed单元格双击事件未运行。只有 Cellclick 事件被执行
【发布时间】:2010-06-22 12:40:31
【问题描述】:

我在我的窗体应用程序中同时拥有 datagridview 的 cellclick 和 celldoubleclick 事件。 问题是当我双击时,只有 cellclick 事件触发,因为它无法确定是单击还是双击。

我搜索了这个,发现计时器可能是解决方案..但是怎么做呢? 请帮忙

【问题讨论】:

  • 您是否通过临时删除点击处理程序来验证双击事件正在触发?
  • 是的,我已经验证了

标签: c# winforms


【解决方案1】:

您可能应该尝试找出双击没有被触发的原因。回答您的问题:您确实需要一个计时器,您将其间隔设置为双击时间:

public partial class Form1 : Form {
    public Form1() {
        InitializeComponent();
        timer1.Interval = SystemInformation.DoubleClickTime;
        timer1.Tick += delegate { timer1.Enabled = false; };
    }

    private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) {
        if (timer1.Enabled) {
            timer1.Enabled = false;
            // Do double-click stuff
            //...
        }
        else {
            timer1.Enabled = true;
            // Do single-click stuff
            //...
        }
    }
}

【讨论】:

    【解决方案2】:

    我为此建立了一个小解决方案: 在表单中添加计时器

    public Form1()
    {
         timer1.Interval = SystemInformation.DoubleClickTime;
    }
    bool double_click = false;
    //
    DataGridViewCellEventArgs sendedEvent = null;
    private void DatagridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
    {
       double_click = true;
       sendedEvent = e;
    }
    private void DatagridView1_CellClick(object sender, DataGridViewCellEventArgs e)
    {
        sendedEvent = e;
        //
        timer1.Enabled = true;
        //
        timer1.Start();
     }
     private void timer1_Tick(object sender, EventArgs e)
     {
        if (!double_click)
        {
            // DO your simple click stuff and USE sendedEvent if needed
        }
        else
        {
             double_click = false;
             // DO your Doubleclick stuff and USE sendedEvent if needed
        }
    
        timer1.Stop();
        timer1.Enabled = false;
      }
    

    希望对你有所帮助^^

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-24
      相关资源
      最近更新 更多