如何根据vba中的值更改文本框的背景颜色?

时间:2022-05-24 20:58:25

So I have 2 textboxes A and B which by a commandbutton click gets populated automatically.

所以我有2个文本框A和B,它们通过命令按钮单击自动填充。

How do I change the background color of the textboxes when the values in them change ( If a user changes the values in them manually) ?

如果文本框中的值发生更改(如果用户手动更改其中的值),如何更改文本框的背景颜色?

Thanks a lot!

非常感谢!

2 个解决方案

#1


1  

Vinay, You would do that like this:

Vinay,你会这样做:

Private Sub TextBox1_Change()
   TextBox1.BackColor = RGB(255, 0, 0)
End Sub

You'll want to put that in the TextBox1 object (double clicking it will get you there). The color change will be triggered when the value of the text box is changed. If I've understudy your question correctly, this should solve your problem. If not, please let me know.

你想把它放在TextBox1对象中(双击它会让你到那里)。更改文本框的值时将触发颜色更改。如果我正确地研究你的问题,这应该可以解决你的问题。如果没有,请告诉我。

SUPPLEMENT : A breakdown of the code

补充:代码细分

Private Sub TextBox1_Change() is an event handler. The code inside will run whenever a particular event occurs. In this case, whenever the text inside the TextBox1 changes. TextBox1.BackColor = RGB(255, 0, 0)

Private Sub TextBox1_Change()是一个事件处理程序。只要发生特定事件,内部代码就会运行。在这种情况下,只要TextBox1中的文本发生更改。 TextBox1.BackColor = RGB(255,0,0)

TextBox1 is an object, in this case it is your first texbox which is by default named "TextBox1", but we could use any other object in your form, "TextBox2", "Button1" etc...

TextBox1是一个对象,在这种情况下它是你的第一个texbox默认名为“TextBox1”,但我们可以使用你的表单中的任何其他对象,“TextBox2”,“Button1”等......

.BackColor is a property of TextBox1 (its Back Color). We could set the BackColor property to any valid color, but here I went with red.

.BackColor是TextBox1(其Back Color)的属性。我们可以将BackColor属性设置为任何有效的颜色,但在这里我用红色。

RGB stands for Red Green Blue. In this case we set the back of the TextBox to a color equil to 255 (All Red), 0 (with no Green), and 0 (No Blue).

RGB代表红绿蓝。在这种情况下,我们将TextBox的背面设置为等于255(全红),0(没有绿色)和0(无蓝色)的颜色。

SUPPLEMENT 2 So, based on your last comment, you would be looking to do something like this:

补充2因此,根据您的上一条评论,您可能希望执行以下操作:

Private Sub TextBox1_Change()
    If TextBox1.Text = "5" Then
        TextBox1.BackColor = RGB(255, 255, 255)
    ElseIf TextBox1.Text = "6" Or TextBox1.Text = "4" Or TextBox1.Text = "5.6" Then
        TextBox1.BackColor = RGB(255, 0, 0)
    End If
End Sub

#2


2  

When you click on the CommandButton:

当您单击CommandButton时:

TextBox1.Backcolor = 'something (example: &H000000FF&)

If the content of the TextBox is changed, use:

如果更改了TextBox的内容,请使用:

Private Sub TextBox1_Change()
   'Change the color
   'Just an example:

   If TextBox1.Text = "Hello" then
      TextBox1.Backcolor = &H0000C000&
   Else 
      TextBox1.Backcolor = &H000000FF&
   End If

End Sub

The .0 thing:

.0的事情:

Label1.Caption = Ineteger & ".0"

You really don't need to round or anything, because you have Integers.

你真的不需要圆形或任何东西,因为你有整数。

#1


1  

Vinay, You would do that like this:

Vinay,你会这样做:

Private Sub TextBox1_Change()
   TextBox1.BackColor = RGB(255, 0, 0)
End Sub

You'll want to put that in the TextBox1 object (double clicking it will get you there). The color change will be triggered when the value of the text box is changed. If I've understudy your question correctly, this should solve your problem. If not, please let me know.

你想把它放在TextBox1对象中(双击它会让你到那里)。更改文本框的值时将触发颜色更改。如果我正确地研究你的问题,这应该可以解决你的问题。如果没有,请告诉我。

SUPPLEMENT : A breakdown of the code

补充:代码细分

Private Sub TextBox1_Change() is an event handler. The code inside will run whenever a particular event occurs. In this case, whenever the text inside the TextBox1 changes. TextBox1.BackColor = RGB(255, 0, 0)

Private Sub TextBox1_Change()是一个事件处理程序。只要发生特定事件,内部代码就会运行。在这种情况下,只要TextBox1中的文本发生更改。 TextBox1.BackColor = RGB(255,0,0)

TextBox1 is an object, in this case it is your first texbox which is by default named "TextBox1", but we could use any other object in your form, "TextBox2", "Button1" etc...

TextBox1是一个对象,在这种情况下它是你的第一个texbox默认名为“TextBox1”,但我们可以使用你的表单中的任何其他对象,“TextBox2”,“Button1”等......

.BackColor is a property of TextBox1 (its Back Color). We could set the BackColor property to any valid color, but here I went with red.

.BackColor是TextBox1(其Back Color)的属性。我们可以将BackColor属性设置为任何有效的颜色,但在这里我用红色。

RGB stands for Red Green Blue. In this case we set the back of the TextBox to a color equil to 255 (All Red), 0 (with no Green), and 0 (No Blue).

RGB代表红绿蓝。在这种情况下,我们将TextBox的背面设置为等于255(全红),0(没有绿色)和0(无蓝色)的颜色。

SUPPLEMENT 2 So, based on your last comment, you would be looking to do something like this:

补充2因此,根据您的上一条评论,您可能希望执行以下操作:

Private Sub TextBox1_Change()
    If TextBox1.Text = "5" Then
        TextBox1.BackColor = RGB(255, 255, 255)
    ElseIf TextBox1.Text = "6" Or TextBox1.Text = "4" Or TextBox1.Text = "5.6" Then
        TextBox1.BackColor = RGB(255, 0, 0)
    End If
End Sub

#2


2  

When you click on the CommandButton:

当您单击CommandButton时:

TextBox1.Backcolor = 'something (example: &H000000FF&)

If the content of the TextBox is changed, use:

如果更改了TextBox的内容,请使用:

Private Sub TextBox1_Change()
   'Change the color
   'Just an example:

   If TextBox1.Text = "Hello" then
      TextBox1.Backcolor = &H0000C000&
   Else 
      TextBox1.Backcolor = &H000000FF&
   End If

End Sub

The .0 thing:

.0的事情:

Label1.Caption = Ineteger & ".0"

You really don't need to round or anything, because you have Integers.

你真的不需要圆形或任何东西,因为你有整数。