SAVE and EXIT Button

jtoutou

Active member
Joined
Oct 11, 2008
Messages
26
Programming Experience
Beginner
Good Day to everyone...
So Here is my problem.
I Have a Windows Form -Personnel form ..
After you finish entering data there is a save command button become visible.this Save Btn works properly with this Function :
VB.NET:
Private Function Save() As Boolean
        Dim Saved As Boolean = False
        	If MyDataSet.HasChanges Then
            Try
         Dim MyTableUpdates() As DataRow = MyDataSet.Mytable.Select("", "", DataViewRowState.Added Or DataViewRowState.ModifiedCurrent)

MyTableAdapter.Update(MyTableUpdates)
Saved = True

            Catch ex As Exception
                MsgBox(ex.ToString)

            End Try
        End If
return saved
    End Function

Private Sub ButtonSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

        Me.Validate()
        Me.MyTableBindingSource.EndEdit()
If Me.Save() Then
            MsgBox("Changes Saved")
        End If
    End Sub

My problem is the EXIT Button
If you press it i want to prompt you for saving the data or not.There is a mistake in the following Code but i do not know where it is..

VB.NET:
Private Sub ButtonExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        If Me.Save() Then
            Me.Close()
        Else
            Dim result = MsgBox("Do you want to save before closing?", MsgBoxStyle.YesNo Or MsgBoxStyle.Exclamation)

            If result = vbYes Then
                Me.Save()

                If result = vbNo Then
                    Me.Close()

                End If
            End If
        End If


    End Sub


I would be happy if someone would give me a guideline..thanx in advance..
 
"Do you want to save before closing?"
Are you trying to ask two different questions here? Because you know you can only get one answer. It looks like that is the confusion from your code.

The solution: You are asking about save or not save. And you will close no matter what the answer is, because the user clicked the "close" button.
 
Are you trying to ask two different questions here? Because you know you can only get one answer. It looks like that is the confusion from your code.

The solution: You are asking about save or not save. And you will close no matter what the answer is, because the user clicked the "close" button.

you are very right and i 'sory for the confusion
when the user clicks exit ,"IF" he doesn't save anything just to prompt him to save (or not) the data.
for beeing more specific...if the user does not enter anything in the form and press EXIT the msgbox has no reason to popup..

how can i do that ?
thank you
 
You mean like "If MyDataSet.HasChanges Then ask user if want to save"?
 
HasChanges method always exists, it returns a Boolean True/False value indicating whether there is changes.
 
Back
Top