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:
What is needed:
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
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
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.
Comments (3)
Author
Commented:You know the audience better than I do.
Please make what ever editorial adjustments that you see fit.
Sonny
Author
Commented: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
Author
Commented: