Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to hide only the Close (x) button?

Tags:

c#

winforms

I have a modal dialog, and need to hide the Close (X) button, but I cannot use ControlBox = false, because I need to keep the Minimize and Maximize buttons.

I need to hide just Close button, is there any way to do that?

Thanks a lot!

Update: I had permission to disable it, which is simpler :) Thanks all!

like image 933
stefano m Avatar asked Sep 04 '11 20:09

stefano m


People also ask

How do I disable the close button?

Answer: To disable the Close button on an Access form, open the form in Design view. Under the View menu, select Properties. When the Properties window appears, set the "Close Button" property to No.

How to remove close button in Windows Form?

Solution 2 the first one is to set the border style to "none" in the properties menu, however this will completely remove all buttons and the "windows header thingy" in the top on the window. the second method is to set the controlBox property to false. this will remove all buttons but you'll keep the header.

How to Disable button in c# Windows Form?

Enabled = false; this. Controls. Add(DownloadButton); It will make the button fade out and also disabled.


5 Answers

We can hide close button on form by setting this.ControlBox=false;

Note that this hides all of those sizing buttons. Not just the X. In some cases that may be fine.

like image 180
Abhishek.Chopra Avatar answered Oct 03 '22 14:10

Abhishek.Chopra


You can't hide it, but you can disable it by overriding the CreateParams property of the form.

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;
    }
}

Source: http://www.codeproject.com/KB/cs/DisableClose.aspx

like image 39
Daniel A. White Avatar answered Oct 03 '22 13:10

Daniel A. White


Well, you can hide it, by removing the entire system menu:

private const int WS_SYSMENU = 0x80000;
protected override CreateParams CreateParams
{
    get
    {
        CreateParams cp = base.CreateParams;
        cp.Style &= ~WS_SYSMENU;
        return cp;
    }
}

Of course, doing so removes the minimize and maximize buttons.

If you keep the system menu but remove the close item then the close button remains but is disabled.

The final alternative is to paint the non-client area yourself. That's pretty hard to get right.

like image 31
David Heffernan Avatar answered Oct 03 '22 13:10

David Heffernan


If you really want to hide it, as in "not visible", then you will probably have to create a borderless form and draw the caption components yourself. VisualStyles library has the Windows Elements available. You would also have to add back in the functionality of re-sizing the form or moving the form by grabbing the caption bar. Not to mention the system menu in the corner.

In most cases, it's hard to justify having the "close" button not available, especially when you want a modal form with minimizing capabilities. Minimizing a modal form really makes no sense.

like image 42
LarsTech Avatar answered Oct 03 '22 13:10

LarsTech


Well you can hide the close button by changing the FormBorderStyle from the properties section or programmatically in the constructor using:

public Form1()
{
InitializeComponent();
this.FormBorderStyle = FormBorderStyle.None;
}

then you create a menu strip item to exit the application.

cheers

like image 32
Josh John Avatar answered Oct 03 '22 13:10

Josh John