奇怪:按下按钮后,WinForms表单自动关闭

Rah*_*han 8 .net c# winforms

我的应用程序是WinForms .NET 4(C#),其中一个表单按下按钮后会自动关闭.

  • 表单DOES具有默认的"接受"和"取消"按钮,但不会触及这些按钮.
  • 有一个ButtonTestConnection_Click事件,当它被点击时,它会完成它的工作,但会以某种方式关闭表单.
  • 我正在使用鼠标单击按钮,因此这不是级联键击的情况.
  • 我没有在这个函数中设置DialogResult.

我也试图检查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设置