i would like to make an IF statement that will check whether all textboxes in the form have been changed. i dont want to check one by one. can i check all textboxes with one simple IF THEN clause in vb.net?
There is no simple solution. What you could do is write a method which returns a boolean based on the values of the textboxes it processes when it loops over the container controls, checks if the control is a textbox and if it is check if it has a value.
Well, you would store the .text value of each textbox control the form when the form is initially loaded (perhaps store in an array), and then perform an if evaluation (as you mention) comparing the current .text value of each text box against the array. Would that work for your scenario?
If the form represents your app's 'settings'
- Create a Settings Class
- Create a GetSettingsFromInterface function that returns a Settings object
- Create a Settings.Equals function to compare two Settings objects
- If CurrentSettings.Equals(formSettings), there's no change.
Or you could put a JavaScript "onchange" event on each checkbox and use that to set a hidden field, "aCheckboxChanged()" or something, that would set the hidden variable from zero to one.
Here is what you can do in VB.NET.
First, you need a function that returns all the TextBox Controls within a Form (or ContainerControl). For reasons that will become clear, I would have the function actually return a Dictionary, with the name of each TextBox serving as the key, like so:
Private Function getAllTextBoxes(ByVal container As ContainerControl) As Dictionary(Of String, TextBox)
Dim allTextBoxes As New Dictionary(Of String, TextBox)
For Each ctrl As Control In container.Controls
If TypeOf ctrl Is TextBox Then allTextBoxes.Add(ctrl.Name, ctrl)
Next
Return allTextBoxes
End Function
Next, you need a function to return a Dictionary providing the value within every TextBox, so you can determine which values have changed:
Private Function getTextBoxValues(ByVal textBoxDefs As IDictionary(Of String, TextBox)) As Dictionary(Of String, String)
Dim textBoxValues As New Dictionary(Of String, String)
For Each tbxDef As KeyValuePair(Of String, TextBox) In textBoxDefs
Dim tbx As TextBox = tbxDef.Value
Dim name As String = tbx.Name
Dim value As String = tbx.Text
textBoxValues.Add(name, value)
Next
Return textBoxValues
End Function
Lastly, if I understand your question correctly, you want a function to go through every TextBox, compare its value to the previous recorded value, and return True if all values have changed. This will do the trick:
Private Function getAllTextBoxValuesChanged() As Boolean
Dim allTextBoxes As Dictionary(Of String, TextBox) = getAllTextBoxes(Me)
Static allTextBoxPreviousValues As Dictionary(Of String, String) = getTextBoxValues(allTextBoxes)
Dim allTextBoxCurrentValues As Dictionary(Of String, String) = getTextBoxValues(allTextBoxes)
Dim numTextBoxes As Integer = allTextBoxes.Count
Dim numChangedValues As Integer = 0
Dim modifications As New Dictionary(Of String, String)
For Each tbxDef As KeyValuePair(Of String, String) In allTextBoxCurrentValues
Dim name As String = tbxDef.Key
Dim currentValue As String = tbxDef.Value
Dim previousValue As String = allTextBoxPreviousValues(name)
If currentValue <> previousValue Then
numChangedValues += 1
modifications.Add(name, currentValue)
End If
Next
For Each modificationDef As KeyValuePair(Of String, String) In modifications
allTextBoxPreviousValues(modificationDef.Key) = modificationDef.Value
Next
Return (numChangedValues >= numTextBoxes)
End Function
Bear in mind that since the above function uses a static variable, it will only return true if all values have changed since the last time you called it. Also, the very first time you call the function, it will return false. (But this behavior can easily be changed if desired.)
With these functions in place, any time you want to check if all values have changed (since the last time you checked) you can just write:
Dim allTextBoxValuesChanged As Boolean = getAllTextBoxValuesChanged()
If allTextBoxValuesChanged Then
DoSomething()
End If