Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Setting Property of Current Text Control

Status
Not open for further replies.

Michael42

Programmer
Oct 8, 2001
1,454
US
Hello,

In VB5 using the gotfocus event I'd like to change the background color of the current textbox (Text1.BackColor = &HFFFF& etc.).

How can I do this without having to know the controls name?

Thanks,

Michael42
 
Which got focus event are you referring to? The form, text1,text2,text3?
 
Try this
[Code}
Private Sub Text1_GotFocus()
Me.ActiveControl.BackColor = &HFFFF&
End Sub
{/code}
HTH
Michael
 
Hello,

>> Me.ActiveControl.BackColor = &HFFFF&

This almost does it. I am trying to change the bacground color of a text box that gets focus and reset it back to white when focus leaves it.

The problem I am having with this is that it will set the background color but the same code (with the correct value to for white) does not unset it on the LostFocus event.

It does work if I hard hard both events (text1.BackColor = &HFFFF& etc.)

My goal is offcourse to have one command for changing the BackColor not having to know every controls name.

Any suggestions?



Thanks,

Michael42
 
You cannot use Me.ActiveControl.BackColor in the lost_focus event because the other textbox has the focus. It is only good for the object that has the focus.

Private Sub Text1_GotFocus()
Me.ActiveControl.BackColor = &HFFFFF
End Sub

Private Sub Text1_LostFocus()
Me.Text1.BackColor = &HFFFFFF
Debug.Print Me.ActiveControl
End Sub

Private Sub Text2_GotFocus()
Me.ActiveControl.BackColor = &HFFFFF
End Sub

Private Sub Text2_LostFocus()
Me.Text2.BackColor = &HFFFFFF
Debug.Print Me.ActiveControl
End Sub
 
This seems to work....

Place a textbox on the form then COPY it and PASTE 3 more text1 boxes on the form. This sets up a control array.

'This is the code

Private Sub Form_Load()
For text = 0 To Text1.Count - 1
Me.Text1(text).BackColor = &HFFFFFF
Next
End Sub

Private Sub Text1_GotFocus(Index As Integer)
Me.ActiveControl.BackColor = &HFFFFF
End Sub

Private Sub Text1_LostFocus(Index As Integer)
Dim text
For text = 0 To Text1.Count - 1
Me.Text1(text).BackColor = &HFFFFFF
Next
End Sub
 
I made a small booboo, there should be a
Dim text
statement in the form_load.....

Private Sub Form_Load()
Dim text
For text = 0 To Text1.Count - 1
Me.Text1(text).BackColor = &HFFFFFF
Next
End Sub
 
vbrocks,

>> ...This sets up a control array. ...

Very nice solution!



Thanks,

Michael42
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top