Home > Info.Tech, Software Development, Visual C# > Visual C#: Handling X button (top-right)

Visual C#: Handling X button (top-right)

December 7th, 2009

xbuttonDisabling the X button

private const int CP_NOCLOSE_BUTTON = 0x200;

protected override CreateParams CreateParams
{
     get
     {
        CreateParams myCp = base.CreateParams;
        myCp.ClassStyle = myCp.ClassStyle | CP_NOCLOSE_BUTTON;
        return myCp;
     }
}

Add confirmation if window closing or closed…

Insert this code to Main form or the Form1.cs


private void Form1_Closing(object sender,
                   System.ComponentModel.CancelEventArgs e)
{
     if (MessageBox.Show("Are you sure you want to exit?", "Confirm exit",
         MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
     {
        e.Cancel = true;
     }
}

Insert this to Form1.Designer.cs…

this.Closing += new System.ComponentModel.CancelEventHandler(
                             this.frmCCS_Closing);

Camilo III Info.Tech, Software Development, Visual C#

  1. December 23rd, 2009 at 21:55 | #1

    First of all. Thanks very much for your useful post.

    I just came across your blog and wanted to drop you a note telling you how impressed I was with the

    information you have posted here.

    Please let me introduce you some info related to this post and I hope that it is useful for community.

    There is a good C# resource site, Have alook

    CSharpTalk.com

    Thanks again
    Rahul

  1. No trackbacks yet.