我的应用程序是WinForms .NET 4(C#),其中一个表单按下按钮后会自动关闭.
我也试图检查stray this.Close/this.Dispose调用但没有找到.
这是代码:
private void ButtonTestConnection_Click (object sender, System.EventArgs e)
{
this.Enabled = false;
this.Cursor = System.Windows.Forms.Cursors.WaitCursor;
this.ProgressBar.Minimum = 0;
this.ProgressBar.Maximum = 500;
this.ProgressBar.Value = 0;
this.ProgressBar.Visible = true;
this.ButtonTestConnection.Visible = false;
try
{
while (this.ProgressBar.Value < this.ProgressBar.Maximum)
{
// Some proxy code.
this.ProgressBar.Value++;
}
}
catch
{
}
this.ProgressBar.Visible = false;
this.ButtonTestConnection.Visible = true;
this.ProgressBar.Invalidate();
System.Windows.Forms.Application.DoEvents();
System.Threading.Thread.Sleep(10);
this.Cursor = System.Windows.Forms.Cursors.Default;
this.Enabled = true;
System.Windows.Forms.MessageBox.Show(result.ToString());
}
Run Code Online (Sandbox Code Playgroud)
Ste*_*eve 26
检查DialogResult按钮上的属性是否等于None.
如果没有,那么当你点击该按钮时表格将被关闭,表格将返回Button的DialogResult属性的设置.
通常,当您复制/粘贴现有表单的按钮时会发生这种情况,但忘记在粘贴按钮上删除原始DialogResult设置