471,050 Members | 1,442 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,050 software developers and data experts.

How to remove or disable Close (X)-button of the Form?

Customer wants the Close (X)-button of the forms top right corner to be
Disabled or removed from Windows Form-application, which is made using
VB.NET, but leave MinimizeBox and MaximizeBox.

I think this is not so clever to do like this way, but have to do like
customer wants - so how to do this? It seems to be not just like
changing property like with MinimizeBox and MaximizeBox. Any succestions?

--
Thanks in advance!

Mika
Nov 21 '05 #1
12 64584
Mika,

When you are sure your users wants it and you are not distributing the
program too others, (because otherwise you will most probably directly be
confrontated with bug reports which are probably justifiable correct) you
can just use the closing event and set in that e.cancel = true (or false I
always am in doubt about the last just try).

I would in your situation let the user sign a contract that he wanted this.
What you can do as well is showing in a the messagebox from the clossing
event that this is by order from your client disabled. (A little more polite
however not to misunderstand text than of course).

Instead of that all you can as well overpaint the X (and keep the cancel)
however that will be a lot more work.

I hope this helps?

Cor
Nov 21 '05 #2
#Region " Windows Form Designer generated code "

'disables close
Protected Overrides ReadOnly Property CreateParams() As CreateParams
Get
Dim cp As CreateParams = MyBase.CreateParams
Const CS_NOCLOSE As Integer = &H200
cp.ClassStyle = cp.ClassStyle Or CS_NOCLOSE
Return cp
End Get
End Property
"Mika M" wrote:
Customer wants the Close (X)-button of the forms top right corner to be
Disabled or removed from Windows Form-application, which is made using
VB.NET, but leave MinimizeBox and MaximizeBox.

I think this is not so clever to do like this way, but have to do like
customer wants - so how to do this? It seems to be not just like
changing property like with MinimizeBox and MaximizeBox. Any succestions?

--
Thanks in advance!

Mika

Nov 21 '05 #3
Mark,

Nice, I never saw this one before (is now in my snippets)

Thanks

Cor
Nov 21 '05 #4
"Mika M" <mahmik_nospam@removethis_luukku.com> schrieb:
Customer wants the Close (X)-button of the forms top right corner to be
Disabled or removed from Windows Form-application, which is made using
VB.NET, but leave MinimizeBox and MaximizeBox.


Add this to your form:

\\\
Protected Overrides ReadOnly Property CreateParams() As CreateParams
Get
Dim cp As CreateParams = MyBase.CreateParams
Const CS_DBLCLKS As Int32 = &H8
Const CS_NOCLOSE As Int32 = &H200
cp.ClassStyle = CS_DBLCLKS Or CS_NOCLOSE
Return cp
End Get
End Property
///

Alternatively you can remove the according menu items from the system
menu using p/invoke.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Nov 21 '05 #5
> #Region " Windows Form Designer generated code "

'disables close
Protected Overrides ReadOnly Property CreateParams() As CreateParams
Get
Dim cp As CreateParams = MyBase.CreateParams
Const CS_NOCLOSE As Integer = &H200
cp.ClassStyle = cp.ClassStyle Or CS_NOCLOSE
Return cp
End Get
End Property


Thanks Mark! Seems to work like I need!

--
Mika
Nov 21 '05 #6
I had quite a discussion about this in GotDotNet a few months back:
http://www.gotdotnet.com/Community/M...&Page=1#248801

If you never want the Close Button to show then use the CreateParams method,
but if you want to toggle the state of the close button then this method is
no good, you will instead have to modify the Window Menu.

--
Mick Doherty
http://dotnetrix.co.uk/nothing.html
"Cor Ligthert" <no************@planet.nl> wrote in message
news:%2****************@tk2msftngp13.phx.gbl...
Mika,

When you are sure your users wants it and you are not distributing the
program too others, (because otherwise you will most probably directly be
confrontated with bug reports which are probably justifiable correct) you
can just use the closing event and set in that e.cancel = true (or false I
always am in doubt about the last just try).

I would in your situation let the user sign a contract that he wanted
this. What you can do as well is showing in a the messagebox from the
clossing event that this is by order from your client disabled. (A little
more polite however not to misunderstand text than of course).

Instead of that all you can as well overpaint the X (and keep the cancel)
however that will be a lot more work.

I hope this helps?

Cor

Nov 21 '05 #7
....and I thought you were always paying attention ;)

--
Mick Doherty
http://dotnetrix.co.uk/nothing.html
"Cor Ligthert" <no************@planet.nl> wrote in message
news:OJ**************@TK2MSFTNGP09.phx.gbl...
Mark,

Nice, I never saw this one before (is now in my snippets)

Thanks

Cor

Nov 21 '05 #8
Mick,

When I saw your article I thought I have to say Sorry Sorry Sorry however
you gave me the change here.

