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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to loop through all the textboxes on a form and set backcolor 2

Status
Not open for further replies.

lminmei

Programmer
Feb 1, 2000
111
US
i want to be able to loop through all the textboxes on a form and when each textbox has focus, i want to set the background color to (somecolor..like blue) and set it back to white when the textbox loses focus...

does anyone know how to do this?
TIA,
Derek
 
The function will do the job for you. You need to pass it the form. It also looks to see if you have any container objects on the form panel etc.. . You call the function like this.

ClearChildText(Me.Controls)


Private Sub ClearChildText(ByVal contin As Control.ControlCollection)

Dim foundcontrol As Control


For Each foundcontrol In contin
If foundcontrol.GetType.ToString = "System.Windows.Forms.TextBox" Then
CType(foundcontrol, TextBox).BackColor= Color.Blue
End If

If foundcontrol.Controls.Count <> 0 Then
ClearChildText(foundcontrol.Controls)
End If
Next foundcontrol

End Sub

DotNetDoc
M.C.S.D.
---------------------------------------

Tell me and I forget. Show me and I remember. Involve me and I understand.
- Anonymous Chinese Proverb
-----------------------------------
If you can't explain it simply, you don't understand it well enough.
- A. Einstein
 
i tried your suggestion and it colors all the textbox backgrounds on my form to blue.... i just want a textbox's backcolor to change when it receives focus...
thanks for your help, i'm gonna see if i can use your example to do what i'm trying to do....
 
Here try this.

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Put this in your load event

TextBoxEvents(Me.Controls)


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5

Add two sub proceedures without a handles clause



Private Sub txtBox_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs)
CType(sender, TextBox).BackColor = Color.Blue
End Sub

Private Sub txtBox_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs)
CType(sender, TextBox).BackColor = Color.Gray
End Sub


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Then add this Sub. This should be close to what you are looking for




Private Sub TextBoxEvents(ByVal contin As Control.ControlCollection)

Dim foundcontrol As Control


For Each foundcontrol In contin
If foundcontrol.GetType.ToString = &quot;System.Windows.Forms.TextBox&quot; Then

AddHandler CType(foundcontrol, TextBox).GotFocus, AddressOf txtBox_GotFocus
AddHandler CType(foundcontrol, TextBox).LostFocus, AddressOf txtBox_LostFocus

'CType(foundcontrol, TextBox).BackColor = Color.Blue
End If

If foundcontrol.Controls.Count <> 0 Then
TextBoxEvents(foundcontrol.Controls)
End If
Next foundcontrol

End Sub

DotNetDoc
M.C.S.D.
---------------------------------------

Tell me and I forget. Show me and I remember. Involve me and I understand.
- Anonymous Chinese Proverb
-----------------------------------
If you can't explain it simply, you don't understand it well enough.
- A. Einstein
 
Thank you very much DotNetDoc!!! I'm starting to understand the logic more.....
Thanks again.
 
DocNetDoc: Bless you. I have struggled mightily to find a reliable procedure to reset different types of controls on my forms and you have shown me the way. Your solution is elegant and concise, two hallmarks of excellence in coding. My sincerest thanks for your solution to a problem that had been gnawing at me quite a while.
 
Glad I could help :)

DotNetDoc
M.C.S.D.
---------------------------------------

Tell me and I forget. Show me and I remember. Involve me and I understand.
- Anonymous Chinese Proverb
-----------------------------------
If you can't explain it simply, you don't understand it well enough.
- A. Einstein
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top