Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9145705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:34:50+00:00 2026-06-17T10:34:50+00:00

Ok, so a Windows Forms class, WindowSettings, and the form has a Cancel-button. When

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T10:34:52+00:00Added an answer on June 17, 2026 at 10:34 am

    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();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The Form class of windows forms has the AcceptButton property, which allows certain button
Imports System Imports System.Windows.Forms Class MyButtonClass Inherits Form Private mrButton As Button Public Sub
Class A has a Form1 (subclass of System.Windows.Forms.Form) member. class A { Form1 form;
public class Form1 : System.Windows.Forms.Form { private void eachCornerPix (object sender, PaintEventArgs e, out
I have a class which inherits System.Windows.Forms.Form. I need to make the form available
I have piece of Code in Form that works: public class Form1 : System.Windows.Forms.Form
I've got a Windows Forms Button on a Form which submits a web request.
When I add IDisposable class member to Windows Forms Form class, I add disposing
According to http://msdn.microsoft.com/en-us/library/system.windows.forms.linklabel.aspx , the LinkLabel class has both a Click event inherited from
Windows Forms application: MainForm.cs - Windows Form Settings.settings - Settings class with an entry

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.