我正在尝试使用ShowDialog启动一个表单,它返回DialogResult.Cancel。为什么是这样?

时间:2016-04-30 05:49:31

标签: c# winforms showdialog dialogresult

Progress progress = new Progress();//progress.DialogResult is OK.
if (progress.ShowDialog() != DialogResult.OK)
{//progress.DialogResult is cancel.
    throw new ArgumentException();
}

在调试器中,它表示结果是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;
}

1 个答案:

答案 0 :(得分:1)

这是正常行为。您可以参考此answerForm.DialogResult Property

  

当表单显示为模式对话框时,单击“关闭”   按钮(表单右上角带有X的按钮)   导致隐藏窗体并设置DialogResult属性   到DialogResult.Cancel。不会自动调用Close方法   当用户单击对话框的“关闭”按钮或设置时   DialogResult属性的值。相反,表单是隐藏的   可以在不创建对话框的新实例的情况下再次显示。   由于此行为,您必须调用窗体的Dispose方法   当您的申请不再需要表格时。

默认情况下,当您关闭表单时,它将具有DialogResult.Cancel,除非它被覆盖。