private sub thanks() { Dim frm as new Form2 frm.MinimizeBox = False frm.MaximizeBox = False frm.ControlBox = False frm.Show() } |
Option Explicit On 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 |
then just call this on a form load or whatever... it worked for me when i tested it
This code allows the form to be closed by clicking a button on the form, but not by clicking the form's close button.
Private okToClose As Boolean = False Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click okToClose = True Me.Close() End Sub Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing If Not okToClose Then e.Cancel = True End If End Sub |
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 |
Hi
I think that the following is what you are wanting, try it please!
Create a new project and insert a new class that must have the following
Public
Class close_buttonPrivate Declare Function EnableMenuItem Lib "user32" (ByVal menu As Integer, ByVal ideEnableItem As Integer, ByVal enable As Integer) As IntegerPrivate Declare Function GetSystemMenu Lib "user32" (ByVal hwnd As Int32, ByVal bRevert As Boolean) As Int32Private Const SC_CLOSE As Integer = &HF060Private Const MF_BYCOMMAND As Integer = &H0Private Const MF_GRAYED As Integer = &H1Private Const MF_ENABLED As Integer = &H0Public Sub enable(ByVal form As Form)EnableMenuItem(GetSystemMenu(form.Handle,
False), SC_CLOSE, MF_ENABLED)End SubPublic Sub disable(ByVal form As Form)EnableMenuItem(GetSystemMenu(form.Handle,
False), SC_CLOSE, MF_GRAYED)End SubEnd
Classthen you are ready to use, on the form1 create 2 buttons and change do text to "enable" and "disable" respectivly, the code must be the following:
Public
Class Form1Dim close_button As New close_buttonPrivate Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Clickclose_button.enable(
Me)End SubPrivate Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Clickclose_button.disable(Me)
End SubEnd
Classif you want to use with another form put for example Form2.Handle intead of Me
Hello,
I have this working fine. I put the close_button.enable(Me) method in my load event. However, when I minimized the form, then maximized it, my close button went back to enabled. Is there a workaround for this?
Thanks.