"X" button clicked

  • 18 years ago

    Salute..
    How can I know if the user closed the form using the "X" button?..
    Thanks

  • 18 years ago

    The easiest way I can come up with is to have a form boolean variable, NotX, say initialised to false. Then when someone closes your form using a button you have provided you can set it to true:-


    NotX = True
    Me.Close


    You can then use the form's closing event to check this:-


    If NotX = False then
       MsgBox("You Pressed the X button to close this form!")
    End If

  • 18 years ago

    well I think that thier is an EASIER WAY!..

    I found it after I posted..I think I am posting prematurely !!...
    U can do the following:
    In the FORM's CLOSING event handler u can insert the following code :
           If DialogResult = DialogResult.Cancel Then
               e.Cancel = True
           End If
    This will disable closing the form using the X button
    Salute..

  • 18 years ago

    Da_Retina> well heres an 'even' better way of stopping ppl clicking on the X :


    Code:

    Private Declare Function GetSystemMenu Lib "user32" (ByVal hwnd As Long, ByVal bRevert As Long) As Long
    Public Sub RemoveCloseItem(lHwnd As Long)
       Dim lMenu As Long
       lMenu = GetSystemMenu(lHwnd, False)
       DeleteMenu lMenu, 6, &H400&
    End Sub


    just call like this in the Form_Load:


    RemoveCloseItem me.hwnd


    Hope that helps!


    Good Luck!

  • 18 years ago

    websoftware: that looks suspiciously like VB code to me... this is the VB.net forum!!!


    ... and also, Da_Retina didn't actually say he wanted to stop people clicking the X... he just wanted to know when they did

  • 18 years ago

    dammnation


    I'm going blinder every day!


    Whooops! I'll look@the top b4 posting.


    My apologies,


    Thanks Chap

  • 18 years ago

    Strange I tried the DialogResult and it returned false no matter how the form was closing. I don't consider my solution that difficult anyway

  • 18 years ago

    i agree with musician!!

  • 18 years ago


    Everyone should feel fine about posting VB6 code in
    the .NET forum, since VB6 code is easily convertable by hand
    like so:


    Private Declare Function GetSystemMenu Lib "user32" (ByVal hwnd As Long, ByVal bRevert As Long) As Long
    Public Sub RemoveCloseItem(lHwnd As Long)
      Dim lMenu As Long
      lMenu = GetSystemMenu(lHwnd, False)
      DeleteMenu lMenu, 6, &H400&
    End Sub


    to:


    Declare Function GetSystemMenu Lib "user32" (ByVal hwnd As Integer, ByVal bRevert As Integer) As Integer
    Public Sub RemoveCloseItem(Byval lHwnd As Integer)
      Dim lMenu As Integer
      lMenu = GetSystemMenu(lHwnd, False)
      DeleteMenu(lMenu, 6, &H400I)
    End Sub


    no biggie




    Just my thoughts


    Mike

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“There's no test like production” - Anon