Changing Textbox Color with Focus

AnarchyX
03-13-2003, 12:29 PM
Does anyone know how to change a textbox and font color without placing it in it's own Sub?

This is what I am trying to accomplish:

When a user sets focus on a text box the backcolor changes and font color changes. Once the focus is lost the textbox reverts back to white and the font to black.

Here is what I have so far:

Private Sub Text1_GotFocus()
Text1.BackColor = vbYellow ' Changes text box to yellow
Text1.ForeColor = vbRed ' Changes font to red
End Sub

Private Sub Text1_LostFocus()
Text1.BackColor = vbWhite 'Changes text box back to white after losing focus
Text1.ForeColor = vbBlack 'Changes font back to black after losing focus
End Sub

My question is: Is there anyway easier to do this? I have many text boxes per form. :huh:

TIA

dexmix
03-13-2003, 01:11 PM
have you considered control arrays?

AnarchyX
03-13-2003, 01:32 PM
Yes..That is what I would like to do..But how do I do this?

Its been a while... :huh:

All I remember is Dim MyCtrl as ?

mark82
03-13-2003, 02:11 PM
You need to name all of the text boxes the same and you will also find an index property, this is how to refer to each one individually.

BostjanK
03-14-2003, 02:14 AM
After you'll name all the controls the same, use this:

Private Sub Text1_GotFocus(Index As Integer)
Text1(Index)..BackColor = vbYellow ' Changes text box to yellow
Text1(Index)..ForeColor = vbRed ' Changes font to red
End Sub

Private Sub Text1_LostFocus(Index As Integer)
Text1(Index).BackColor = vbWhite 'Changes text box back to white after losing focus
Text1(Index)..ForeColor = vbBlack 'Changes font back to black after losing focus
End Sub

AnarchyX
03-14-2003, 10:18 AM
Thanks.. It works great..No more cramps.. :cool:

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum