Check all controls in form to see if they have values
I am wanting to be able to check all the controls on a form to see if one or multiple have value. If none have a value, then the end user can close the form without prompt to save. If one or more control types have a value, then prompt to save before closing the form.
I have Comboboxes, MaskedTextBoxes, TextBoxes and ListViews on the same form that I want to check.
I am writing a boolean function right now and want to get your ideas on if I am going about this the right way, or if you think I need to create a boolean for each control type as there are four in total. This is what I have right now.
Code:
Public Function CheckIfFormEmpty() As Boolean
For Each ctrl As Control In Me.Controls
'Comboox/ListView/MaskedTextBox/TextBox.
With ctrl
If TypeOf ctrl Is ComboBox Then
Select Case .Text.Length
Case Is > 0
CheckIfFormEmpty = False
Case Else
CheckIfFormEmpty = True
End Select
ElseIf TypeOf ctrl Is ListView Then
Dim RowCount As Integer = CType(ctrl, ListView).Items.Count
Select Case RowCount
Case Is > 0
CheckIfFormEmpty = False
Case Else
CheckIfFormEmpty = True
End Select
ElseIf TypeOf ctrl Is TextBox Then
Select Case .Text.Length
Case Is > 0
CheckIfFormEmpty = False
Case Else
CheckIfFormEmpty = True
End Select
ElseIf TypeOf ctrl Is MaskedTextBox Then
CType(ctrl, MaskedTextBox).TextMaskFormat = MaskFormat.ExcludePromptAndLiterals
Select Case .Text.Length
Case Is > 0
CheckIfFormEmpty = False
Case Else
CheckIfFormEmpty = True
End Select
End If
End With
Next ctrl
Return CheckIfFormEmpty
End Function
Re: Check all controls in form to see if they have values
For the Comboboxes, MaskedTextBoxes, and TextBoxes you're able to check if the Text property is empty and you can use LINQ to do that fairly easily:
Code:
Private Function ContainsValues() As Boolean
Dim txtboxesWithValues As Integer = Me.Controls.OfType(Of TextBox).Where(Function(t) Not String.IsNullOrWhiteSpace(t.Text)).Count
Dim maskedtxtboxesWithValues As Integer = Me.Controls.OfType(Of MaskedTextBox).Where(Function(m) Not String.IsNullOrWhiteSpace(m.Text)).Count
Dim comboboxWithValues As Integer = Me.Controls.OfType(Of ComboBox).Where(Function(c) Not String.IsNullOrWhiteSpace(c.Text)).Count
Return (txtboxesWithValues = 0 AndAlso maskedtxtboxesWithValues = 0 AndAlso comboboxWithValues = 0)
End Function
The LINQ basically gets all controls of a certain type(textbox, MaskedTextBox, combobox) where their text value is not empty. The function then returns a Boolean value based on if the count is equal to 0 or not.
I'm not sure how to do it for a ListView as I never use them, but that example should give a good place to start.
Re: Check all controls in form to see if they have values
The only problem with OP's code is that it continues to loop through each control and re-setting the value for CheckIfFormEmpty. Thus in reality, this will only set CheckIfFormEmpty = false if the final control it checks is not empty. Since you only need to find the first control that is not empty, you should "Exit For" once you have found a control that is not empty immediately after setting "CheckIfFormEmpty = false". DDay9's code would also work since the boolean is getting set at the very end (in the Return statement); you'd just need to add code checking for the ListView. However this too goes through every control even if the very first control is not empty, so it may lead to a longer processing time than is necessary (although if the number of controls is at a sane level, this shouldn't be noticeable so it's still a viable solution).