Thread: Editing the Red X Control Button....

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    21

    Editing the Red X Control Button....

    Hi,

    I've stuck some simple code in my form cancel / ok buttons that disables any predecesing forms, and then enables them and brings them to the front once they are required again.

    Is there any way to replicate this code in the Red X control button? I don't want to remove the whole controlbox as I'd like to keep minimise / maximise functionality.

    Thanks.

  2. #2
    Registered User
    Join Date
    Mar 2009
    Location
    england
    Posts
    209
    If I understand you correctly, you want the X button to do something and then not close the application? You need to use the OnFormClosing event, which you can place anywhere in your form class:

    Code:
    protected override void OnFormClosing(FormClosingEventArgs e)
    {
        // put your custom code here
    
    
    
        e.Cancel = true; // prevent form closure
    
        base.OnFormClosing(e);
    }
    Here's an example, where if you click the X button it shows a dialog asking you to confirm you wish to exit:

    Code:
    protected override void OnFormClosing(FormClosingEventArgs e)
    {
        DialogResult result = MessageBox.Show("Are you sure you want to quit?", "Exit?", MessageBoxButtons.OKCancel);
    
        if (result != DialogResult.OK)
            e.Cancel = true;
        else
            base.OnFormClosing(e);
    }

  3. #3
    Registered User
    Join Date
    Jun 2009
    Posts
    21
    Great stuff.

    Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BN_CLICKED, change button style
    By bennyandthejets in forum Windows Programming
    Replies: 13
    Last Post: 07-05-2010, 11:42 PM
  2. Button and edit control border
    By maxorator in forum Windows Programming
    Replies: 4
    Last Post: 11-04-2005, 02:31 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. I need some ideas please>>
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 08-23-2002, 01:55 PM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM