MadsTheMan - 1 year ago 189
Vb.net Question

# VB.net Comparing multiple textboxes by checking if a number is greater than

I have 18 textboxes that simply contains a number. 9 textboxes called

`tbmax`
(tbmax1-9) and 9 called
`tbnumber`
(tbnumber1-9). Like this:
`tbnumber1`
,
`tbnumber2`
,
`tbnumber3`
... so on. And
`tbmax1`
,
`tbmax2`
,
`tbmax3`
and so on.

This is what I want...

If ANY of the numbers in
`tbnumber(1-9).text`
is greater than ANY of the numbers in
`tbmax(1-9).text`
, do something. In other words, if just one of the
`tbnumber`
textboxes has a larger number than just one of the
`tbmax`
texboxes, do something.

Attempt 1

``````For Count = 1 To 9
Dim Tnumber = CType(Controls.Find("tbnumber" & Count, True).FirstOrDefault(), TextBox)
Dim Tmax = CType(Controls.Find("tbmax" & Count, True).FirstOrDefault(), TextBox)
If Tnumber.Text > Tmax.text Then
'do something
next
end if
``````

Attempt 2

``````Dim tbnumber_textboxes As New List(Of TextBox) From {tbNumber1,
tbNumber2, tbNumber3, tbNumber4, tbNumber5, tbNumber6, tbNumber7,
tbNumber8, tbNumber9}

Dim tbmax_textboxes As New List(Of TextBox) From {tbmax1, tbmax2,
tbmax3, tbmax4, tbmax5, tbmax6, tbmax7, tbmax8, tbmax9}

If tbNumber_textboxes.Any(Function(cb) cb.Text) > tbMax_textboxes.Any(Function(cb) cb.Text) Then
'Do something
'For example msgbox("A number is larger than any of the max-textboxes")
``````

However, It doesn't seem to work and I'm not quite sure why. I think in my attempt 2 that "Any" is not the right usage for my purpose.

I've tested your method, and I removed the MaxVal to simplify my testing by putting a number in instead.

This is what I've got

``````For Count = 1 To 9
Dim Tnumber = CType(Controls.Find("tbnumber" & Count, True).FirstOrDefault(), TextBox)
Dim number = Convert.ToInt32(Tnumber.Text)
If (number > 0) And (number < 30) Then
tbTest1.Text = "0-30"
tbtest2.Text = number

elseif (number > 30) And (number < 50) Then
tbTest1.Text = "30-50"
tbtest2.Text = number
end if
next
``````

This is how the tbnumber textboxes (1-9) are at the moment as I get
`1`
in
`tbtest2.text`
:

So what I notice is that
`tbtest2.text`
outputs
`1`
for some reason...
`Number`
should be the greatest number of any of the tbnumber1-9 textboxes.

Attempt 1 looks promising to me (I'd have tried the same thing). However, you are not comparing the right values in the end.

``````If Tnumber.Text > Tmax Then
'do something
``````

Your comparison above compares the text of the `Tnumber` textbox to the instance `Tmax` - you're comparing `String` with `TextBox`.

You need to get the text from both text boxes, convert them to `Int32` and then compare those two values. I'm not a VB.NET developer, but I hope the blow would work:

``````Dim number = Convert.ToInt32(Tnumber.Text)
Dim maxVal = Convert.ToInt32(Tmax.Text)

If number > maxVal Then
'do something
end if
``````

In addition you need to keep the following in mind: If your text boxes are part of panels, group boxes or other containers, they will be part of the respective container's `Controls` collection, not of your form's `Controls` collection, so please use the debugger to find out whether you're actually finding the controls.

Edit after some comments to the question: Please note that looking up controls by name is case sensitive! So if the control is named `tbNumber9` and you're searching for `tbnumber9` you will not get a result.

Edit: To display the maximum number you'd do this:

``````Dim maxNumber = Int32.MinValue

For Count = 1 To 9
Dim Tnumber = CType(Controls.Find("tbnumber" & Count, True).FirstOrDefault(), TextBox)
Dim number = Convert.ToInt32(Tnumber.Text)

If (number > maxNumber) Then
maxNumber = number
tbtest2.Text = maxNumber

If (maxNumber > 0) And (maxNumber < 30) Then
tbTest1.Text = "0-30"
elseif (maxNumber > 30) And (maxNumber < 50) Then
tbTest1.Text = "30-50"
end if
End If
next
``````

This stores the current maximum encoutered in `maxNumber` and if that value changed, updates the other text boxes.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download