比较值多个文本框(V​​B.Net)

时间:2018-12-25 10:25:43

标签: vb.net

我有以下文本框,TextBox16-最多TextBox22TextBox16,17,18,19,20,21,22,23)如何比较其值并以最低或最高的值显示我的TextBox?代码是否可以与以下代码一起使用:如果最大的TextBox,则为它分配最小的其他颜色。例如-红色代表最小,紫色代表最大。

Valuel示例:文本框-比较(最高/最低)

    TextBox16.Text = ("21")
    TextBox17.Text = ("24")
    TextBox18.Text = ("343")
    TextBox19.Text = ("393")
    TextBox20.Text = ("371")
    TextBox21.Text = ("473")
    TextBox22.Text = ("499")
    TextBox23.Text = ("410")

最小的文本框是TextBox16.text,带21,所以我们将分配红色(TextBox16.BackColor = Red);最大/较大的文本框是TextBox22.text,带499,因此我们将自动分配TextBox22.BackColor = ("Violet")或其他任何方式。没关系:)

    Private Sub TextBoxes_TextChanged(ByVal sender As Object, ByVal e As EventArgs) Handles txtDrawA1.TextChanged, txtDrawA2.TextChanged, etc.
        SetTextBoxColor(DirectCast(sender, TextBox))
    End Sub
    Sub SetTextBoxColor(ByVal txt As TextBox)
        Select Case txt.Text
            Case "1"
                txt.BackColor = Color.DarkSalmon
            Case "2"
                txt.BackColor = Color.Aqua
            Case "3"
                txt.BackColor = Color.DimGray
            Case "4"
                txt.BackColor = Color.DarkBlue
            Case "5"
                txt.BackColor = Color.Violet
            Case "6"
                txt.BackColor = Color.BlueViolet
            Case "7"
                txt.BackColor = Color.Yellow
            Case "8"
                txt.BackColor = Color.blablabla Doesn't matter
        End Select

按从小到大的升序,分别为8个文本框分配颜色。那就是应该的代码。

2 个答案:

答案 0 :(得分:0)

我认为您可以适应此代码以供使用。

 Private lstColor As New List(Of Color)
 Private lstTextBox As New List(Of TextBox)

 Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
     FillColorList()
 End Sub

Private Sub ColorTextBoxes()
    FillTextBoxList(16, 23)
    Dim SortedList As List(Of TextBox) = SortList()
    Dim index As Integer
    For Each txt As TextBox In SortedList
         txt.BackColor = lstColor(index)
         index += 1
    Next
End Sub

Private Sub FillColorList()
    lstColor.Add(Color.Red) 'for lowest number
    lstColor.Add(Color.BlanchedAlmond)
    lstColor.Add(Color.PaleGreen)
    lstColor.Add(Color.Chartreuse)
    lstColor.Add(Color.CadetBlue)
    lstColor.Add(Color.Orange)
    lstColor.Add(Color.DarkMagenta)
    lstColor.Add(Color.Violet) 'for highest number
End Sub

Private Sub FillTextBoxList(StartNumber As Integer, EndNumber As Integer)
    lstTextBox.Clear()
    For suffix = StartNumber To EndNumber
        lstTextBox.Add(DirectCast(Controls("TextBox" & suffix.ToString), TextBox))
    Next
End Sub

Private Function SortList() As List(Of TextBox)
    Dim orderedList = From txt In lstTextBox Order By CInt(txt.Text) Descending Select txt '$"{scorer.Score} - {scorer.Name}"
    Dim SortedList As List(Of TextBox) = orderedList.ToList
    Return SortedList
End Function

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    ColorTextBoxes()
End Sub

答案 1 :(得分:-1)

您可以使用Tuple来保留对按钮及其值的引用。然后,您可以使用OrderBy扩展方法通过文本框的值对元组列表进行排序。参见下面的代码:

Dim list As New List(Of Tuple(Of TextBox, Integer))
' Here you need to initilize list of buttons
Dim txts As List(Of TextBox)
' Here you sort you text boxes based on their value,
' later you just loop through list of tuples assigning each
' text box color, which you could also store in list
list = txts _
  .Select(Function(txt) New Tuple(Of TextBox, Integer)(txt, Integer.Parse(txt.Text))) _
  .OrderBy(Function(tuple) tuple.Item2)
' Here you loop through sorted list setting each color.
' Here youinitialize list with colors.
Dim colors As List(Of Color)
For i = 0 To list.Count
  list(i).Item1.BackColor = colors(i)
Next

注意:我使用了容易发生异常的integer.Parse,请考虑使用TryParse istead。

此外,您需要确保数组大小相同!

编辑:

即使不使用Tuple也可以做到:

txts = txts.OrderBy(Function(txt) Integer.Parse(txt.Text))
相关问题