View Single Post
  #5   Report Post  
Posted to microsoft.public.excel.programming
Rick Rothstein Rick Rothstein is offline
external usenet poster
 
Posts: 5,934
Default Change Textbox Backcolor and Forecolor at Runtime

Two points about your code (even though it appears not to be what the OP
wanted)... first, you should probably have an Else block in your ChangeColor
subroutine to change the colors back to the default fore and back colors if
the text in the box is changed from "actual" and "projected" to something
else (otherwise they remain colored); and second, because this is a UserForm
and you are calling your ChangeColor subroutine from an event, you don't
have to pass the TextBox as an argument to the subroutine, you can just
refer to it through the UserForm's ActiveControl object. Here is what I am
thinking...

Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
ChangeColor
End Sub

Private Sub TextBox2_Exit(ByVal Cancel As MSForms.ReturnBoolean)
ChangeColor
End Sub

Private Sub ChangeColor()
With UserForm1.ActiveControl
If .Text = "actual" Then
.BackColor = &H8000&
.ForeColor = &HFFFFFF
ElseIf .Text = "projected" Then
.BackColor = &H8000&
.ForeColor = &HFFFFFF
Else
.BackColor = &H80000005
.ForeColor = &H80000008
End If
End With
End Sub

--
Rick (MVP - Excel)


"Jacob Skaria" wrote in message
...
Try the below...

Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
ChangeColor Me.TextBox1
End Sub

Private Sub TextBox2_Exit(ByVal Cancel As MSForms.ReturnBoolean)
ChangeColor Me.TextBox2
End Sub

Private Sub ChangeColor(txtTemp As MSForms.TextBox)
If txtTemp.Text = "actual" Then
txtTemp.BackColor = &H8000&
txtTemp.ForeColor = &HFFFFFF
ElseIf txtTemp.Text = "projected" Then
txtTemp.BackColor = &H8000&
txtTemp.ForeColor = &HFFFFFF
End If
End Sub


If this post helps click Yes
---------------
Jacob Skaria


"Ayo" wrote:

I have a UserForm that contains about 28 TextBoxes. I need to change the
Back
and Fore color of the textboxes based on the values inside each textbox.
Below is an example of what I am looking to accomplish:
Private Sub txtbx2_Change()
If txtbx2.Text = "actual" Then
ctl.BackColor = &H8000&
ctl.ForeColor = &HFFFFFF
ElseIf txtbx2.Text = "projected" Then
ctl.BackColor = &H8000&
ctl.ForeColor = &HFFFFFF
End If
End Sub

but I don't want to have to write this code for each and every textbox
on
the form. Is there a way to do this within one subroutine?
I am looking for one subrutine that would automatically update the
textboxes
once the value in it is change and focus is set to another control on the
form.