2016-04-30 11 views
1
Progress progress = new Progress();//progress.DialogResult is OK. 
if (progress.ShowDialog() != DialogResult.OK) 
{//progress.DialogResult is cancel. 
    throw new ArgumentException(); 
} 

デバッガでは、結果がDialogResult.Cancelであることを示します。 これはなぜですか? 私は進行状況バーとラベルだけのフォームを持っています。これ以上何もない。 これが重要な場合は、他のフォームも開いて閉じました。 フォームが開きません。私はShowDialogでフォームを開始しようとしており、DialogResult.Cancelを返します。どうしてこれなの?

進捗クラス:

public partial class Progress : Form 
{ 
    public Progress() 
    { 
     InitializeComponent(); 
    } 

    bool filesLength = false; 

    public void OnWordsChanged() 
    { 
     if (!filesLength) 
     { 
      progressBar1.Maximum = Program.numberOfFiles; 
      filesLength = true; 
     } 
     progressBar1.Value = Program.wordsDone; 
    } 
} 

とデザイナー:

partial class Progress 
{ 
    /// <summary> 
    /// Required designer variable. 
    /// </summary> 
    private System.ComponentModel.IContainer components = null; 

    /// <summary> 
    /// Clean up any resources being used. 
    /// </summary> 
    /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> 
    protected override void Dispose(bool disposing) 
    { 
     if (disposing && (components != null)) 
     { 
      components.Dispose(); 
     } 
     base.Dispose(disposing); 
    } 

    #region Windows Form Designer generated code 

    /// <summary> 
    /// Required method for Designer support - do not modify 
    /// the contents of this method with the code editor. 
    /// </summary> 
    private void InitializeComponent() 
    { 
     this.label1 = new System.Windows.Forms.Label(); 
     this.progressBar1 = new System.Windows.Forms.ProgressBar(); 
     this.SuspendLayout(); 
     // 
     // label1 
     // 
     this.label1.AutoSize = true; 
     this.label1.Location = new System.Drawing.Point(13, 13); 
     this.label1.Name = "label1"; 
     this.label1.Size = new System.Drawing.Size(35, 13); 
     this.label1.TabIndex = 0; 
     this.label1.Text = "label1"; 
     // 
     // progressBar1 
     // 
     this.progressBar1.Location = new System.Drawing.Point(13, 30); 
     this.progressBar1.Name = "progressBar1"; 
     this.progressBar1.Size = new System.Drawing.Size(259, 23); 
     this.progressBar1.TabIndex = 1; 
     // 
     // Progress 
     // 
     this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); 
     this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; 
     this.ClientSize = new System.Drawing.Size(284, 261); 
     this.Controls.Add(this.progressBar1); 
     this.Controls.Add(this.label1); 
     this.Name = "Progress"; 
     this.Text = "Progess"; 
     this.ResumeLayout(false); 
     this.PerformLayout(); 

    } 

    #endregion 

    private System.Windows.Forms.Label label1; 
    private System.Windows.Forms.ProgressBar progressBar1; 
} 
+0

あなたのプログレスクラスを含めることができますか? – PeteB

+0

OK、私はちょうど新しいフォームをクリックして、進捗バーとラベルをドラッグしたと思うでしょう。 – trinalbadger587

+0

素早く簡単なテスト...いくつかのものを取り除き、やり直してください。あなたが削除する必要があるアイテムを識別できる場合は、解決策の99%になります! – PeteB

答えて

1

これは正常な動作です。これはanswerForm.DialogResult Propertyから参照できます。

フォームがフォームが非表示になり 閉じる ボタン(フォームの右上隅にあるXの付いたボタン)をクリックし、モーダルダイアログボックスとして表示され

とDialogResultプロパティにDialogResult.Cancelに を設定してください。ユーザーがダイアログボックスの閉じるボタンをクリックするか、DialogResultプロパティの 値を設定すると、Closeメソッドは自動的に と呼ばれません。代わりに、フォームが非表示になり、ダイアログボックスの新しいインスタンスを作成せずに を再度表示することができます。 この現象のため、アプリケーションでフォームが不要になったときに、フォーム のDisposeメソッドを呼び出す必要があります。

デフォルトでは、フォームを閉じるときに、それがオーバーライドされない限り、DialogResult.Cancelが作成されます。

+0

@Ephriam、私は閉じていない、それはDialogResult.Cancelとそれ自身で閉じた。 – trinalbadger587

+0

@ trinalbadger587 DialogResult変数に最初に代入して比較しましたか? –

+0

@Ephriamはい、私もDialogResult.OKにそれを割り当てようとしました。 – trinalbadger587

関連する問題