MedVision ad

VB. NET Query (1 Viewer)

SW220

New Member
Joined
Oct 8, 2003
Messages
28
Gender
Male
HSC
2003
If i had 5 textboxes how would i in Visual Basic .nET find out the lowest number inputed and display it in a msg box?
thanks for any replies!
 

Winston

Active Member
Joined
Aug 30, 2002
Messages
6,128
Gender
Undisclosed
HSC
2003
Well i whipped up this function:


Function findLowestNum(ByVal textBoxArray As ArrayList) As Integer
Dim textBox As TextBox = CType(textBoxArray.Item(0), TextBox)
Dim currentMin As Integer = textBox.Text
For i As Integer = 0 To textBoxArray.Count - 1
textBox = CType(textBoxArray.Item(i), TextBox)
If currentMin > textBox.Text Then
currentMin = textBox.Text
End If
Next
Return currentMin
End Function

So let's say you're gonna find the lowest number in the 5 textboxes, we can do this:


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim textBoxArray As New ArrayList
textBoxArray.Add(Me.TextBox1)
textBoxArray.Add(Me.TextBox2)
textBoxArray.Add(Me.TextBox3)
textBoxArray.Add(Me.TextBox4)
textBoxArray.Add(Me.TextBox5)

MessageBox.Show(Me.findLowestNum(textBoxArray))

End Sub

Dodgy, but you'd get the idea.
 

Agent Z

Member
Joined
Sep 30, 2003
Messages
181
Location
Sydney
Gender
Female
HSC
2005
Winston, im not doubting your code, but wouldnt a much simpler way to do it be this:

If CInt(Val(txtscore2.Text)) <= CInt(Val(txtscore1.Text)) And CInt(Val(txtscore2.Text)) <= CInt(Val(txtscore3.Text)) And CInt(Val(txtscore2.Text)) <= CInt(Val(txtscore4.Text)) And CInt(Val(txtscore2.Text)) <= CInt(Val(txtscore5.Text)) And CInt(Val(txtscore2.Text)) <= CInt(Val(txtscore6.Text)) And CInt(Val(txtscore2.Text)) <= CInt(Val(txtscore7.Text)) Then lbllowest.Text = CInt(Val(txtscore2.Text))

If CInt(Val(txtscore3.Text)) <= CInt(Val(txtscore1.Text)) And CInt(Val(txtscore3.Text)) <= CInt(Val(txtscore2.Text)) And CInt(Val(txtscore3.Text)) <= CInt(Val(txtscore4.Text)) And CInt(Val(txtscore3.Text)) <= CInt(Val(txtscore5.Text)) And CInt(Val(txtscore3.Text)) <= CInt(Val(txtscore6.Text)) And CInt(Val(txtscore3.Text)) <= CInt(Val(txtscore7.Text)) Then lbllowest.Text = CInt(Val(txtscore3.Text))

If CInt(Val(txtscore4.Text)) <= CInt(Val(txtscore1.Text)) And CInt(Val(txtscore4.Text)) <= CInt(Val(txtscore2.Text)) And CInt(Val(txtscore4.Text)) <= CInt(Val(txtscore3.Text)) And CInt(Val(txtscore4.Text)) <= CInt(Val(txtscore5.Text)) And CInt(Val(txtscore4.Text)) <= CInt(Val(txtscore6.Text)) And CInt(Val(txtscore4.Text)) <= CInt(Val(txtscore7.Text)) Then lbllowest.Text = CInt(Val(txtscore4.Text))

If CInt(Val(txtscore5.Text)) <= CInt(Val(txtscore1.Text)) And CInt(Val(txtscore5.Text)) <= CInt(Val(txtscore2.Text)) And CInt(Val(txtscore5.Text)) <= CInt(Val(txtscore3.Text)) And CInt(Val(txtscore5.Text)) <= CInt(Val(txtscore4.Text)) And CInt(Val(txtscore5.Text)) <= CInt(Val(txtscore6.Text)) And CInt(Val(txtscore5.Text)) <= CInt(Val(txtscore7.Text)) Then lbllowest.Text = CInt(Val(txtscore5.Text))

If CInt(Val(txtscore6.Text)) <= CInt(Val(txtscore1.Text)) And CInt(Val(txtscore6.Text)) <= CInt(Val(txtscore2.Text)) And CInt(Val(txtscore6.Text)) <= CInt(Val(txtscore3.Text)) And CInt(Val(txtscore6.Text)) <= CInt(Val(txtscore4.Text)) And CInt(Val(txtscore6.Text)) <= CInt(Val(txtscore5.Text)) And CInt(Val(txtscore6.Text)) <= CInt(Val(txtscore7.Text)) Then lbllowest.Text = CInt(Val(txtscore6.Text))

If CInt(Val(txtscore7.Text)) <= CInt(Val(txtscore1.Text)) And CInt(Val(txtscore7.Text)) <= CInt(Val(txtscore2.Text)) And CInt(Val(txtscore7.Text)) <= CInt(Val(txtscore3.Text)) And CInt(Val(txtscore7.Text)) <= CInt(Val(txtscore4.Text)) And CInt(Val(txtscore7.Text)) <= CInt(Val(txtscore5.Text)) And CInt(Val(txtscore7.Text)) <= CInt(Val(txtscore6.Text)) Then lbllowest.Text = CInt(Val(txtscore7.Text))


Ok now looking back on my code, no its not the easiest way to do it =/
 

Agent Z

Member
Joined
Sep 30, 2003
Messages
181
Location
Sydney
Gender
Female
HSC
2005
Ok, maybe that code looks complex, but its not really. All im doing is getting the textboxes, getting the value that inside then and using a whole lot of AND statements to see which one satisfies the critera (<=) And to display it in a label (lbllowest)
 

sunny

meh.
Joined
Jul 7, 2002
Messages
5,350
Gender
Male
HSC
2002
Loop structures existing for a reason.
 

Winston

Active Member
Joined
Aug 30, 2002
Messages
6,128
Gender
Undisclosed
HSC
2003
Agent Z said:
Ok, maybe that code looks complex, but its not really. All im doing is getting the textboxes, getting the value that inside then and using a whole lot of AND statements to see which one satisfies the critera (<=) And to display it in a label (lbllowest)
Yes, you can do that, but think about if i wanted to check through another 18 textboxes, did you know one if then check actually takes time... and my code is flexible all u need to do is populate an arraylist with newly textboxes.

I'd also like to say it's not really that good programming practices to do things the way you did it, like look at how poorly it is, in the future if u wanted to maintain things, having functions are good... my implementation was dodgy though, but i just made it up on the spot, but still its ok...
 
Last edited:

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Top