Iterate thru controls collection on a form?

R

Rich

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
 
R

Rob Windsor [MVP]

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
 
T

Tom Scales

If you find controls on the control, you can recursively call FindControl.

Tom
Rob Windsor said:
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 said:
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
 
R

Rich

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



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


.
 
D

Da~One

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

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
 
C

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
 
R

Rich

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top