Comparing and Ranking Textboxes

  • Thread starter Thread starter lmefford
  • Start date Start date
L

lmefford

I need some help comparing a list of textboxes and taking the results
and ranking them.

What I'm wanting to do is avoid a huge nested if/then-else statement
and I dunno if there is an easy way to compare about 5 or 6 textboxes
and then assign them to variables or an array to be called on later so
that it doesn't matter what textbox I put the numbers in, they all
come out in the order I want (least to greatest in this case)

Does anybody have any hints for this to make the code shorter and
avoid those nasty nested statements that can wind up causing trouble?
 
you could make a class that holds the value of the textbox and a reference to
the textbox, then to rank them just run them through a Quicksort. hope this
helps
 
I'm not 100% sure I understand what it is you're looking for, but the code
below shows adding the values to an array and then using that array to
repopulate the textboxes with the values sorted. Note that .LostFocus event
is handled instead of TextChanged --- other wise some unexpected results
occur.

Private Sub TextBoxValueChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) _

Handles TextBox1.LostFocus, TextBox2.LostFocus, TextBox3.LostFocus,
TextBox4.LostFocus, TextBox5.LostFocus

' load value into an array

Dim arr(4) As String

arr(0) = TextBox1.Text

arr(1) = TextBox2.Text

arr(2) = TextBox3.Text

arr(3) = TextBox4.Text

arr(4) = TextBox5.Text

' sort the array

Array.Sort(arr)

' output sorted values to text boxes

TextBox1.Text = arr(0).ToString

TextBox2.Text = arr(1).ToString

TextBox3.Text = arr(2).ToString

TextBox4.Text = arr(3).ToString

TextBox5.Text = arr(4).ToString

End Sub
 
Matt,

That was my idea too, although I think that you can even set the textbox
direct in the array and use that with some casting.

However, as you I have the idea that I don't really understand the problem
and therefore surely did not try it.

Cor
 
Back
Top