如何防止DataGridView双击打开表单不止一次
本文关键字:表单 不止一次 双击 何防止 DataGridView | 更新日期: 2023-09-27 18:05:05
当我双击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,我想如果它被打开,双击将不会打开它
将打开的表单保留在class字段中
例如,代替你的代码,调用像这样的方法:
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();
}
}
在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();
}
最好的方法是使变量f4成为类级别变量,Form4 f4 = new Form4();
行应该只运行一次,然后在f4.Show();
行之前测试查看表单是否已经显示,然后再尝试显示它。