8

私のアプリはWinForms.NET4(C#)で、ボタンを押した後、フォームの1つが自動的に閉じ続けます。

  • フォームにはデフォルトの[承認]ボタンと[キャンセル]ボタンがありますが、これらには触れていません。
  • ButtonTestConnection_Clickイベントがあります。このイベントは、クリックされるとその役割を果たしますが、何らかの方法でフォームを閉じます。
  • 私はマウスを使用してボタンをクリックしているので、これはカスケードキーストロークの場合ではありません。
  • この関数でDialogResultを設定していません。

また、これを迷わないようにチェックしようとしました。閉じる/ this。通話を破棄しましたが、見つかりませんでした。

コードは次のとおりです。

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

1 に答える 1

34

ボタンのプロパティDialogResultが と等しいかどうかを確認しますNone
そうでない場合、そのボタンを押すとフォームが閉じられ、フォームはボタンの DialogResult プロパティの設定を返します。

通常、これは既存のフォームのボタンをコピーして貼り付けたが、貼り付けたボタンで元の DialogResult 設定を削除するのを忘れた場合によく発生します。

于 2012-06-05T15:53:47.167 に答える