Not Closing

LamKat

Newcomer
Joined
Dec 3, 2011
hey.
i'm making a very basic contacts type program, and one of the fetures is that when the user closes the form it checks is any information has not been saved. but for some reason once i placed this sub in it now doesn't close at all

Code:
Private Sub FormClosing() Handles Me.FormClosing
        Dim IsDiffrent As Boolean = True
        For Counter As Byte = 0 To COM_Name.Items.Count - 1
            If COM_Name.Text = COM_Name.Items.Item(Counter) Then
                Dim Information(0 To 7) As String
                Dim TxtReader As New StreamReader(My.Application.Info.DirectoryPath & "\" & COM_Name.Text & ".txt")
                For InfoCounter As Byte = 0 To 7
                    Information(InfoCounter) = TxtReader.ReadLine
                Next
                TxtReader.Close()
                If Information(0) = TXT_HomePhone.Text And _
                    Information(1) = TXT_MobPhone.Text And _
                    Information(2) = DTP_Birthday.Value And _
                    Information(3) = TXT_HomeAddress.Lines(1) And _
                    Information(4) = TXT_HomeAddress.Lines(2) And _
                    Information(5) = TXT_HomeAddress.Lines(3) And _
                    Information(6) = TXT_HomeAddress.Lines(4) And _
                    Information(7) = TXT_Email.Text Then
                    IsDiffrent = False
                End If
            End If
        Next

        If IsDiffrent = True Then
            Dim Answer As Byte = MsgBox("Do you wish to Save Changes?", MsgBoxStyle.YesNo)
            If Answer = 6 Then
                Call Save(True, "Closing")
            End If
        Else
            Me.Close()
        End If
    End Sub

(please note that it is not even getting to the save changes msgbox)
 

snarfblam

Ultimate Contributor
Joined
Jun 10, 2003
Location
USA
(please note that it is not even getting to the save changes msgbox)

So, if you step through with the debugger, where does it get to? I don't see an obvious reason that the form is not closing, but I did notice this:

Code:
        If IsDiffrent = True Then
            Dim Answer As Byte = MsgBox("Do you wish to Save Changes?", MsgBoxStyle.YesNo)
            If Answer = 6 Then
                Call Save(True, "Closing")
            End If
        Else
            Me.Close()
        End If

This code seems odd for a FormClosing event handler. You shouldn't be calling Close in the FormClosing event handler; you're already closing (in fact, something bad happened when I called Me.Close in the FormClosing handler). If you want to let the form close, do nothing. If you want to prevent the form from closing, set e.Cancel to true. In order to get access to 'e', you need to use the complete event signature for your handler, but it doesn't look like you actually intend to prevent the form from closing.

Code:
' change
Private Sub FormClosing() Handles Me.FormClosing
' to
Private Sub FormClosing(ByVal sender As Object, ByVal e As FormClosingEventArgs) Handles Me.FormClosing

The best suggestion I can offer is to use the debugger. Step through the code and find out what it's doing that it shouldn't be doing.

On an unrelated note, why code like this:
Code:
Dim Answer As Byte = MsgBox("Do you wish to Save Changes?", MsgBoxStyle.YesNo)
If Answer = 6 Then
when you can code like this?
Code:
Dim Answer As MsgBoxResult = MsgBox("Do you wish to Save Changes?", MsgBoxStyle.YesNo)
If Answer = MsgBoxResult.Yes Then
That code tells me you aren't using Option Strict. I would very, very strongly advise you to use Option Strict. It might seem a bit inconvenient until you get used to it, but Option Strict is a good thing.
 
Top Bottom