Close ("X") Button

Top Score Score

Newcomer
Joined
Aug 25, 2003
Messages
17
G'day,

Is there anyway of when the close "X" button up in the top corner of the screen is clicked on, to stop the program being closed.

I have a msgbxo that asks to leave or not. If you click "No" it closes anyway how can it be stopped??

Thanks....TSS
 
Easiest way i find is to place the messagebox routine within the forms closing event, and using an if statement, if the messagebox result is no then cancel the close event.
 
stustarz said:
Easiest way i find is to place the messagebox routine within the forms closing event, and using an if statement, if the messagebox result is no then cancel the close event.

What close event do you use??
 
Something along these lines should be what your looking for:

Put this inside the Forms closing event:

Visual Basic:
        Dim MessageResponse As MessageBox
        If MessageResponse.Show("Message", "Caption", MessageBoxButtons.YesNo) = DialogResult.No Then
            e.Cancel = True
        End If

I dont actually have VS open at the moment so some of the syntax may be a little wrong.
 
stustarz said:
Something along these lines should be what your looking for:

Put this inside the Forms closing event:

Visual Basic:
        Dim MessageResponse As MessageBox
        If MessageResponse.Show("Message", "Caption", MessageBoxButtons.YesNo) = DialogResult.No Then
            e.Cancel = True
        End If

I dont actually have VS open at the moment so some of the syntax may be a little wrong.

It doesn't like the "e."
 
This is exactly how i have it on my form:
Visual Basic:
    Private Sub Main_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
        Dim MessageResponse As MessageBox
        If MessageResponse.Show("Message", "Caption", MessageBoxButtons.YesNo) = DialogResult.No Then
            e.Cancel = True
        End If
    End Sub

What have you got?
 
Visual Basic:
    Private Sub Main1_Closed(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Closed
        Var = MsgBox("Are You Sure You Want To Leave?", MsgBoxStyle.YesNo + MsgBoxStyle.Question + MsgBoxStyle.DefaultButton2)

        If Var = MsgBoxResult.No Then
            
        End If
 
Firstly, your code is in the closed event, it needs to be in the closing event.

Remove the code you have and replace it with the entire code (the one with the actual event handler aswell) and all should work fine.
 
Sorry in addition to above:

Its better practice to use the .NET MessageBox rather than MsgBox (which requires the Microsoft.VisualBasic namespace) ;)
 
I have done all that.

It is changing the close variable to true, but conntinuing on with the sub. It is not stopping there.

stustarz said:
Sorry in addition to above:

Its better practice to use the .NET MessageBox rather than MsgBox (which requires the Microsoft.VisualBasic namespace) ;)
It was originaly build in Visual Basic and was converted, thats why all the MsgBox's are still there.
 
If the code for your forms closing event is exactly the same as the code i have posted then there is no reason why it won't work. :)
 
Visual Basic:
Private Sub Main1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
        Dim MessageResponse As MessageBox
        If MessageResponse.Show("Are You Sure You Want To Leave?", "Top Score Scorebook", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) = DialogResult.No Then
            e.Cancel = True
        End If

Thats the code that is there.
 
No error.

It is just continuing on with the Sub, it is not stopping at that point??

It is running the IF statement and setting the variable properties but that is it, doesn't exit the sub.
 
Ive attached a project, with one form and just the code in the closing event to demonstrate how this will work. Open the project in Visual Studio and just press F5 to run, try and close the form with the X.

This works fine for me, sorry! In all honesty without looking at your entire project it is a bit difficult to work out what could be happening, something overriding the default closeing methods maybe?
 

Attachments

That one works fine??

Visual Basic:
Private Sub Main1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
        Dim MessageResponse As MessageBox
        If MessageResponse.Show("Are You Sure You Want To Leave?", "Top Score Scorebook", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) = DialogResult.No Then
            e.Cancel = True
        End If

        If GameRun = False Then
            Application.Exit()
        End If
        If GameRun = True Then
            Dim MyFile As Object
            Dim fs As Object
            Dim var1 As String
            Dim DataFile
            Dim Backup As New Backup()
            SaveStats()
            fs = CreateObject("Scripting.FileSystemObject")
            MyFile = Dir(FileLocation & "*.dat", FileAttribute.Normal)
            DataFile = Dir(FileLocation & "\Backup", FileAttribute.Directory)
            If DataFile = "" Then
                MkDir(FileLocation & "\Backup")
            End If
            fs.CopyFile(FileLocation & "*.*", FileLocation & "\Backup")
            Var = MsgBox("Would You Like To Backup?", MsgBoxStyle.YesNo + MsgBoxStyle.Question + MsgBoxStyle.DefaultButton2)
            If Var = MsgBoxResult.No Then
                Application.Exit()
            End If
            If Var = MsgBoxResult.Yes Then
                Backup.Show()
                Exit Sub
            End If

            On Error GoTo ErrorFix
            SaveVars()
            SaveData()
            SaveBowlingStats()
            SaveMatchStats()
            SaveHomeTeam()
            SaveAwayTeam()
            SaveTeamData()
        End If
ErrorFix:
        If Err.Number = 53 Then
            SaveData()
            Application.Exit()
        End If

    End Sub

That is the code. It is a little messy at the moment.
 
OK! i didnt realise you had more code within the closing event, this will always execute because if the user clicks 'Yes' to state they want to exit, the closing event will run to the end.....

Change the code i gave you to this:

Visual Basic:
        Dim MessageResponse As MessageBox
        If MessageResponse.Show("Are You Sure You Want To Leave?", "Top Score Scorebook", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) = DialogResult.No Then
            e.Cancel = True
        Else
            Exit Sub
        End If

This way when they choose 'yes' the sub will be forced to exit
 
Back
Top