MadsTheMan
MadsTheMan

Reputation: 703

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.

Update for Thorsten's answer

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 1in tbtest2.text:

enter image description here

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.

Upvotes: 0

Views: 1592

Answers (2)

Thorsten Dittmar
Thorsten Dittmar

Reputation: 56697

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.

Upvotes: 1

Veeke
Veeke

Reputation: 239

Your code only compares the numbers with the same index (so value 1 compared to value 1, value 5 compared to value 5). Personally I would do the following, wich also makes it a bit more readable (imo)

'1-get-store all Integers
dim listof_tbnumber as new list (of Integer)
dim listof_tbmax as new list (of Integer)
For Count = 1 To 9
    listof_tbnumber.add(cint(Controls.Find("tbnumber" & Count, True).FirstOrDefault().Text))
    listof_tbmax.add(cint(Controls.Find("tbmax" & Count, True).FirstOrDefault().Text))
next

'2-compare
'If listof_tbnumber has at least 1 value greater then listof_tbmax
If (listof_tbnumber.FindAll(Function(x As Integer) x > listof_tbmax.Max)).Count > 0 Then
    MsgBox("isgreater")
End If

Upvotes: 1

Related Questions