473,387 Members | 1,493 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to pass controls to subs?

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
3 994
"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
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
"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: | last post by:
In global.asa I have some code outside all the Subs. I have some confirmation that it is being executed OnStart. Yet I can's see if it is executed OnEnd. The literature I have says that OnEnd...
13
by: Steven Scaife | last post by:
I have decided to re-write the intranet site i created over a year ago. The coding is pretty awful and hard to read cos I made the mistake of not putting comments in or putting crappy comments in...
8
by: darrel | last post by:
This is a follow-up to a question I asked yesterday. I'm loading a UC programatically as such: =================================================== public customContentControl as UserControl...
0
by: ben | last post by:
I am having some difficulty in understanding how to deal with variables, session states, objects, user controls while attempting to develop a ASP.Net application. I have created several user...
8
by: Jerry Spence1 | last post by:
I am trying to create timers on demand by doing the following: Dim NewTimer As New System.Timers.Timer NewTimer.Interval = 10000 AddHandler NewTimer.Elapsed, AddressOf Timeup NewTimer.Enabled =...
10
by: Michael | last post by:
Hi Everyone. I have been designing a form with about 100 or so controls and today I pasted some code from another test project into this one and then all the controls on the form disapeared from...
1
by: Fadi.MM | last post by:
Hi Guys, I'm trying to create a library of ASP.NET user controls. What i'm trying to do is to encapsulate all my controls in one library (DLL) and re-using them in other projects. Everything is...
15
by: Arpan | last post by:
Consider the following code which retrieves data from a SQL Server 2005 DB table & displays it in a DataGrid: <script runat="server"> Sub Page_Load(ByVal obj As Object, ByVal ea As EventArgs)...
2
by: John Kotuby | last post by:
Hi all, I have a page that displays the contents of a business document in HTML that uses multiple select queries to provide the relevent data. There is a call to the parent table and then...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.