HI

I have an application that contains certain editable fields (textboxs) and what i want to do is know when someone has changed or entered new text.

What i done was in the 'TextChanged' part of a text box was this:

Private Sub txtCNotes_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtCNotes.TextChanged

ChangesMade = True

End Sub


now 'ChangesMade' is a global boolean variable which in its first state is set to false and then true if a change is made.

no on the exit of the program or if they navigate away from the record i have this bit of code:

If ChangesMade = True Then
MsgBox("Do you wish to save your changes?")
End If


but the problem is it is just giving that message, regardless of any changes or not.

I wondered if anyone had any ideas?

Regards

Simon

Try this:

Public Class Form1
    Dim ChangesMade As Boolean = False

    Private Sub TextBox1_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.LostFocus
        If ChangesMade Then
            If MessageBox.Show("Do you want to save?", "Save Changes", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
                SaveTextbox()
            End If
        End If
    End Sub

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        ChangesMade = True
    End Sub

    Private Sub SaveTextbox()
        'do whatever to save the textbox
        ChangesMade = False
    End Sub

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        If ChangesMade Then
            If MessageBox.Show("Do you want to save?", "Save Changes", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
                SaveTextbox()
            End If
        End If
    End Sub

End Class

it still seems to do the same thing, even without making any changes or even touching any of the text boxs, when i change or close it asks if i want to save?

Sorry about that. I reset the changesmade in the wrong place. Shows you should run code before you submit it. Try this:

Public Class Form1
    Dim ChangesMade As Boolean = False

    Private Sub TextBox1_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles TextBox1.LostFocus
        If ChangesMade Then
            If MessageBox.Show("Do you want to save?", "Save Changes", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
                SaveTextbox()
            End If
            ChangesMade = False
        End If
    End Sub

    Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
        ChangesMade = True
    End Sub

    Private Sub SaveTextbox()
        'do whatever to save the textbox
        MessageBox.Show(ChangesMade.ToString)
    End Sub

    Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        If ChangesMade Then
            If MessageBox.Show("Do you want to save?", "Save Changes", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
                SaveTextbox()
            End If
        End If
    End Sub

End Class

sorry wayne, but its still happening. everytime i move to another record without making any changes at all it still asks to save.

Could you zip your project up so I can take a look at it. I think you are setting your variable somewhere and not realizing it. I ran the code I submitted and it worked fine.

hi wayne, this is only a small part of the project and dont know how to seperate it out. i have included the code of the whole form as all bits are included in it. apologies and thanks for your help

Public Record As Integer
    Public con As System.Data.OleDb.OleDbConnection
    Public CustomerID As Integer
    Public dt As DataTable
    Public ChangesMade As Boolean = False

    Private Sub CustomerInfo_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        lstDetails.Columns.Add("Arrive", 100, HorizontalAlignment.Left)
        lstDetails.Columns.Add("Departure", 100, HorizontalAlignment.Left)
        lstDetails.Columns.Add("Notes", 300, HorizontalAlignment.Left)
        lstDetails.Columns.Add("Room", 60, HorizontalAlignment.Left)

        Record = 0
        Call LoadCustomerData(Record)

    End Sub

    Public Sub LoadCustomerData(ByRef record As Integer)

        Dim Ratesds As New DataSet
        Dim Ratesda As OleDb.OleDbDataAdapter
        Dim RowCount As Integer

        Dim sql As String

        con = New OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = C:\cdr\ReferenceData.mdb")
        con.Open()

        sql = "SELECT tblCustData.CustID, tblCustData.CustName, tblCustData.CustAddress, tblCustData.CustPostcode, tblCustData.CustTel, tblCustData.CustEmail, tblCustData.CustNotes FROM tblCustData"

        Ratesda = New OleDb.OleDbDataAdapter(sql, con)
        Ratesda.Fill(Ratesds)

        RowCount = Ratesds.Tables(0).Rows.Count

        If RowCount <= record Or record = -1 Then

            MsgBox("No more Records", , "No More Records")
            Return

        Else
            txtCName.Text = Ratesds.Tables(0).Rows(record).Item(1)
            txtCAddress.Text = Ratesds.Tables(0).Rows(record).Item(2)
            txtCPostcode.Text = Ratesds.Tables(0).Rows(record).Item(3)
            txtCTel.Text = Ratesds.Tables(0).Rows(record).Item(4)
            txtCEmail.Text = Ratesds.Tables(0).Rows(record).Item(5)
            txtCNotes.Text = Ratesds.Tables(0).Rows(record).Item(6)
            AppVariables.CustID = Ratesds.Tables(0).Rows(record).Item(0)

            Call StayDetail()
        End If


        con.Close()

    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        If ChangesMade = True Then
            If MessageBox.Show("Do you want to save?", "Save Changes", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
                Return
            End If
        End If


        'move to the next record
        Record = Record + 1
        Call LoadCustomerData(Record)

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        If ChangesMade = True Then
            If MessageBox.Show("Do you want to save?", "Save Changes", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
                Return
            End If
        End If
        'move to the previous record
        Record = Record - 1
        Call LoadCustomerData(Record)

    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        AddCust.Show()
    End Sub

    Public Sub StayDetail()

        lstDetails.Items.Clear()

        Dim Stayds As New DataSet
        Dim Stayda As OleDb.OleDbDataAdapter
        Dim sql As String
        Dim RecordCount As Integer

        con = New OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source = C:\cdr\ReferenceData.mdb")
        con.Open()

        sql = "SELECT tblStay.ArriveDate, tblStay.LeaveDate, tblStay.StayNotes, tblStay.Room FROM tblStay WHERE tblStay.CustID =" & AppVariables.CustID

        Stayda = New OleDb.OleDbDataAdapter(sql, con)
        Stayda.Fill(Stayds)

        RecordCount = Stayds.Tables(0).Rows.Count
        If RecordCount > 0 Then
            For i = 0 To RecordCount - 1
                lstDetails.Items.Add(Stayds.Tables(0).Rows(i).Item(0))
                lstDetails.Items(i).SubItems.Add(Stayds.Tables(0).Rows(i).Item(1))
                lstDetails.Items(i).SubItems.Add(Stayds.Tables(0).Rows(i).Item(2))
                lstDetails.Items(i).SubItems.Add(Stayds.Tables(0).Rows(i).Item(3))
            Next
        End If

        con.Close()
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click

        StayDet.Show()

    End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click

        AppVariables.SearchTerm = txtSearch.Text
        SearchResults.Show()

    End Sub

    Private Sub txtCNotes_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtCNotes.LostFocus
        If ChangesMade Then
            If MessageBox.Show("Do you want to save?", "Save Changes", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
                Return
            End If
            ChangesMade = False
        End If


    End Sub

    Private Sub txtCNotes_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtCNotes.TextChanged
        ChangesMade = True
    End Sub

hi wayne, one thing i should add that has just come to mind is that all the text boxs are bound to a database so this could be causing the chnage when the form loads, if this is the case - is there any way to check for changes after the form has loaded?

regards

solved - well, sort of

just changed the state on the text box from changed to got focus, even though it may have not changed it still knows something has been done!

In your Button1_click, Button2_click and txtCNotes_LostFocus do this:

If ChangesMade = True Then
            ChangesMade = False
            If MessageBox.Show("Do you want to save?", "Save Changes", MessageBoxButtons.YesNo) = Windows.Forms.DialogResult.Yes Then
                Return
            End If
        End If

Your return was not getting to the ChangesMade=False

This question has already been answered. Start a new discussion instead.