locked
If a controls changes in a form, show a message to force saving the data RRS feed

  • Question

  • Hi,

    I have a form named "frmCategories" on which I have textboxes that a user can change. If that happens and the user don't click on the button save (BtnSave) then I want a message (ShowFrmMsgTBChangeSoSave) to be displayed, I wrote some code but even if a change is made, no form appears.

    Below is the code followed by the form image:

    Thanks in advance for the help

    Claude From Quebec, Canada

        Private Sub BoxHasChanged()
            'Iterate through all controls and handle them according to their type
            For Each c As Control In Me.Controls
                If TypeOf (c) Is CheckBox Then
                    AddHandler CType(c, CheckBox).CheckedChanged, AddressOf SomethingChanged
                ElseIf TypeOf (c) Is RadioButton Then
                    AddHandler CType(c, RadioButton).CheckedChanged, AddressOf SomethingChanged
                ElseIf TypeOf (c) Is TextBox Then
                    AddHandler CType(c, TextBox).TextChanged, AddressOf SomethingChanged
                End If
            Next
        End Sub
    
        Private Sub SomethingChanged()
            ShowFrmMsgTBChangeSoSave()
        End Sub
    
        Private Sub FrmCategories_FormClosed(sender As Object, e As FormClosedEventArgs) Handles MyBase.FormClosed
            BoxHasChanged()
        End Sub
        Private Sub ShowFrmMsgTBChangeSoSave()
            Try
                Dim SmallerMsg As New FrmMsgSmaller
                FrmMsgSmaller.Close()
                FrmMsgSmaller.TextBox1.Text = ""
                FrmMsgSmaller.TextBox1.Text = "There was changes made, please click the save button!"
                FrmMsgSmaller.Show()
                Dim SW2 As New Stopwatch
                SW2.Start()
                Do
                    Application.DoEvents()
                Loop Until SW2.ElapsedMilliseconds >= 2000
                FrmMsgSmaller.Hide()
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
        End Sub


    Friday, July 17, 2020 10:30 AM

Answers

  • Hi Claude du Québec,

    Thank you for posting here.

    You need to bind the ‘changed’ event of the controls to your form at the beginning.

    I make a test on my side and you can refer to the following code:

        Dim IsChanged As Boolean
        Dim savedText As String
        Private Sub frmCategories_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            BoxHasChanged()
        End Sub
        Private Sub SaveBtn_Click(sender As Object, e As EventArgs) Handles SaveBtn.Click
            savedText = TextBox1.Text
        End Sub
    
        Private Sub SomethingChanged()
            If TextBox1.Text = savedText Then
                IsChanged = False
            Else
                IsChanged = True
            End If
        End Sub
        Private Sub frmCategories_FormClosed(sender As Object, e As FormClosedEventArgs) Handles MyBase.FormClosed
            If IsChanged = True Then
                ShowFrmMsgTBChangeSoSave()
            End If
        End Sub

    Result of my test:

    Hope it could be helpful.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.




    Monday, July 20, 2020 9:06 AM
  • Hello,

    The right way to detect unsaved changes is to use DataTable.GetChanges in tangent with DataRowState.

    For instance if you check changes using DataRowState Modified and after calling GetChanges the result is a populated DataTable there are changes. This should be done in form Closing event using e parameter which allows the form closing to be cancelled thus allowing the user to go back and save changes.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Monday, July 20, 2020 9:53 AM

All replies

  • Hi Claude du Québec,

    Thank you for posting here.

    You need to bind the ‘changed’ event of the controls to your form at the beginning.

    I make a test on my side and you can refer to the following code:

        Dim IsChanged As Boolean
        Dim savedText As String
        Private Sub frmCategories_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            BoxHasChanged()
        End Sub
        Private Sub SaveBtn_Click(sender As Object, e As EventArgs) Handles SaveBtn.Click
            savedText = TextBox1.Text
        End Sub
    
        Private Sub SomethingChanged()
            If TextBox1.Text = savedText Then
                IsChanged = False
            Else
                IsChanged = True
            End If
        End Sub
        Private Sub frmCategories_FormClosed(sender As Object, e As FormClosedEventArgs) Handles MyBase.FormClosed
            If IsChanged = True Then
                ShowFrmMsgTBChangeSoSave()
            End If
        End Sub

    Result of my test:

    Hope it could be helpful.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.




    Monday, July 20, 2020 9:06 AM
  • Hello,

    The right way to detect unsaved changes is to use DataTable.GetChanges in tangent with DataRowState.

    For instance if you check changes using DataRowState Modified and after calling GetChanges the result is a populated DataTable there are changes. This should be done in form Closing event using e parameter which allows the form closing to be cancelled thus allowing the user to go back and save changes.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Monday, July 20, 2020 9:53 AM
  • Thanks Karen, I knew it must have something to check if data changes... thanks so lot.

    Claude

    Monday, July 20, 2020 1:18 PM
  • Thanks for your response, your code works, I am testing Karen response.

    I appreciate your time

    Claude

    Monday, July 20, 2020 1:20 PM