We help IT Professionals succeed at work.
Get Started

FormClosing Save MessageBox works on Datagridview but not on same form's TextBox.

dave_sky
dave_sky asked
on
758 Views
Last Modified: 2011-09-20
Hi Experts,

VB.Net 2005
SQL Server 2005

Probably a very simple question but I can't get it figured out.

I have the code below that works on the FormClosing event when a change is made in a datagridview.
When I add a bound TextBox to the same form and edit without saving, the message is  not shown.

My Code:

 Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

      Dim response As MsgBoxResult
      If DataSet1.HasChanges = True Then
         response = MsgBox("You have unsaved changes. Save them now?", MsgBoxStyle.YesNoCancel + MsgBoxStyle.Question, "Exiting - Save Changes?")
         If response = MsgBoxResult.Yes Then
            'save (and exit)
            Me.Validate()
            Me.Table1BindingSource.EndEdit()
            Me.Table1TableAdapter.Update(Me.DataSet1.Table1)
         ElseIf response = MsgBoxResult.Cancel Then
            'cancel will keep the form open so they can continue
            e.Cancel = True
         End If
      End If

  End Sub

Thanks for your help,

Dave
Comment
Watch Question
Commented:
This problem has been solved!
Unlock 1 Answer and 15 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE