我想尽量减少这段代码。我大约有8个文本框,并且花了很多时间,从txtDrawA1
到txtDraw8
。我想更轻松地更改文本框颜色,具体取决于以下值1-2-3-4-5-6-7-每个值都分配了相同的颜色。
Private Sub txtDrawA1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtDrawA1.TextChanged
If txtDrawA1.Text = ("1") Then
txtDrawA1.BackColor = Color.Red
ElseIf txtDrawA1.Text = ("2") Then
txtDrawA1.BackColor = Color.Aqua
ElseIf txtDrawA1.Text = ("3") Then
txtDrawA1.BackColor = Color.Chocolate
ElseIf txtDrawA1.Text = ("4") Then
txtDrawA1.BackColor = Color.BlanchedAlmond
ElseIf txtDrawA1.Text = ("5") Then
txtDrawA1.BackColor = Color.BurlyWood
ElseIf txtDrawA1.Text = ("6") Then
txtDrawA1.BackColor = Color.BlueViolet
ElseIf txtDrawA1.Text = ("7") Then
txtDrawA1.BackColor = Color.DarkBlue
End If
End Sub
Private Sub txtDrawA2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtDrawA2.TextChanged
If txtDrawA2.Text = ("1") Then
txtDrawA2.BackColor = Color.Red
ElseIf txtDrawA1.Text = ("2") Then
txtDrawA2.BackColor = Color.Aqua
ElseIf txtDrawA2.Text = ("3") Then
txtDrawA2.BackColor = Color.Chocolate
ElseIf txtDrawA2.Text = ("4") Then
txtDrawA2.BackColor = Color.BlanchedAlmond
ElseIf txtDrawA2.Text = ("5") Then
txtDrawA2.BackColor = Color.BurlyWood
ElseIf txtDrawA2.Text = ("6") Then
txtDrawA2.BackColor = Color.BlueViolet
ElseIf txtDrawA1.Text = ("7") Then
txtDrawA2.BackColor = Color.DarkBlue
End If
答案 0 :(得分:1)
您可以将代码放在一个子目录中,并使用一个子目录处理来自不同文本框的多个事件。
Private Sub txtDrawA1_TextChanged(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles txtDrawA1.TextChanged,
txtDrawA2.TextChanged,
txtDrawA3.TextChanged
'I just did three you can add as many as you like make sure that you select the correct event
SetColor(sender)
End Sub
Public Sub SetColor(txtBox As TextBox)
If txtBox.Text = ("1") Then
txtBox.BackColor = Color.Red
ElseIf txtBox.Text = ("2") Then
txtBox.BackColor = Color.Aqua
ElseIf txtBox.Text = ("3") Then
txtBox.BackColor = Color.Chocolate
ElseIf txtBox.Text = ("4") Then
txtBox.BackColor = Color.BlanchedAlmond
ElseIf txtBox.Text = ("5") Then
txtBox.BackColor = Color.BurlyWood
ElseIf txtBox.Text = ("6") Then
txtBox.BackColor = Color.BlueViolet
ElseIf txtBox.Text = ("7") Then
txtBox.BackColor = Color.DarkBlue
End If
End Sub
答案 1 :(得分:1)
例如,您可以按照以下步骤进行操作。
创建一个方法来处理八个文本框的TextChanged
事件,并每次调用第二个方法来设置适当的颜色。
我还添加了Case Else
,因为当文本框包含另一个值时,您可能想将背景色重置为默认值。
Private Sub TextBoxes_TextChanged(sender As Object, e As EventArgs) Handles txtDrawA1.TextChanged,
txtDrawA2.TextChanged,
txtDrawA3.TextChanged,
txtDrawA4.TextChanged,
txtDrawA5.TextChanged,
txtDrawA6.TextChanged,
txtDrawA7.TextChanged,
txtDrawA8.TextChanged
SetTextBoxColor(DirectCast(sender, TextBox))
End Sub
Sub SetTextBoxColor(txt As TextBox)
Select Case txt.Text
Case "1"
txt.BackColor = Color.Red
Case "2"
txt.BackColor = Color.Aqua
Case "3"
txt.BackColor = Color.Chocolate
Case "4"
txt.BackColor = Color.BlanchedAlmond
Case "5"
txt.BackColor = Color.BurlyWood
Case "6"
txt.BackColor = Color.BlueViolet
Case "7"
txt.BackColor = Color.DarkBlue
Case Else
txt.BackColor = SystemColors.Window
End Select
End Sub
答案 2 :(得分:0)
我正在提议一个特殊的类,该类将所有颜色映射保存在Dictionary(Of string, Color)
中,该类提供了一种基于字符串值(用作字典键)检索Color的方法。
如果<{1}}对应于当前TextBoxColors
,则 GetColor()
类 Key
方法将返回Color , 被定义为。否则,它返回TextBox.Text
。
在Color.White
事件中,事件处理程序都被添加到所有名称与 pattern 匹配的文本框中: Form.Load()
,在这里。
您可以改用Tag属性,在此选择中需要包含的所有TextBox中将其设置为特定值。
请注意,我假设所有TextBoxes都是Form的子级。如果它们是其他容器的子代,只需将"txtDraw"
中的Me
引用更改为实际的父代。
修改:
由于您使用的是Visual Studio 2008,因此不支持For Each txtDraw As TextBox In Me.Controls
语句中的Lambda。修改代码以符合(非常旧的)VB.Net版本。您需要引用AddHandler
,并且Framework版本必须为3.5或更高版本(以支持System. Linq
和OfType
扩展名)。
Where