如何更改比较多个文本框

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

标签: vb.net

我有2个代码可将颜色更改为“文本框”,但不幸的是,它们均无作用。怎么了为什么代码不好。每次尝试使用该代码(均不起作用)时,如果第一个代码的值介于1到7之间,则第一个代码将颜色更改为文本框,第二个代码将其值从最低到最高的升序更改,并分配相应的颜色。请告诉我这两个代码是否正确编写,或者有写错误。

代码1:图片:http://www.imagebam.com/image/5ac5ee1073004874

Public Class TextBoxColors
    Private ColorTable As Dictionary(Of String, Color) = New Dictionary(Of String, Color)()
    Public Sub New()
        ColorTable.Add("1", Color.Red)
        ColorTable.Add("2", Color.Aqua)
        ColorTable.Add("3", Color.Chocolate)
        ColorTable.Add("4", Color.BlanchedAlmond)
        ColorTable.Add("5", Color.BurlyWood)
        ColorTable.Add("6", Color.BlueViolet)
        ColorTable.Add("7", Color.DarkBlue)
    End Sub
    Public Function GetColor(ColorMap As String) As Color
        Return If(ColorTable.Keys.Contains(ColorMap), ColorTable(ColorMap), Color.White)
    End Function
End Class

Private txtColor As TextBoxColors = New TextBoxColors()

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    For Each txtDraw As TextBox In Me.Controls.OfType(Of TextBox).Where(Function(txt) txt.Name.StartsWith("txtDraw"))
        AddHandler txtDraw.TextChanged,
            Sub()
                If Not String.IsNullOrEmpty(txtDraw.Text) Then
                    txtDraw.BackColor = txtColor.GetColor(txtDraw.Text)
                End If
            End Sub
    Next
End Sub

代码2:必须根据我的文本框(以SumtxtDraw开头)更改此第二个代码 -图片:http://www.imagebam.com/image/92a4091073004904

 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

0 个答案:

没有答案