Close box (x) on winform 
Author Message
 Close box (x) on winform

Is there a way to make the close box not an option on a winform without
getting rid of the minimize and Maximize buttons too?  If not, how does one
nullify or trap for the clicking of the x (close) button on a winform.


Tue, 20 Sep 2005 09:38:20 GMT  
 Close box (x) on winform
Hello Woody,


Quote:
> Is there a way to make the close box not an option
> on a winform without getting rid of the minimize and
> Maximize buttons too?  If not, how does one nullify
> or trap for the clicking of the x (close) button on a winform.

\\\
Private Declare Function GetSystemMenu Lib "user32.dll" ( _
    ByVal hWnd As IntPtr, _
    ByVal bRevert As Int32 _
) As IntPtr
Private Declare Function GetMenuItemCount Lib "user32.dll" ( _
    ByVal hMenu As IntPtr _
) As Int32
Private Declare Function DrawMenuBar Lib "user32.dll" ( _
    ByVal hWnd As IntPtr _
) As Int32
Private Declare Function RemoveMenu Lib "user32.dll" ( _
    ByVal hMenu As IntPtr, _
    ByVal nPosition As Int32, _
    ByVal wFlags As Int32 _
) As Int32

Private Const MF_BYPOSITION As Int32 = &H400
Private Const MF_REMOVE As Int32 = &H1000

Private Sub RemoveCloseButton(ByVal frmForm As Form)
    Dim hMenu As IntPtr, n As Int32
    hMenu = GetSystemMenu(frmForm.Handle, 0)
    If Not hMenu.Equals(IntPtr.Zero) Then
        n = GetMenuItemCount(hMenu)
        If n > 0 Then
            RemoveMenu(hMenu, n - 1, MF_BYPOSITION Or MF_REMOVE)
            RemoveMenu(hMenu, n - 2, MF_BYPOSITION Or MF_REMOVE)
            DrawMenuBar(Me.Handle)
        End If
    End If
End Sub
///

Sample usage:

\\\
RemoveCloseButton(Me)
///

Regards,
Herfried K. Wagner



Tue, 20 Sep 2005 10:13:36 GMT  
 Close box (x) on winform
Woody,

Unfortunately, you cannot do away with the "x" (close) button on a WinForm.
But, you can definitely handle it thru code.

The form has a Form.Closing event, in which you can cancel the closing
process. It receives an arg of tye CancelEventArgs. Set the "Cancel"
property of the CancelEventArgs variable to true and your form will remain
open.

But then, the question is, how do you "really" close it? For this, you will
have to come up with a logic in the Form.Closing event and do the needful.

Hope this helps.

Ajay Singala [Synergetics].


Quote:
> Is there a way to make the close box not an option on a winform without
> getting rid of the minimize and Maximize buttons too?  If not, how does
one
> nullify or trap for the clicking of the x (close) button on a winform.



Tue, 20 Sep 2005 16:21:39 GMT  
 Close box (x) on winform
I realize this question is real basic but I am still shakey with VS.  What
is the syntax for writing the addHandler in this case?  I tried something
like:

AddHandler me.Closing(byVal frmClosing as Object, _
            ByVal EventData as CancelEventArgs)

But this obviously didn't work.



Wed, 21 Sep 2005 08:34:19 GMT  
 Close box (x) on winform
Hello Woody,


Quote:
> I realize this question is real basic but I am still shakey
> with VS.  What is the syntax for writing the addHandler

\\\
AddHandler Me.Closing, AddressOf Me.Form1_Closing
.
.
.
Private Sub Form1_Closing( _
    ByVal sender As Object, _
    ByVal e As System.ComponentModel.CancelEventArgs _
)
    MsgBox("Closing")
    e.Cancel = True
End Sub
///

-- or --

\\\
Private Sub Form1_Closing( _
    ByVal sender As Object, _
    ByVal e As System.ComponentModel.CancelEventArgs _
) Handles MyBase.Closing
    MsgBox("Closing")
    e.Cancel = True
End Sub
///

Regards,
Herfried K. Wagner



Wed, 21 Sep 2005 08:45:30 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Close from Control Box Doesn't Close Application

2. how to close a dialogue box?

3. Close sorting/grouping box

4. Close sorting/grouping box

5. problem with closing dialog box

6. Save and Close Control in the Tool Box

7. dialog boxes on closing

8. Close a dialog box

9. Testing a Dialog Box for Close (x)

10. How handle user selecting close box.

11. save inputs and close dialog box?

12. Closing from control box causes error

 

 
Powered by phpBB® Forum Software