Reputation: 11
txtRound.Text
txtDaysWorked.Text
txtPerGallon.Text
txtGasCost.Text
txtMaint.Text
txtParking.Text
txtInsure.Text
I have all these textboxes in a vb form and instead of checking every single one to make sure they are all positive numbers I want a more efficient way of checking them with one variable. Once that variable is declared I'll just go like this
If (dblVariable > 0) Then
A lot easier than a long list of checking
Upvotes: 1
Views: 42
Reputation: 25475
Your form has a Controls
properties which is a list of all controls on that form.
You can loop through each control and check it is a TextBox
, and if so set it's Text property.
If LINQ is available you could simplify this to
For Each control In myForm.Controls.OfType<TextBox>()
control.Text = "My Value"
Next
Re-read the question, the title is a bit misleading, but I get what you're asking now.
Again, assuming LINQ is available you should use the .All
extension.
If Me.Controls.OfType(Of TextBox)().All(Function(x) Convert.ToDouble(x.Text) > 0) Then
' Do something
End If
Upvotes: 5
Reputation: 1117
You could have a function which takes the list of TextBoxes:
Private Function CheckValues(txt As TextBox()) As Boolean
For Each member In txt
If not String.IsNullOrWhiteSpace(member.Text) Then
Dim number As Integer = CInt(member.Text)
If number < 0 Then
Return False
Next
Return true 'all positives
End Function
And then call it like this:
If CheckValues(new TextBox(){txtRound, txtDaysWorked}) Then
'case true
Else
'case false
End If
Upvotes: 0
Reputation: 162
This is going to be a vague answer, because I'm working from memory, but here's my best attempt.
In short, I don't think so. If you want to check one variable you're going to have to generate that variable yourself. You could do this client-side.
Alternatively, if all of these controls are within a single form or something, you might be able to cycle through each control in a loop to check each one.
Upvotes: 0