By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,761 Members | 1,212 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,761 IT Pros & Developers. It's quick & easy.

How to pass controls to subs?

P: n/a
I'd like to change the background color of each textbox on my form to a
different color when entered. The code below doesn't work (invalid cast
error) but you should be able to get the gist of what I want to do. Any
help is appreciated. Thanks.

Private Sub txtHubMnemonic_Enter(ByVal sender As Object, ByVal e As
System.EventArgs) Handles txtHubMnemonic.Enter
'SetBackColor(sender)
SetBackColor(txtHubMnemonic)
End Sub

Private Sub SetBackColor(ByRef MyTB As TextBox)
For Each TB As TextBox In Me.Controls
TB.BackColor = System.Drawing.Color.White
Next
MyTB.BackColor = System.Drawing.Color.BlanchedAlmond
End Sub

*** Sent via Developersdex http://www.developersdex.com ***
Nov 21 '05 #1
Share this Question
Share on Google+
3 Replies

P: n/a
"Terry Olsen" <to******@hotmail.com> schrieb:
I'd like to change the background color of each textbox on my form to a
different color when entered. The code below doesn't work (invalid cast
error) but you should be able to get the gist of what I want to do.
[...]
Private Sub txtHubMnemonic_Enter(ByVal sender As Object, ByVal e As
System.EventArgs) Handles txtHubMnemonic.Enter
'SetBackColor(sender)
SetBackColor(txtHubMnemonic)
=> 'SetBackColor(DirectCast(sender, TextBox))'.
Private Sub SetBackColor(ByRef MyTB As TextBox)
'ByRef' => 'ByVal'
For Each TB As TextBox In Me.Controls


\\\
For Each ctr As Control In Me.Controls
If TypeOf ctr Is TextBox Then
DirectCast(ctr, TextBox).<...> = ...
...
....
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
Nov 21 '05 #2

P: n/a
Thanks! That worked... Of cource I had to nest the For...Each due to each
of my text boxes being in its own GroupBox... But I got it dun!

"Herfried K. Wagner [MVP]" <hi***************@gmx.at> wrote in message
news:uE**************@TK2MSFTNGP15.phx.gbl...
"Terry Olsen" <to******@hotmail.com> schrieb:
I'd like to change the background color of each textbox on my form to a
different color when entered. The code below doesn't work (invalid cast
error) but you should be able to get the gist of what I want to do.
[...]
Private Sub txtHubMnemonic_Enter(ByVal sender As Object, ByVal e As
System.EventArgs) Handles txtHubMnemonic.Enter
'SetBackColor(sender)
SetBackColor(txtHubMnemonic)


=> 'SetBackColor(DirectCast(sender, TextBox))'.
Private Sub SetBackColor(ByRef MyTB As TextBox)


'ByRef' => 'ByVal'
For Each TB As TextBox In Me.Controls


\\\
For Each ctr As Control In Me.Controls
If TypeOf ctr Is TextBox Then
DirectCast(ctr, TextBox).<...> = ...
...
...
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #3

P: n/a
"Terry Olsen" <to******@hotmail.com> schrieb:
Of cource I had to nest the For...Each due to each of my text boxes being
in its own GroupBox...


Alternatively you can use a more flexible recursive function for control
enumeration:

\\\
Private Sub RecurseControls(ByVal ctr As Control)
Debug.WriteLine(ctr.Name)
If ctr.HasChildren Then
For Each c As Control In ctr.Controls
RecurseControls(c)
Next c
End If
End Sub
..
..
..
RecurseControls(Me)
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.