Form Close button

how to code the form close button

Page 1 of 1

10 Replies - 16321 Views - Last Post: 31 August 2010 - 04:29 AM Rate Topic: -----

#1 xikky  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 88
  • Joined: 19-December 09

Form Close button

Posted 10 March 2010 - 03:11 AM

hi everyone!

i'm trying to find a way to find where i can code the 'X' button of the form. i've tried different ways that i've found on the net (even on msdn) but i couldn't manage to get it to work.

what i've mainly tried is this:
private void frmLogin_Close(object sender, System.ComponentModel.CancelEventArgs e)
        {
            DialogResult dr = MessageBox.Show("Are you sure you want to exit application?", "Exit", MessageBoxButtons.YesNo);

            if (dr == DialogResult.Yes)
            {
                e.Cancel = true;
            }
        }



but this is not working :/ .. the program just closes normally and no message is displayed. am i leaving something out?

thanks 4 ur help :)

Is This A Good Question/Topic? 0
  • +

Replies To: Form Close button

#2 FlashM  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 382
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: Form Close button

Posted 10 March 2010 - 03:16 AM

You should use the FormClosing event instead of FormClosed. FormClosed event is fired when a form is already closed. FormClosing event is fired when form is starting to close (is in close pending mode). And if your dialog result is NO then you should cancel closing.

private void frmLogin_FormClosing(object sender, FormClosingEventArgs e)
{
    DialogResult dr = MessageBox.Show("Are you sure you want to exit application?", 
                                      "Exit", MessageBoxButtons.YesNo);

    if (dr == DialogResult.No)
    {
        e.Cancel = true;
    }
}


This post has been edited by FlashM: 10 March 2010 - 03:17 AM

Was This Post Helpful? 3
  • +
  • -

#3 pdkharkar  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 63
  • View blog
  • Posts: 345
  • Joined: 19-January 09

Re: Form Close button

Posted 10 March 2010 - 03:17 AM

You are going into the deep for no reason
to close the form, you have to simply add the this.close(); of close(); in the button_click event
this is sample code
private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }


Was This Post Helpful? 0
  • +
  • -

#4 FlashM  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 382
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: Form Close button

Posted 10 March 2010 - 03:21 AM

pdkharkar, I believe that xikky is trying not to just close the form, but ask user first if he/she really wants to exit the application.

This post has been edited by FlashM: 10 March 2010 - 03:21 AM

Was This Post Helpful? 0
  • +
  • -

#5 xikky  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 88
  • Joined: 19-December 09

Re: Form Close button

Posted 10 March 2010 - 03:28 AM

Thanks once again FlashM xD (already helped me twice in less then 24hrs)

but still the message is not displayed when i'm closing ('X') the window

i'm guessing that this method is automaticaly loaded like the Form1_Load(..)

@pdkharkar .. i did that already from an exit menu option, but i want to control the 'X' button aswell ^^
Was This Post Helpful? 0
  • +
  • -

#6 b.ihde  Icon User is offline

  • D.I.C for life
  • member icon

Reputation: 44
  • View blog
  • Posts: 1,070
  • Joined: 29-September 08

Re: Form Close button

Posted 10 March 2010 - 03:56 AM

Hey,

create a new event for form_closing ... not form_closed ^^

you can easy google it to get an simple example, or look here MSDN

greetz

ben

edit* oh i missread, thought u close the form with a buttonclick event.. Seems like ur formclosing event isn t registered..

oh and you got in your code "System.ComponentModel.CancelEventArgs e" but like FlashM said it must be FormClosingEventArgs e .. idk if you changed that...

This post has been edited by b.ihde: 10 March 2010 - 04:05 AM

Was This Post Helpful? 0
  • +
  • -

#7 FlashM  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 382
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: Form Close button

Posted 10 March 2010 - 04:07 AM

You can manually register the FormClosing event like I did with code below:

public partial class frmLogin : Form
{
    public frmLogin()
    {
        InitializeComponent();
        this.FormClosing += new FormClosingEventHandler(frmLogin_FormClosing);
    }

    private void frmLogin_FormClosing(object sender, FormClosingEventArgs e)
    {
        DialogResult dr = MessageBox.Show("Are you sure you want to exit application?", 
                                          "Exit", MessageBoxButtons.YesNo);

        if (dr == DialogResult.No)
        {
            e.Cancel = true;
        }
    }
}



Or you can use the frmLogin events properties window to register you FormClosing event...
Was This Post Helpful? 2
  • +
  • -

#8 xikky  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 88
  • Joined: 19-December 09

Re: Form Close button

Posted 10 March 2010 - 07:04 AM

oh yes! ^^ thanks 4 the help! .. and thanks for helping me learning more about event handlers
Was This Post Helpful? 0
  • +
  • -

#9 FlashM  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 382
  • View blog
  • Posts: 1,195
  • Joined: 03-December 09

Re: Form Close button

Posted 10 March 2010 - 07:06 AM

No problem. Anytime...
Was This Post Helpful? 0
  • +
  • -

#10 Guest_Rajesh Vyas*


Reputation:

Re: Form Close button

Posted 30 August 2010 - 11:49 PM

but some time this.close() does not work on that case just add this.Dispose() before close.This will release all resources occupied by form. :helpsmilie:
Was This Post Helpful? 0

#11 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1525
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Re: Form Close button

Posted 31 August 2010 - 04:29 AM

View PostRajesh Vyas, on 31 August 2010 - 02:49 AM, said:

but some time this.close() does not work on that case just add this.Dispose() before close.This will release all resources occupied by form. :helpsmilie:


If you call Dispose before Close, then there is no reason to call Close.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1