Until now I thought that I had seen only awfull painting solutions, because
this simple one I would have remembered me I thought.

Again Sorry Sorry Sorry

:-)))

Cor
Nov 21 '05 #9
No need to apologise. I just couldn't believe you hadn't seen it before
since it's been here several times in the past and I'm almost positive that
you've read every message since you started interacting with the group. By
the amount of responses you give it certainly looks that way.

--
Mick Doherty
http://dotnetrix.co.uk/nothing.html
"Cor Ligthert" <no************@planet.nl> wrote in message
news:un**************@TK2MSFTNGP10.phx.gbl...
Mick,

When I saw your article I thought I have to say Sorry Sorry Sorry however
you gave me the change here.

Until now I thought that I had seen only awfull painting solutions,
because this simple one I would have remembered me I thought.

Again Sorry Sorry Sorry

:-)))

Cor

Nov 21 '05 #10
Declarations:

Private Declare Function GetSystemMenu Lib "user32" Alias
"GetSystemMenu" (ByVal hwnd As Integer, ByVal bRevert As Integer) As Integer
Private Declare Function DeleteMenu Lib "user32" Alias "DeleteMenu"
(ByVal hMenu As Integer, ByVal nPosition As Integer, ByVal wFlags As Integer)
As Integer

Private Const MF_BYPOSITION As Integer = &H400&
Put this in form load:

DeleteMenu(iMenu, 6, MF_BYPOSITION) ' Disable the 'X' (Control Box)
DeleteMenu(iMenu, 5, MF_BYPOSITION) ' Remove the seperator bar

The above will disable your ControlBox for you

"Herfried K. Wagner [MVP]" wrote:
"Mika M" <mahmik_nospam@removethis_luukku.com> schrieb:
Customer wants the Close (X)-button of the forms top right corner to be
Disabled or removed from Windows Form-application, which is made using
VB.NET, but leave MinimizeBox and MaximizeBox.


Add this to your form:

\\\
Protected Overrides ReadOnly Property CreateParams() As CreateParams
Get
Dim cp As CreateParams = MyBase.CreateParams
Const CS_DBLCLKS As Int32 = &H8
Const CS_NOCLOSE As Int32 = &H200
cp.ClassStyle = CS_DBLCLKS Or CS_NOCLOSE
Return cp
End Get
End Property
///

Alternatively you can remove the according menu items from the system
menu using p/invoke.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #11
"Crouchie1998" <Cr**********@discussions.microsoft.com> schrieb:
Private Declare Function GetSystemMenu Lib "user32" Alias
"GetSystemMenu" (ByVal hwnd As Integer, ByVal bRevert As Integer) As
Integer
Private Declare Function DeleteMenu Lib "user32" Alias "DeleteMenu"
(ByVal hMenu As Integer, ByVal nPosition As Integer, ByVal wFlags As
Integer)
As Integer

Private Const MF_BYPOSITION As Integer = &H400&
Put this in form load:

DeleteMenu(iMenu, 6, MF_BYPOSITION) ' Disable the 'X' (Control Box)
DeleteMenu(iMenu, 5, MF_BYPOSITION) ' Remove the seperator bar

The above will disable your ControlBox for you


ACK. I have published a similar solution on my website some time ago
(<URL:http://dotnet.mvps.org/dotnet/code/windowsandforms/#NoClose>), but I
prefer the 'CreateParams' solution.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #12
I first used the above solution in VB 6, but 2 years ago when I moved to
VB.NET I charged the code accordingly.

"Herfried K. Wagner [MVP]" wrote:
"Crouchie1998" <Cr**********@discussions.microsoft.com> schrieb:
Private Declare Function GetSystemMenu Lib "user32" Alias
"GetSystemMenu" (ByVal hwnd As Integer, ByVal bRevert As Integer) As
Integer
Private Declare Function DeleteMenu Lib "user32" Alias "DeleteMenu"
(ByVal hMenu As Integer, ByVal nPosition As Integer, ByVal wFlags As
Integer)
As Integer

Private Const MF_BYPOSITION As Integer = &H400&
Put this in form load:

DeleteMenu(iMenu, 6, MF_BYPOSITION) ' Disable the 'X' (Control Box)
DeleteMenu(iMenu, 5, MF_BYPOSITION) ' Remove the seperator bar

The above will disable your ControlBox for you


ACK. I have published a similar solution on my website some time ago
(<URL:http://dotnet.mvps.org/dotnet/code/windowsandforms/#NoClose>), but I
prefer the 'CreateParams' solution.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>

Nov 21 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Matt | last post: by
5 posts views Thread by Jonas | last post: by
3 posts views Thread by =?Utf-8?B?bWFydGluMQ==?= | last post: by
reply views Thread by Garg | last post: by
9 posts views Thread by Laurent ARNAL | last post: by
reply views Thread by =?iso-8859-1?B?S2VyZW0gR/xtcvxrY/w=?= | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.