Closing a form during a constructor

前端 未结 9 2072
面向向阳花
面向向阳花 2020-12-01 11:59

Is it possible to close a form while the constructor is executing (or simply to stop it showing at this stage)?

I have the following code:

public par         


        
9条回答
  •  南笙
    南笙 (楼主)
    2020-12-01 12:10

    I think it is not wise to close a form in the constructor. If you do this, users of your form wouldn't know whether to ShowDialog or not.

    The following code would be quite normal use:

    // in the parent form:
    public void ShowMyForm()
    {
        MyForm form = new MyForm();
        form.propertyA = ...;
        from.propertyB = ...;
        DialogResult dlgResult = form.ShowDialog(this);
        ProcessDialogResult(dlgResult);
    }
    

    If you decided in the constructor whether the Form ought to be shown, you would have to add code after construction to decide whether to call ShowDialog or not and whether to Process the dialog result.

    Furthermore, are you sure that changing the properties will never influence whether the form is to be shown or not? Also after future changes?

    During construction the form is not shown / opened yet. So I'm afraid Close() doesn't do what you expect.

    The neat method is to do the checks that you wanted to do in the constructor in the Form_Load. Add an event handler for form-load and do your checks in the event handler. Use the property DialogResult to indicate that you decided not to show the form.

    private void FormMain_Load (object sender, EventArgs e)
    {
        if (FormShouldNotLoad())
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Abort;
            Close();
            // Warning, this does not work, see below, (but we're almost there!)
        }
    }
    

    The user of the code could check the result of the dialog:

    // in the parent form:
    public void ShowMyForm()
    {
        MyForm form = new MyForm();
        form.propertyA = ...;
        from.propertyB = ...;
        DialogResult dlgResult = form.ShowDialog(this);
        switch (dlgResult)
        {
            case System.Windows.Forms.DialogResult.Abort:
                ProcessFormNotLoaded();
                break;
            case System.Windows.Forms.DialogResult.OK:
                ProcessFormOk();
                break;
            // etc.
        }
    }
    

    However, calling Close() in the event handler for form-load won't work, because Close() can only be called properly after Load is completed.

    Therefore, instead of calling Close(), you should BeginInvoke the Close() function, so the Close function will be called after loading is done:

    private void FormMain_Load (object sender, EventArgs e)
    {
        if (FormShouldNotLoad())
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Abort;
            // invoke the Close function after Load completed
            this.BeginInvoke(new MethodInvoker( () => this.CancelLoading())
        }
    }
    

提交回复
热议问题