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

Iterate thru controls collection on a form?

P: n/a
Hello,

I have a form with 5 textboxes named txt0, txt1, txt2,
txt3, tx4.

In VB6 I could iterate through these with

For i = 0 to 4
debug.print Me.controls("txt" & i).Name
Next

In dotNet, I can't seem to do that.

Console.WriteLine(Me.Controls("txt" & i).Name)

Could someone share how to do this in dotnet vb?

Thanks,
Rich
Nov 20 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Hi Rich,

It doesn't appear that the form offers an easy way to find a control given
it's name. It's easy to do this by looping through the Controls collection
and find the one whose Name property matches the search string.

Note: You have to watch for container controls (e.g. Panel, GroupBox,
TabControl, etc.).If the control you're looking for is on a container
control the code below won't find it because it will belong to the container
control's Controls collection.

Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim ctl As Control
For i As Integer = 1 To 5
ctl = Me.FindControl("textbox" & i)
If Not ctl Is Nothing Then
ListBox1.Items.Add(ctl.Name)
End If
Next
End Sub

Private Function FindControl(ByVal name As String) As Control
For Each ctl As Control In Me.Controls
If ctl.Name.ToUpper = name.ToUpper Then
Return ctl
End If
Next
Return Nothing
End Function

--
Rob Windsor [MVP-VB]
G6 Consulting
Toronto, Canada

"Rich" <an*******@discussions.microsoft.com> wrote in message
news:d1****************************@phx.gbl...
Hello,

I have a form with 5 textboxes named txt0, txt1, txt2,
txt3, tx4.

In VB6 I could iterate through these with

For i = 0 to 4
debug.print Me.controls("txt" & i).Name
Next

In dotNet, I can't seem to do that.

Console.WriteLine(Me.Controls("txt" & i).Name)

Could someone share how to do this in dotnet vb?

Thanks,
Rich

Nov 20 '05 #2

P: n/a
If you find controls on the control, you can recursively call FindControl.

Tom
"Rob Windsor [MVP]" <rw******@NO.MORE.SPAM.bigfoot.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
Hi Rich,

It doesn't appear that the form offers an easy way to find a control given
it's name. It's easy to do this by looping through the Controls collection
and find the one whose Name property matches the search string.

Note: You have to watch for container controls (e.g. Panel, GroupBox,
TabControl, etc.).If the control you're looking for is on a container
control the code below won't find it because it will belong to the container control's Controls collection.

Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim ctl As Control
For i As Integer = 1 To 5
ctl = Me.FindControl("textbox" & i)
If Not ctl Is Nothing Then
ListBox1.Items.Add(ctl.Name)
End If
Next
End Sub

Private Function FindControl(ByVal name As String) As Control
For Each ctl As Control In Me.Controls
If ctl.Name.ToUpper = name.ToUpper Then
Return ctl
End If
Next
Return Nothing
End Function

--
Rob Windsor [MVP-VB]
G6 Consulting
Toronto, Canada

"Rich" <an*******@discussions.microsoft.com> wrote in message
news:d1****************************@phx.gbl...
Hello,

I have a form with 5 textboxes named txt0, txt1, txt2,
txt3, tx4.

In VB6 I could iterate through these with

For i = 0 to 4
debug.print Me.controls("txt" & i).Name
Next

In dotNet, I can't seem to do that.

Console.WriteLine(Me.Controls("txt" & i).Name)

Could someone share how to do this in dotnet vb?

Thanks,
Rich


Nov 20 '05 #3

P: n/a
Thank you all for your replies. That function worked
perfectly. And Thanks also for the recursive idea for
finding controls on controls. I was able to modify that
function to return values too. But I noticed that you are
diming ctl As control right in the For Loop. Cool, except
it didn't work on my version of dotnet (2002). Will be
getting 2003 this week. So I hope it was that 2002 did
not support that funcitonality because that was real
cool.

Rich
-----Original Message-----
Hi Rich,

It doesn't appear that the form offers an easy way to find a control givenit's name. It's easy to do this by looping through the Controls collectionand find the one whose Name property matches the search string.
Note: You have to watch for container controls (e.g. Panel, GroupBox,TabControl, etc.).If the control you're looking for is on a containercontrol the code below won't find it because it will belong to the containercontrol's Controls collection.

Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim ctl As Control
For i As Integer = 1 To 5
ctl = Me.FindControl("textbox" & i)
If Not ctl Is Nothing Then
ListBox1.Items.Add(ctl.Name)
End If
Next
End Sub

