save before exit

xswzaq

Well-known member
Joined
Jul 9, 2007
Messages
61
Programming Experience
Beginner
1. I got this code below to handle my save before exit. But whenver I click the red (x) on form, but it did not do anything. Whether I click Yes/No/Cancel, it will only close the program. I don’t see what I do wrong that make it behavior like this. Please guide me to right direction.
2. Is there a way to make the messagebox show only when user make change. Like if they click savebtn then click close (x) the message will not show up, but the message going to show up if user make change and did not click savebtn before close.

Private Sub Detail_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Dim response As DialogResult
MessageBox.Show("Do you want to save any changes before closing this window?", "DTL", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button3)
If response = Windows.Forms.DialogResult.Cancel Then
Me.TextBox1.Focus()
ElseIf response = Windows.Forms.DialogResult.No Then
Me.Close()
ElseIf response = Windows.Forms.DialogResult.Yes Then
Try
Me.Validate()
Me.DTLBindingSource.EndEdit()
Me. DTLTableAdapter.Update(Me.PtmnDataSet.DTL)
Me.Close()
Catch ex As Exception
MessageBox.Show("Unable to Save! " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End If
End Sub
 
VB.NET:
dim response as integer

1+1

if response = 2 then ....
is response 2 here ?


now what if you had done something like this:
VB.NET:
response = 1 + 1
would response be 2 now?
 
Why not do something like this, it is one way to handle it.

Declare a global variable as a Boolean

VB.NET:
Private hasChanged As Boolean = False

For exiting the form

VB.NET:
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        Try
            If hasChanged Then
                Dim yesNo As MsgBoxResult = MsgBox("Your changes have not been saved, are you sure you want to exit?", MsgBoxStyle.YesNo, "Yes, No")
                If yesNo = MsgBoxResult.Yes Then
                    Me.Close()
                Else
                    Exit Sub
                End If
            Else
                Me.Close()
            End If
        Catch ex As Exception
            MessageBox.Show("An error occured in frmEmployees. Method: btnExit_Click. Error: " & ex.Message)
        End Try
        Me.Close()
    End Sub

In your controls you could add this

VB.NET:
Private Sub txtEmployeeName_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtEmployeeName.TextChanged
        hasChanged = True
    End Sub

This works fine in my apps

CoachBarker
 
Why not do something like this, it is one way to handle it.

Declare a global variable as a Boolean

VB.NET:
Private hasChanged As Boolean = False

For exiting the form

VB.NET:
Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
        Try
            If hasChanged Then
                Dim yesNo As MsgBoxResult = MsgBox("Your changes have not been saved, are you sure you want to exit?", MsgBoxStyle.YesNo, "Yes, No")
                If yesNo = MsgBoxResult.Yes Then
                    Me.Close()
                Else
                    Exit Sub
                End If
            Else
                Me.Close()
            End If
        Catch ex As Exception
            MessageBox.Show("An error occured in frmEmployees. Method: btnExit_Click. Error: " & ex.Message)
        End Try
        Me.Close()
    End Sub

In your controls you could add this

VB.NET:
Private Sub txtEmployeeName_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtEmployeeName.TextChanged
        hasChanged = True
    End Sub

This works fine in my apps

CoachBarker

Instead of putting all that checkin into btnExit, I would put it into the Form's Closing event. If the form is not supposed to close, then set e.Cancel = True to keep the form from close, otherwise simply let the code reach 'End Sub' for the form to still close
 
Why not do something like this, it is one way to handle it.

Declare a global variable as a Boolean

VB.NET:
Private hasChanged As Boolean = False
..snip..

With datasets, you wouldnt do that. The dataset is the destination for data changes, and it tracks them.. your method would require you add a handler to every textbox. You'd generate far simpler code simply by asking dataSet.HasChanges
 
xswzaq:
First off, dont use QUOTE tags for code. Use CODE tags

1. I got this code below to handle my save before exit. But whenver I click the red (x) on form, but it did not do anything. Whether I click Yes/No/Cancel, it will only close the program. I don’t see what I do wrong that make it behavior like this. Please guide me to right direction.

You should have this:
VB.NET:
Private Sub Detail_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

[B]'if there are no changes, dont even bother asking the user
If Not Me.PtmnDataSet.HasChanges Then Return[/B]

[B]
'this is what john was getting at - you never assign the response of the dialog to the response variable![/B]
[B]Dim response As DialogResult = MessageBox.Show("Do you want to save any changes before closing this window?", _ 
  "DTL", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question, MessageBoxDefaultButton.Button3)[/B]

If response = Windows.Forms.DialogResult.Cancel Then
[B]e.Cancel = True 'cancel the closing event
 [COLOR="Red"] 'ElseIf response = Windows.Forms.DialogResult.No Then -- this is pointless statement, remove it[/COLOR][/B]
Else[B] 'the only other result possible is YES[/B]
 Try
  Me.Validate()
  Me.DTLBindingSource.EndEdit()
  Me. DTLTableAdapter.Update(Me.PtmnDataSet.DTL)
  [B]'Me.Close() -- you dont need to do this! the form is already closing![/B]
 Catch ex As Exception
  MessageBox.Show("Unable to Save! " & ex.Message, Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error)
  [B]e.Cancel = True 'maybe we should cancel the closing if we couldnt save?[/B]
 End Try
End If

End Sub


2. Is there a way to make the messagebox show only when user make change. Like if they click savebtn then click close (x) the message will not show up, but the message going to show up if user make change and did not click savebtn before close.

See the first line of my adjusted code
 
Just offering advice based on my limited knowledge, and what has worked on projects we completed while in school, unfortunately I am still learning, and as most of you know it is a steep learning curve. Sometimes what works for me is much more simplistic than it needs to be and sometimes you need to understand simple before you can grasp complex.

I do find that the comments by the more experienced programmers that follow a post I make, point me in the right direction when I am looking to write much more complex code.

We all need experience to learn and this is one of those places where we can usually count on the experience of the professionals to steer us in the right direction.

CoachBarker
 
Back
Top