【发布时间】:2012-06-09 15:55:10
【问题描述】:
我的应用是 WinForms .NET 4 (C#),其中一个表单在按下按钮后会自动关闭。
- 表单具有默认的“接受”和“取消”按钮,但未触及这些按钮。
- 有一个 ButtonTestConnection_Click 事件,当单击该事件时,它会完成它的工作,但会以某种方式关闭表单。
- 我使用鼠标单击按钮,所以这不是级联击键的情况。
- 我没有在此函数中设置 DialogResult。
我还尝试检查流浪 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());
}
【问题讨论】:
-
我感觉这可能与将按钮设置为未启用有关,这可能会转移焦点。还不确定。
-
从事件处理程序中取出所有内容,然后开始一次添加一行以找出导致问题的行(使用二进制搜索进行优化)
-
覆盖表单的 OnFormClosing 方法。在它上面设置一个断点并在它命中时查看调用堆栈。如果您无法理解,请将其发布在您的问题中。
-
感谢您的建议。事实证明,史蒂夫的回答是这样的。