【问题标题】:How can I prevent DataGridView double click to open a form more than one time?如何防止 DataGridView 双击打开表单多次?
【发布时间】:2011-08-11 08:34:38
【问题描述】:

双击dataGridView单元格时如何打开表单一次?

private void dataGridView1_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
{
    //string queryString = "SELECT id, thename, address,fax,mobile,email,website,notes FROM movie";
    int currentRow = int.Parse(e.RowIndex.ToString());
    try
    {
        string movieIDString = dataGridView1[0, currentRow].Value.ToString();
        movieIDInt = int.Parse(movieIDString);
    }
    catch (Exception ex) { }
    // edit button
    if (e.RowIndex != -1)
    {
        string id = dataGridView1[0, currentRow].Value.ToString();
        string thename = dataGridView1[1, currentRow].Value.ToString();
        string address = dataGridView1[2, currentRow].Value.ToString();
        string fax = dataGridView1[3, currentRow].Value.ToString();
        string mobile = dataGridView1[4, currentRow].Value.ToString();
        string email = dataGridView1[5, currentRow].Value.ToString();
        string website = dataGridView1[6, currentRow].Value.ToString();
        string notes = dataGridView1[7, currentRow].Value.ToString();

        Form4 f4 = new Form4();

        f4.id = movieIDInt;
        f4.thename = thename;
        f4.address = address;
        f4.fax = fax;
        f4.mobile = mobile;
        f4.email = email;
        f4.website = website;
        f4.notes = notes;


        f4.Show();
    }
}

这段代码每次点击dataGridView都会打开一个表单,我想如果打开了,doubleClick就不会再打开了

【问题讨论】:

  • 您发布的代码是在 Click 事件处理程序还是 DoubleClick 事件处理程序中(或在 CellClick 或 CellDoubleClick... 中)?
  • dataGridView1_CellMouseDoubleClick

标签: c#


【解决方案1】:

将打开的表单保存在类字段中
例如而不是你的代码,调用这样的方法:

    Form4 f4 = null; // class field

    // call this method when cellMouseDoubleClick is triggered
    private void OpenForm4IfNotOpened()
    {
        if (f4 == null || f4.IsDisposed)
        {
            f4 = new Form4();

            f4.id = movieIDInt;
            f4.thename = thename;
            f4.address = address;
            f4.fax = fax;
            f4.mobile = mobile;
            f4.email = email;
            f4.website = website;
            f4.notes = notes;
            f4.Show();
        }
        else
        {
            f4.BringToFront();
        }
    }

【讨论】:

  • @amer:不客气。无论如何,您应该接受您认为正确的答案(不仅在这里,而且在您之前的问题中),因为 StackOverflow 就是这样工作的 ;-) (link: How to accept answers)
【解决方案2】:

在 form.cs 中将其声明为全局变量

  bool isopened = false;

然后检查 isopened 变量

if (isopened == false)
            {
                FormInitialSettings();
                Form4 f4 = new Form4();

                f4.id = movieIDInt;
                f4.thename = thename;
                f4.address = address;
                f4.fax = fax;
                f4.mobile = mobile;
                f4.email = email;
                f4.website = website;
                f4.notes = notes;
                isopened = true;
                f4.Show();
            }

【讨论】:

    【解决方案3】:

    最好的方法是使变量 f4 成为一个类级别的变量,并且Form4 f4 = new Form4(); 行应该只运行一次,然后在f4.Show(); 行之前测试,看看表单是否已经显示,然后再尝试再次显示它.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-13
      • 2017-07-19
      • 1970-01-01
      相关资源
      最近更新 更多