473,378 Members | 1,507 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,378 software developers and data experts.

enumerate controls - Why doesnt this work?

I had this:

dim t as new textbox
for each t in me.controls
'do something
next

but I am getting an error...so I tried doing this and it worked:

Dim c As New Control
For Each c In Me.Controls
If c.GetType Is GetType(System.Windows.Forms.TextBox) Then
c.BackColor = Color.HotPink
End If

Next

whyizit? I am sure I have done it the above way previously...

--
--Eric Cathell, MCSA
Nov 23 '05 #1
4 2247
Not a "nettie" here... and no help but.... why use As New here?

In VB6, there would be no need for As New
'======
Private Sub Form_Load()
Dim c As Control
For Each c In Me.Controls
If TypeOf c Is TextBox Then
c.Text = "Here's a box!"
End If
Next
End Sub
'======

--
Ken Halter - MS-MVP-VB (visiting from VB6 world) - http://www.vbsight.com
Please keep all discussions in the groups..
"ECathell" <ec******@nospam.com> wrote in message
news:ew**************@TK2MSFTNGP12.phx.gbl...
I had this:

dim t as new textbox
for each t in me.controls
'do something
next

but I am getting an error...so I tried doing this and it worked:

Dim c As New Control
For Each c In Me.Controls
If c.GetType Is GetType(System.Windows.Forms.TextBox) Then
c.BackColor = Color.HotPink
End If

Next

whyizit? I am sure I have done it the above way previously...

--
--Eric Cathell, MCSA
Nov 23 '05 #2
I don't see how that old code ever would have worked.

You definately don't need the new keyword in your Dim, in fact you can avoid the Dim altogether.

For Each c As Control In Me.Controls
If TypeOf c Is TextBox Then
c.Text = "Here's a box!"
End If
Next

Greg

"ECathell" <ec******@nospam.com> wrote in message news:ew**************@TK2MSFTNGP12.phx.gbl...
I had this:

dim t as new textbox
for each t in me.controls
'do something
next

but I am getting an error...so I tried doing this and it worked:

Dim c As New Control
For Each c In Me.Controls
If c.GetType Is GetType(System.Windows.Forms.TextBox) Then
c.BackColor = Color.HotPink
End If

Next

whyizit? I am sure I have done it the above way previously...

--
--Eric Cathell, MCSA
Nov 23 '05 #3
>"ECathell" <ec******@nospam.com> wrote in message
news:ew**************@TK2MSFTNGP12.phx.gbl...
I had this:

dim t as new textbox
for each t in me.controls
'do something
next
This would work if you know that *every* control is a textbox
Since that is rarely the case, you'll probably get a type casting exception
Dim c As New Control
For Each c In Me.Controls
If c.GetType Is GetType(System.Windows.Forms.TextBox) Then
c.BackColor = Color.HotPink
End If

Next

whyizit? I am sure I have done it the above way previously...


Here you treat everything as a Control, which is always possible since
the basic type contained in the Controls collection is Control.

And as others have said already, remove the New keyword

/claes

Nov 23 '05 #4
Thanks for the replies everyone. After looking through my code I realize I was confused about the enumerations because of my strongly typed collections. Also thanks for the tips about the 'new'

--
--Eric Cathell, MCSA
"ECathell" <ec******@nospam.com> wrote in message news:ew**************@TK2MSFTNGP12.phx.gbl...
I had this:

dim t as new textbox
for each t in me.controls
'do something
next

but I am getting an error...so I tried doing this and it worked:

Dim c As New Control
For Each c In Me.Controls
If c.GetType Is GetType(System.Windows.Forms.TextBox) Then
c.BackColor = Color.HotPink
End If

Next

whyizit? I am sure I have done it the above way previously...

--
--Eric Cathell, MCSA
Nov 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Cliff Harris | last post by:
I have an odd question that I'm hoping someone can help with. I simply (or not simply?) need to enumerate through all of the data types in the .Net framework. I do understand that if this is...
1
by: MuZZy | last post by:
Hi, I need to build a function which would return all controls which are upper in z-order relatively to given control. I need this in order to be able to temporary bring the control to front...
5
by: HL | last post by:
Hi, I need to enumerate windows and find the sum of the rect of all the windows of a specific application. In C++, I use the APIs - 'EnumWindows , GetWindowRect and UnionRect to accomplish the...
8
by: Connor T | last post by:
Hi, I posted this a while back, and got a solution but it didnt work. The question was: I have a form which is dynamically generated from the contents of a Database using commands such as...
1
by: smichr | last post by:
I see that there is a thread of a similar topic that was posted recently ( enumerate with a start index ) but thought I would start a new thread since what I am suggesting is a little different. ...
1
by: gjohncock | last post by:
I am working on a project that requires me to be able to enumerate through all the controls that may be selected (ie: showing their selection handles) in a forms design view window. I have managed...
6
by: Gregory Petrosyan | last post by:
Hello! I have a question for the developer of enumerate(). Consider the following code: for x,y in coords(dots): print x, y When I want to iterate over enumerated sequence I expect this to...
3
by: Mike Collins | last post by:
I have some controls that I am creating dynamically. After the user finishes entering their answers in these controls, I would like to iterate through these controls and get the answers out. I have...
15
by: rizwanahmed24 | last post by:
Hello i have made a custom control. i have placed a panel on it. I want this panel to behave just like the normal panel. The problem i was having is that the panel on my custom control doesnt...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.