"Dirtied Form" in VB.NET

Sonny GDeveloper
Published:
Updated:
MisAccess has a form property to determine if any controls on a form are changed. This property is called “Dirty”. Use of the Dirty property in MsAccess determines whether the current record has been modified since it was last saved. Read/write Boolean.

I was unable to find a property in VB.NET
(THE SOFTWARE CODE BELOW IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. THE SOFTWARE CODE BELOW CAN BE FREELY USED, MODIFIED, AND SHARED.)

 

My data entry screen contains data controls, a listbox (ListView) that contains all records in the data table, and action buttons (OK/Save, Cancel/Undo, Delete and Exit/Close form). The user can directly add new records or select existing records to modify or delete. See below:

 

Graphical user interface, text

Description automatically generated

 

  

What is needed:

  • For all TextBoxes:
    1. A custom TextBox control that Inherits System.Windows.Forms.TextBox
    2. I named the custom control: MyTextBox
    3. A ReadOnly Boolean property is added and named “TextAltered
  • For all MaskedTextBoxes:
    1. A custom MaskedTextBox control is needed that Inherits System.Windows.Forms.MaskedTextBox
    2. I named the custom control: MyMaskedTextBox 
    3. A ReadOnly Boolean property is added and named “TextAltered
  • If a Listbox(ListView) picklist is used to select a record to populate a form with existing data , then code at the end of the code in the control event ListViewPickList_SelectedIndexChanged.
  • A private property in the data entry form named FormIsDirty which is placed in the code button for closing the form or in the event when the form is closed.
  • After MyTextBox and MyMaskedTextBox classes create the new controls, use these controls in your data entry form. They will be found in your Toolbox (search for “My”)
  • Below, you will see the newly-created property of MyTextBox:

 

Table

Description automatically generated with medium confidence

 


 

 

 

Public Class MyTextBox

    Inherits System.Windows.Forms.TextBox

 

    Private Property OriginalValue As String = ""

 

    Public ReadOnly Property OriginalText As String

        Get

            Return Me.OriginalValue

        End Get

    End Property

 

    Public ReadOnly Property TextAltered As Boolean

        Get

            If Me.OriginalValue Is Nothing And MyBase.Text Is Nothing Then

                Return False

            ElseIf Me.OriginalValue Is Nothing Or MyBase.Text Is Nothing Then

                Return True

            Else

                Return Not Me.OriginalValue.Equals(MyBase.Text)

            End If

        End Get

    End Property

 

    Public Sub Reset()

        Me.OriginalValue = MyBase.Text

    End Sub

End Class

 

 

 

Public Class MyMaskedTextBox

    Inherits System.Windows.Forms.MaskedTextBox

 

    Private Property OriginalValue As String = "(   )    -"

 

    Public ReadOnly Property OriginalText As String

        Get

            Return Me.OriginalValue

        End Get

    End Property

 

    Public ReadOnly Property TextAltered As Boolean

        Get

            If Me.OriginalValue Is Nothing And MyBase.Text Is Nothing Then

                Return False

            ElseIf Me.OriginalValue Is Nothing Or MyBase.Text Is Nothing Then

                Return True

            Else

                Return Not Me.OriginalValue.Equals(MyBase.Text)

            End If

        End Get

    End Property

 

    Public Sub Reset()

        Me.OriginalValue = MyBase.Text

    End Sub

End Class

 

SPECIAL NOTE: 

In the case of the MaskedTextBox, the setting of Private Property OriginalValue As String conforms to the mask that you intend to use. In this case, it is for telephone number.

  

If a Listbox(ListView) picklist is used to select a record to populate a form with existing data , then code at the end of the code in the control event ListViewPickList_SelectedIndexChanged. (The Reset() is part of the MyTextBox and MyMaskedTextBox classes.)

 

Code to be added 

            Me.sFirstName.Reset()

            Me.sLastName.Reset()

            Me.sSpouseName.Reset()

            Me.sAddress.Reset()

            Me.sCity.Reset()

            Me.sState.Reset()

            Me.sPostalCode.Reset()

            Me.sEmailAddress.Reset()

            Me.sLandLinePhone.Reset()

            Me.sMobilePhone.Reset()

 

 

A private property in the data entry form named FormIsDirty which is placed in the code button for closing the form or in the event when the form is closed.

 

   Private ReadOnly Property FormIsDirty As Boolean

        Get

            Return Me.sFirstName.TextAltered Or

              Me.sLastName.TextAltered Or

              Me.sSpouseName.TextAltered Or

              Me.sAddress.TextAltered Or

              Me.sCity.TextAltered Or

              Me.sState.TextAltered Or

              Me.sPostalCode.TextAltered Or

              Me.sEmailAddress.TextAltered Or

              Me.sLandLinePhone.TextAltered Or

              Me.sMobilePhone.TextAltered

        End Get

    End Property

 

 

This is how the FormIsDirty property is used. In this case, a button closes the form but the code could be used in the FormClosed or FormClosing event.

 

Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click

        

If FormIsDirty Then

iUserResponse = MsgBox("Data on this screen has changed but is not being saved!" & vbCrLf & vbCrLf & "Do you want to save these changes?", vbYesNo + vbQuestion)

            

If iUserResponse = vbYes Then 

btnOK_Click(btnOK, New EventArgs())

      Else

         MsgBox("The data was not saved.", vbOKOnly + vbExclamation, sTitle)

      End If

End If

 

Me.Close()

 

End Sub


Remember to reset all of the values [  ResetTextAlteredAttribute()  ] when you save a record and are keeping the data entry screen active for more transactions. Use this code when you save, delete or undo a record on your data entry screen and the data is being cleared:

 Private Sub ResetTextAlteredAttribute()
        Me.sFirstName.Reset()
        Me.sLastName.Reset()
        Me.sSpouseName.Reset()
        Me.sAddress.Reset()
        Me.sCity.Reset()
        Me.sState.Reset()
        Me.sPostalCode.Reset()
        Me.sEmailAddress.Reset()
        Me.sLandLinePhone.Reset()
        Me.sMobilePhone.Reset()
    End Sub

Example:
 Private Sub btnOK_Click(sender As Object, e As EventArgs) Handles btnOK.ClickPrivate Sub btnOK.Click
....
...
...
...
ResetTextAlteredAttribute() 
End Sub
1
457 Views

Comments (3)

Sonny GDeveloper

Author

Commented:
David,

You know the audience better than I do.

Please make what ever editorial adjustments that you see fit.

Sonny

Sonny GDeveloper

Author

Commented:
Hi David,

Thanks for the guidance.

Here's my response:
1. I'd suggest adding a sentence or two of introduction providing the context for what you're explaining in the article.

The blue box at the beginning addresses this, I think.


2. In the same manner, a short conclusion.

I have no clue how to create a conclusion or what to say, but I would trust you to add one if it is necessary. This code took me about 3-4 days of Internet research and coding to perfect. Anyone using the code will save that time.


3. As I'm not a programmer - I'm not sure where the individual code blocks start and stop but if you could put all instances of code into the 'code block' in the editor I think that would help a great deal.

This is a very technical article directed to developers. I tried to use the code block editor just now and almost destroyed the article. Feel free to cut and paste the code into a code block editor if that is the convention at EE. I have no problems if you do that. Just keep the code in its order. What I have in the article is a perfect cut-and-paste format for a developer to use the code as-is.


Thanks for your guidance. I hope my reply is appropriate and cooperative.

Sonny
Sonny GDeveloper

Author

Commented:
Thank you! I hope that it makes a difference.

Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.