John (Yiannis) Toutountzoglou
asked on
Save data On Exit (prompt)
I use a function For saving Data to dataset As the following code ...
I use Has Changes Method and saved data correctlly
The usual qusetion ...Prompt for save on exit .
The Boolean is Save...
The problem is when the form opens in addnew mode and you press Exit and no changes has made the message appears again...
I use Has Changes Method and saved data correctlly
The usual qusetion ...Prompt for save on exit .
The Boolean is Save...
The problem is when the form opens in addnew mode and you press Exit and no changes has made the message appears again...
Private Function Save() As Boolean
Dim Saved As Boolean = False
If MyDataSet.HasChanges Then
Try
Dim myUpdates() As DataRow = MyDataSet.Personnel.Select("", "", DataViewRowState.Added Or DataViewRowState.ModifiedCurrent)
Me.MyTableAdapter.Update(MyUpdates)
Saved = True
Catch ex As Exception
'MsgBox(ex.Message)
End Try
End If
Return Saved
End Function
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
'button 3 =SAVE BUTTON
Me.Validate()
Me.MyBindingSource.EndEdit()
If Me.Save() Then
messagebox.show(".......")
end if
Private Sub Exit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Exit.Click
If Not Me.Save Then
Select Case MessageBox.Show("Exit Without SAVE...Do You Want to Save Data?", "Data Changes", MessageBoxButtons.YesNo, MessageBoxIcon.Warning)
Case Windows.Forms.DialogResult.Yes
Me.Validate()
Me.MyBindingSource.EndEdit()
Me.Save()
Me.hide
Case Windows.Forms.DialogResult.No
'-- do nothing
Me.Hide()
End Select
Else
Me.Hide()
End If
The problem is when the form opens in addnew mode and you press Exit and no changes has made the message appears again...
You can add an event handler to handle the keypress or textchanged event of all the text boxes and within the event set a flag to indicate that changes have been made.
ASKER
i am trying to set up a code but i thing that the HasChanges returns Always True...
Yes. I experienced this problem that HasChanges always returns true and GetChanges returns all the rows whether or not these are changed.
better way of designing is having a storage class which will be used to store the data and validate. when ever the user presses ADD you will create a storage object and all the textboxes and control state changes will result in property change in storage class. you can simply query the class for changes.
this will be useful for you to run any validations.
this will be useful for you to run any validations.
creating, using, and querying the class is more hassle than writing an event handler to textchanges or keypresses. Its a good design practice though.
ASKER
with the event handler i thing its better..but i must handle a lot of controls...
i'am trying to get another approach but seem to be a not as i want approach.
The first part is working if there are no changes...
But if there are no message appears..
i'am trying to get another approach but seem to be a not as i want approach.
The first part is working if there are no changes...
But if there are no message appears..
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Changes As Boolean = True
Dim xControl As Control
For Each xControl In Me.GroupBox1.Controls
If xControl.GetType Is GetType(TextBox) Then
If xControl.Text = String.Empty Then
Changes = False
Else
Return
End If
ElseIf xControl.GetType Is GetType(MaskedTextBox) Then
If xControl.Text = String.Empty Then
Changes = False
Else
Return
End If
ElseIf xControl.GetType Is GetType(ComboBox) Then
If xControl.Text = String.Empty Then
Changes = False
Else
Return
End If
End If
If Changes Then
Select Case MessageBox.Show("Exit Without SAVE...Do You Want to Save Data?", "Data Changes", MessageBoxButtons.YesNo, MessageBoxIcon.Warning)
Case Windows.Forms.DialogResult.Yes
Me.Validate()
Me.MyBindingSource.EndEdit()
If Me.Save() Then
MessageBox.Show("DataSaved")
Form_Main.Panel2.Controls.Remove(Me.Panel1)
End If
Case Windows.Forms.DialogResult.No
'-- do nothing
Me.MyDataSet.RejectChanges()
End Select
Else
Form_Main.Panel2.Controls.Remove(Me.Panel1)
end if
end sub
does Me.Validate check for all the valid values. if yes in that you verify if any changes are there from the old value as it has to be tested for updations. there you set the flag instead of in individual controls.
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
The Me.validate cheks all fields...couse all the controls are binding to a dataset..Can you be more clear about the second part of answer?
can you post the Me.Validate() method code
In the validate method set a flag to identify if any changes are there. at the end call for validation and check the flag if errors are there.
ASKER
i have no code for Validate() method I use it by default any time i want to update data.
Me.Validate()
Me.MyBindingSource.EndEdit ()
Me.MyTableAdapter.Update(. ........
Me.Validate()
Me.MyBindingSource.EndEdit
Me.MyTableAdapter.Update(.
SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
ok...I am trying to create an event handler(TextChanged) but i need a little help
...(confused)
...(confused)
Private Sub AnyControl_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)
Dim Change As Boolean = False
If TryCast(sender, TextBox).Text.Length > 0 Then
Change = True
ElseIf TryCast(sender, MaskedTextBox).Text.Length > 0 Then
Change = True
Else
Return
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Changes As Boolean = False
Dim xcontrol As Control
For Each xcontrol In Me.GroupBox1.Controls
If xcontrol.GetType Is GetType(TextBox) Then
AddHandler xcontrol.TextChanged, AddressOf AnyControl_TextChanged
Changes = True
ElseIf xcontrol.GetType Is GetType(MaskedTextBox) Then
AddHandler xcontrol.TextChanged, AddressOf AnyControl_TextChanged
Changes = True
End If
Next
If Changes Then..........
ASKER
How the handler fires in this case?????
when addnew make the
Me.Save to true and any changes make if false. this will solve the problem with the existing If Not Me.Save Then only.
you can add one more flag as isChangesMade and use "if isChangesMade and Not Me.Save Then" , set the flag to true when changes are made