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 458025
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T22:37:48+00:00 2026-05-12T22:37:48+00:00

(VS2005, .Net 2.0) I have a form that is displayed as a dialog using

  • 0

(VS2005, .Net 2.0)

I have a form that is displayed as a dialog using the ShowDialog() method. The form’s CancelButton property is set to a button on the form. Even if I set that button’s DialogResult to None, clicking the button still closes the dialog. I don’t want this to happen – I want to be able to control whether the dialog closes or not.

This issue doesn’t occur with the form’s AcceptButton – with that button’s DialogResult set to none, I can do what processing is necessary, then decide whether or not to manually set the form’s DialogResult to cause it to close.

I thought the CancelButton property was meant soley to indicate the button that should be “clicked” if Escape is pressed (much as the AcceptButton is only supposed to indicate the button to “click” when Enter is pressed). Am I wrong in this? Have I missed some other reason my form is closing? Or is this a bug?

Edit: Code added. This is the dialog form (form 2) with the cancel button (button 1).
The cancel button is only the form’s CancelButton, it does not have DialogResult set to Cancel, but pressing the button still closes the form

    private void InitializeComponent()
    {
        this.button1 = new System.Windows.Forms.Button();
        this.SuspendLayout();
        // 
        // button1
        // 
        this.button1.Name = "button1";
        this.button1.Text = "button1";
        // 
        // Form2
        // 
        this.CancelButton = this.button1;
        this.Controls.Add( this.button1 );
        this.Name = "Form2";
        this.Text = "Form2";
        this.ResumeLayout( false );
    }
  • 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-05-12T22:37:48+00:00Added an answer on May 12, 2026 at 10:37 pm

    Also beware that the form may be closed by pressing Alt+F4 and pressing the X button and both ways will not trigger the cancel button click event.

    Unless you are also handling these situations wouldn’t it be best to follow the advice of slurdge and prevent the form from closing in the FormClosing event.

    Edit:
    Also note that if you change the DialogResult of the button back to None in the Properties windows, you are changing it to the default value. If the value is the default value of the property then it will not be persisted in the *.Designer.cs. Even if it were persisted the form initialization code is placed last in the *.Designer.cs and would override the None with Cancel because of the line:

    this.CancelButton = this.button1;
    

    As you can check in Reflector the previous line does this:

    public void set_CancelButton(IButtonControl value)
    {
        base.Properties.SetObject(PropCancelButton, value);
        if ((value != null) && (value.DialogResult == DialogResult.None))
        {
            value.DialogResult = DialogResult.Cancel;
        }
    }
    

    You can change it back to None in the constructor after the InitializeComponent() call.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have VS2005 and I am currently trying to debug an ASP.net web application.
I have a website I've built in VS2005, C#, .NET 2.0. This website does
I have an Asp.NET application (VS2008, Framework 2.0). When I try to set a
I'm currently using VS2005 Profesional and .NET 2.0, and since our project is rather
Using .Net 3.0 and VS2005. The objects in question are consumed from a WCF
NOTE: Using .NET 2.0, and VS2005 as IDE Hello all, I'm working on logging
I have 3 .net projects. Project1.dll is generated by a VS2008 project. Project2.dll is
I am coding VB.NET in VS2008. I have a comma delimited string of numbers,
I have created a setup project in VS2008. My WinForms app uses .NET 2.0,
This is a strange one... In a windows forms app (VB.NET/VS 2005) I have

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.