Change Textbox Backcolor and Forecolor at Runtime
The code Jacob gave you makes the color change automatic (see my follow up
comments to him though)... change the text, move out of the TextBox and the
color changes right then and there. To do that, you need event code and
event code means you have to have one for each control you want it to apply
to. I don't see what your resistance is to just putting the 28 Exit event
procedures in the code window... just use the modification I posted back to
Jacob and all your 28 Exit events have to have in them is the single
subroutine name ChangeColor... that's it... do it once and you are done. If
you are worried about future code maintenance, you don't have to be... all
active code is in the single ChangeColor subroutine... any change you make
there is used by all 28 TextBoxes without you having to ever touch their
Exit event procedures. If you go with the For Each routine Jacob just
posted, the changing of the color will not be automatic.. you will have to
manually kick off the code yourself (perhaps by pushing a button)... forget
to do it and the color scheme will be wrong. I think Jacob's original event
procedure method is the better way to go.
--
Rick (MVP - Excel)
"Ayo" wrote in message
...
This still involves writing 28 TextBox1_Exit subroutine. I am looking for
an
option that only involves one For Each .... Next sub.
"Jacob Skaria" wrote:
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.
|