473,403 Members | 2,284 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,403 software developers and data experts.

Iterate thru controls collection on a form?

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

Similar topics

4
by: nc | last post by:
My iterator can find my collection when my Action class calls my jsp directly, however when my Action class calls an html file that is set up with IFrames (one of which is loading that same jsp), I...
2
by: Simon | last post by:
I've got a webform and many controls on it. I would like to iterate thru the control collection to get all the textboxes control to let me change the text property. Here is my code...for unknown...
16
by: TD | last post by:
This is the code under a command button - Dim ctl As Control For Each ctl In Me.Controls If ctl.BackColor <> RGB(255, 255, 255) Then ctl.BackColor = RGB(255, 255, 255) End If Next ctl
0
by: dantheman | last post by:
Hi, i'm doing a programe in a few languages so i've found this code that iterates tru the forms controls and returns there names but the problem is after re-parenting the controls to optimize...
2
by: JDavies | last post by:
Hi All I am dynamically Creating DropDownList boxes for a Survey form. These DropdownLists are added to Table Cells programatically. I tried to loop thru the controls on the form but it doesnt...
2
by: James Doran | last post by:
Hello, I'd like to iterate through each Page of my ASP.NET project from within a Custom web control and access the Page.Controls collection. I've tried using Reflection on the web project...
1
by: Dan Sikorsky | last post by:
How do you recursively iterate thru each collection on each form on a web page and find each web control? -- Thank you kindly, Dan Sikorsky BAB, BScE, MSC
2
by: Michael Beck | last post by:
I am new to .net programming and I want to know if I can loop thru a specified number of controls on an html form. In Access, the code looks like this: For i = 1 to 10 Me("txtAlternate" & i) =...
4
by: romy | last post by:
Hi In VB.net I have a set of linkButtons controls in a form , which I want to iterate on them and change their text property. How it's done ? thanks
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: 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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.