Is there anyway that i can disable the X close button but i still want to have maximize and minimize. I just want X to be either gone or X to be grayed out so it can't be used.
Class CloseButton Private Declare Function GetSystemMenu Lib "user32" (ByVal hwnd As Integer, ByVal revert As Integer) As Integer Private Declare Function EnableMenuItem Lib "user32" (ByVal menu As Integer, ByVal ideEnableItem As Integer, ByVal enable As Integer) As Integer Private Const SC_CLOSE As Integer = &HF060 Private Const MF_BYCOMMAND As Integer = &H0 Private Const MF_GRAYED As Integer = &H1 Private Const MF_ENABLED As Integer = &H0 Private Sub New() End Sub Public Shared Sub Disable(ByVal form As System.Windows.Forms.Form) ' The return value specifies the previous state of the menu item (it is either ' MF_ENABLED or MF_GRAYED). 0xFFFFFFFF indicates that the menu item does not exist. Select Case EnableMenuItem(GetSystemMenu(form.Handle.ToInt32, 0), SC_CLOSE, MF_BYCOMMAND Or MF_GRAYED) Case MF_ENABLED Case MF_GRAYED Case &HFFFFFFFF Throw New Exception("The Close menu item does not exist.") Case Else End Select End Sub
End
Class
try this in the form load event CloseButton.Disable(me)
Protected Overrides ReadOnly Property CreateParams() As CreateParams Get Dim cp As CreateParams = MyBase.CreateParams Const CS_NOCLOSE As Integer = &H200
Ok the code that you posted doesn't work in vb 2005, but indeed if you can give me a working code for the one you posted and maybe an idea on how to go about changing the behaviour
Does anyone have an idea on how to make this work, again i want to disable the x close button and i dont want it to be visible on the form but i do want the minimize on there so the control box is not an option for me. Is this possible
Why don't you simply change the behaviour of your X button Instead of removing it, assign another function to it (minimize window, message box, ...). Why waste a button
I'm sorry, the code was in C#. The VB equivalent would be:
Private Sub Form1_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing If e.CloseReason = CloseReason.UserClosing Then WindowState = FormWindowState.Minimized e.Cancel = True End If End Sub
Basically, what this snippet does, is preventing the form from closing, (e.Cancel = True) and instead it minimizes it (WindowState change). Note that it not only affects the X button but prevents the form from closing at all (I can't say if this is the behaviour you want).
Disable the X
Lasse Steenberg
Option Explicit On
Class CloseButton Private Declare Function GetSystemMenu Lib "user32" (ByVal hwnd As Integer, ByVal revert As Integer) As IntegerOption Strict On
Public
Private Declare Function EnableMenuItem Lib "user32" (ByVal menu As Integer, ByVal ideEnableItem As Integer, ByVal enable As Integer) As Integer Private Const SC_CLOSE As Integer = &HF060
Private Const MF_BYCOMMAND As Integer = &H0
Private Const MF_GRAYED As Integer = &H1
Private Const MF_ENABLED As Integer = &H0 Private Sub New()
End Sub Public Shared Sub Disable(ByVal form As System.Windows.Forms.Form)
' The return value specifies the previous state of the menu item (it is either
' MF_ENABLED or MF_GRAYED). 0xFFFFFFFF indicates that the menu item does not exist.
Select Case EnableMenuItem(GetSystemMenu(form.Handle.ToInt32, 0), SC_CLOSE, MF_BYCOMMAND Or MF_GRAYED)
Case MF_ENABLED
Case MF_GRAYED
Case &HFFFFFFFF
Throw New Exception("The Close menu item does not exist.")
Case Else
End Select
End Sub
End
Classtry this in the form load event CloseButton.Disable(me)
uszaty1973
Or without the API:
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 PropertyBrian27
Its working !!
Grande TaDa !!!
Me salvaste la tarde....
Richard McEnery
jczarni
Vishal Chopra
do you also know the answer to make it completely gone i mean the grayout works perfect but for another application i like to have it gone.
FrankDip
jghfjg
This works great and does just what I want. I translated it into C#:
private const int CS_NOCLOSE = 0x200;
protected override CreateParams CreateParams
{
get
{
CreateParams cp = base.CreateParams;
cp.ClassStyle = cp.ClassStyle | CS_NOCLOSE;
return cp;
}
}
JWTK
Waldo6
Why don't you simply change the behaviour of your X button Instead of removing it, assign another function to it (minimize window, message box, ...). Why waste a button
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
if (e.CloseReason == CloseReason.UserClosing)
{
this.WindowState = FormWindowState.Minimized;
e.Cancel = true;
}
}
simple
frm.controlbox = false
LizO
Closing event of a form as that signature :
Private Sub frmConsigne_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
' Code here
End Sub
Therefore, "CloseReason" is not a member of System.ComponentModel.CancelEventArgs
How to deal with then
Thanks in advance.
With regards,
AlkPonn
Gordon Asbach
I'm sorry, the code was in C#. The VB equivalent would be:
Private Sub Form1_FormClosing(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
If e.CloseReason = CloseReason.UserClosing Then
WindowState = FormWindowState.Minimized
e.Cancel = True
End If
End Sub
Basically, what this snippet does, is preventing the form from closing, (e.Cancel = True) and instead it minimizes it (WindowState change). Note that it not only affects the X button but prevents the form from closing at all (I can't say if this is the behaviour you want).
raaman
hi,
I tried out this coding. but i am getting this error.
'User.CreateParams': no suitable method found to override
Can u suggest some solution for this.
Thanks..