Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to close form

Ok, so a Windows Forms class, WindowSettings, and the form has a "Cancel"-button. When the user clicks the button, the dialog DialogSettingsCancel will pop-up up and ask the user if he is sure he wants to perform the action. The dialog has 2 buttons, a "Yes"-button and a "No"-button. If the user clicks the "Yes"-button, I want both DialogSettingsCancel and WindowSettings to be closed.

My button_Click event handler in DialogSettingsCancel:

private void button1_Click(object sender, EventArgs e)
{
    //Code to trigger when the "Yes"-button is pressed.
    WindowSettings settings = new WindowSettings();
    this.Close();
    settings.Close();
}

When I run my application, and go to the settings form, and click the "Cancel"-button, and then click the "Yes"-button, only DialogSettingsCancel closes without closing WindowSettings.

Why won't it work?

I've also tried changing

this.Close();
settings.Close();

to

settings.Close();
this.Close();

But still the same result.

like image 757
osvein Avatar asked Jan 17 '13 14:01

osvein


People also ask

How do I close a form response?

More options. Open a form in Google Forms. At the top, click Responses. Turn "Accepting responses" from On to Off.

How do I manually close a form?

Close a Google Form Manually To close a form immediately, you can simply flip a switch. You then have the option to display a custom message for those who still try to access the form. Open your form in Google Forms and go to the Responses tab at the top. Turn off the toggle for Accepting Responses.


2 Answers

You need the actual instance of the WindowSettings that's open, not a new one.

Currently, you are creating a new instance of WindowSettings and calling Close on that. That doesn't do anything because that new instance never has been shown.

Instead, when showing DialogSettingsCancel set the current instance of WindowSettings as the parent.

Something like this:

In WindowSettings:

private void showDialogSettings_Click(object sender, EventArgs e)
{
    var dialogSettingsCancel = new DialogSettingsCancel();
    dialogSettingsCancel.OwningWindowSettings = this;
    dialogSettingsCancel.Show();
}

In DialogSettingsCancel:

public WindowSettings OwningWindowSettings { get; set; }

private void button1_Click(object sender, EventArgs e)
{
    this.Close();
    if(OwningWindowSettings != null)
        OwningWindowSettings.Close();
}

This approach takes into account, that a DialogSettingsCancel could potentially be opened without a WindowsSettings as parent.

If the two are always connected, you should instead use a constructor parameter:

In WindowSettings:

private void showDialogSettings_Click(object sender, EventArgs e)
{
    var dialogSettingsCancel = new DialogSettingsCancel(this);
    dialogSettingsCancel.Show();
}

In DialogSettingsCancel:

WindowSettings _owningWindowSettings;

public DialogSettingsCancel(WindowSettings owningWindowSettings)
{
    if(owningWindowSettings == null)
        throw new ArgumentNullException("owningWindowSettings");

    _owningWindowSettings = owningWindowSettings;
}

private void button1_Click(object sender, EventArgs e)
{
    this.Close();
    _owningWindowSettings.Close();
}
like image 157
Daniel Hilgarth Avatar answered Oct 06 '22 00:10

Daniel Hilgarth


new WindowSettings();

You just closed a brand new instance of the form that wasn't visible in the first place.

You need to close the original instance of the form by accepting it as a constructor parameter and storing it in a field.

like image 33
SLaks Avatar answered Oct 05 '22 22:10

SLaks