如何对文本框值进行排名?

How to rank text-boxes values?

我的表单中有 5 个文本框。我想按一个按钮在其他文本框中对它们进行排名。

以上5个文本框取值每次都不一样

我已经尝试了下面提到的代码,但没有用。我想显示高值 TextBox 排名“1”,低值“2”等等。

新文本框的名称:textbox25textbox24textbox23textbox22textbox21

以下代码中已经提到的具有排名价值的文本框名称。


Dim i As String() = New String() {Val(TextBox20.Text), Val(TextBox19.Text),
                                      Val(TextBox18.Text), Val(TextBox17.Text),
                                      Val(TextBox16.Text)}
Dim lastScore As Integer
Dim lastScorePosition As Integer
Dim position As Integer = 1

For Each i1 In i
    If Val(lastScore) <> Val(i1) Then
        TextBox25.Text = (position)
        lastScorePosition = position
        lastScore = Val(i1)
    Else
        TextBox25.Text = (lastScorePosition & ",")
    End If
    position += 1
Next

新文本框中没有结果。

在线解释和评论。

Private Sub OPCode()
    'Don't change to a number with Val when you want an array of String
    Dim StringArray As String() = {TextBox20.Text, TextBox19.Text,
                              TextBox18.Text, TextBox17.Text,
                              TextBox16.Text}
    'Change the Strings to Integers with Linq
    Dim IntegerArray As Integer() = (From i In StringArray
                                     Select CInt(i)).ToArray
    '.Sort puts the array in numerical order 
    Array.Sort(IntegerArray)
    'The highest number will be the last number in the array
    TextBox25.Text = IntegerArray(IntegerArray.Length - 1).ToString
End Sub

根据新图像。试试这个:

Dim dic As New Dictionary(Of TextBox, TextBox) From
    {
    {txtMerit1, txtMerit1Rank},
    {txtMerit2, txtMerit2Rank},
    {txtMerit3, txtMerit3Rank},
    {txtMerit4, txtMerit4Rank},
    {txtMerit5, txtMerit5Rank}
    }

Dim i As Integer = 1

For Each kvp In dic.OrderByDescending(Function(a) Convert.ToSingle(a.Key.Text))
    kvp.Value.Text = i
    i += 1
Next

这将创建 Dictionary that holds each Merit and its rank text boxes as KeyValue 对。然后,会根据成绩文本框的值降序排列条目,最后,会在连接的排名文本框中显示排名。

这是一个快速演示:

希望这次能成功。

祝你好运。

Dim boxes As TextBox() = New TextBox() {TextBox20, TextBox19, TextBox18, TextBox17, TextBox16.Text}
Dim ordered = boxes.OrderBy(Function(box) Val(box.Text))
TextBox25.Text = String.Join(",", ordered.Select(Function(box) box.Text))