enumerate controls - Why doesnt this work?

  • Thread starter Thread starter ECathell
  • Start date Start date
E

ECathell

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...
 
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..


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...
 
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

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...
 
ECathell said:
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
 
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
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...
 
Back
Top