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

In the debugger it says that the result is DialogResult.Cancel. Why is this? I have a form with just a progress bar and a label. Nothing more. I have opened and closed some other forms as well if this matters. The form doesn't open.

Progress class:

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;
    }
}

and designer:

 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;
}
trinalbadger587
  • 1,905
  • 1
  • 18
  • 36
  • sounds weird, can you include your Progress class? – PeteB Apr 30 '16 at 05:52
  • OK, I'll do that thought I just clicked new form and dragged a progress bar and a label in. – trinalbadger587 Apr 30 '16 at 05:54
  • a quick and simple test... rip some stuff out and try again. If you can identify the item that needs to be removed you'll be 99% to a solution! – PeteB Apr 30 '16 at 05:57
  • Or, put a breakpoint on new Progress() line, then F11 for step by step debugging afterwards – Martheen Apr 30 '16 at 06:01
  • I've just tried: deleting form and trying again removing both elements . It still gives cancel – trinalbadger587 Apr 30 '16 at 06:02
  • "The dialog box can be assigned one of the values of the DialogResult enumeration by assigning it to the DialogResult property of a Button on the form or by setting the DialogResult property of the form in code." from https://msdn.microsoft.com/en-us/library/c7ykbedk(v=vs.110).aspx says that you can set a DialogResult property on the form itself... check the property inspector to see if that's set to Cancel. – PeteB Apr 30 '16 at 06:04
  • I've already tried setting form.DialogResult to OK. The result of creating the initial form is OK. – trinalbadger587 Apr 30 '16 at 06:05
  • https://msdn.microsoft.com/en-us/library/hh193692(v=vs.110).aspx also try using a different class name than Progress which seems to be taken... – PeteB Apr 30 '16 at 06:06
  • OK, I've just tried renaming to ProgressForm, no change. – trinalbadger587 Apr 30 '16 at 06:08

1 Answers1

1

This is a normal behavior. You can refer to this answer and from Form.DialogResult Property.

When a form is displayed as a modal dialog box, clicking the Close button (the button with an X in the top-right corner of the form) causes the form to be hidden and the DialogResult property to be set to DialogResult.Cancel. The Close method is not automatically called when the user clicks the Close button of a dialog box or sets the value of the DialogResult property. Instead, the form is hidden and can be shown again without creating a new instance of the dialog box. Because of this behavior, you must call the Dispose method of the form when the form is no longer needed by your application.

Meaning by default, when you close a form it will have DialogResult.Cancel unless it is overridden.

Community
  • 1
  • 1
jegtugado
  • 5,081
  • 1
  • 12
  • 35