2011-08-11 53 views
1

当我双击dataGridView单元格时,如何一次打开窗体?如何防止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就不会再次打开它

+0

是您曾经参与过Click事件处理程序或DoubleClick事件处理程序的代码(或CellClick或CellDoubleClick ...)? – digEmAll

+0

dataGridView1_CellMouseDoubleClick – amer

回答

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(); 
     } 
    } 
+0

digEmAll谢谢:) – amer

+0

@amer:不客气。无论如何,你应该接受你认为是正确的答案(不仅在这里,而且在你的问题之前),因为这是StackOverflow的工作原理;-) [(链接:如何接受答案)](http:// meta。 stackexchange.com/questions/5234/how-does-accepting-an-answer-work) – digEmAll

1

声明此为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(); 
      } 
0

最好的办法是使变量F4一个L级evel变量和行Form4 f4 = new Form4();应该只运行一次,然后行f4.Show();测试,以查看表单是否已显示,然后再试图显示它。