Private Function FindControl(ByVal name As String) As Control For Each ctl As Control In Me.Controls
If ctl.Name.ToUpper = name.ToUpper Then
Return ctl
End If
Next
Return Nothing
End Function

--
Rob Windsor [MVP-VB]
G6 Consulting
Toronto, Canada

"Rich" <an*******@discussions.microsoft.com> wrote in messagenews:d1****************************@phx.gbl...
Hello,

I have a form with 5 textboxes named txt0, txt1, txt2,
txt3, tx4.

In VB6 I could iterate through these with

For i = 0 to 4
debug.print Me.controls("txt" & i).Name
Next

In dotNet, I can't seem to do that.

Console.WriteLine(Me.Controls("txt" & i).Name)

Could someone share how to do this in dotnet vb?

Thanks,
Rich

.

Nov 20 '05 #4

P: n/a
> But I noticed that you are
diming ctl As control right in the For Loop. Cool, except
it didn't work on my version of dotnet (2002). Will be
getting 2003 this week. So I hope it was that 2002 did
not support that funcitonality because that was real
cool.


Your thinking along the lines of the variable in the for loop are absolutely
correct.

Nov 20 '05 #5

P: n/a
Cor
Hi Rich,

There is a way to loop to controls in the classic way.
Beneath a sample to give only those one handler for when they lose focus.

Although there are a lot of more solutions to do this.

I hope this helps,

Cor

\\\
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim txtboxarea As TextBox() = New TextBox() {TextBox1, TextBox2}
dim txt as TextBox
For Each txt In txtboxarea
AddHandler txt.LostFocus, AddressOf TextBox_LostFocus
Next
End Sub
Private Sub TextBox_LostFocus(ByVal sender As Object, _
ByVal e As System.EventArgs)
DirectCast(sender, TextBox).Text = _
Trim(DirectCast(sender, TextBox).Text)
End Sub
///
I hope this helps a little bit?

Cor

I have a form with 5 textboxes named txt0, txt1, txt2,
txt3, tx4.

In VB6 I could iterate through these with

For i = 0 to 4
debug.print Me.controls("txt" & i).Name
Next

In dotNet, I can't seem to do that.

Console.WriteLine(Me.Controls("txt" & i).Name)

Could someone share how to do this in dotnet vb?

Thanks,
Rich

Nov 20 '05 #6

P: n/a
Cor
Hi,

Why do I change always so much in my text that it becomes unreadable.

There is a way to loop to controls in the classic way.
Beneath a sample to give only those one handler for when they lose focus.


Beneath a sample to activate only one handler when one of the two textboxes
loose focus.

Cor
Nov 20 '05 #7

P: n/a
Thanks. This is very cool. Kinda like creating an array
of textboxes on the fly (like using x = array(txt0, txt1,
txt2) in vb6).

Thanks. Just what I was looking for.

Rich

-----Original Message-----
Hi Rich,

There is a way to loop to controls in the classic way.
Beneath a sample to give only those one handler for when they lose focus.
Although there are a lot of more solutions to do this.

I hope this helps,

Cor

\\\
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Dim txtboxarea As TextBox() = New TextBox() {TextBox1, TextBox2} dim txt as TextBox
For Each txt In txtboxarea
AddHandler txt.LostFocus, AddressOf TextBox_LostFocus
Next
End Sub
Private Sub TextBox_LostFocus(ByVal sender As Object, _
ByVal e As System.EventArgs)
DirectCast(sender, TextBox).Text = _
Trim(DirectCast(sender, TextBox).Text)
End Sub
///
I hope this helps a little bit?

Cor

I have a form with 5 textboxes named txt0, txt1, txt2,
txt3, tx4.

In VB6 I could iterate through these with

For i = 0 to 4
debug.print Me.controls("txt" & i).Name
Next

In dotNet, I can't seem to do that.

Console.WriteLine(Me.Controls("txt" & i).Name)

Could someone share how to do this in dotnet vb?

Thanks,
Rich

.

Nov 20 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.