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

selecting form object through a loop

I'm trying to go through a large number of logically named textboxes
using a loop and cannot figure out how short of explicitly referencing
every single one.
Oct 27 '08 #1
3 1892
"Shawn" <sa***********@gmail.comwrote in message
news:48**********************************@u57g2000 hsf.googlegroups.com...
I'm trying to go through a large number of logically named textboxes
using a loop and cannot figure out how short of explicitly referencing
every single one.
Perhaps something like:
For Each ctl As Control In Me.Controls

If TypeOf ctl Is TextBox Then

'do whatever

End If

Next

Be aware that some controls act as containers for other controls and contain
their own collection.
Oct 27 '08 #2
You can just loop through all controls on the form , check if that control
is a textbox
then cast it to a textbox control and read and set all properties and
methods just as normall

However note that if you use containers ( panels e.g. ) that you should
process them recursively

example :

Private Sub SetAllTextBoxBackColor(ByVal ctrlContainer As Control,ByVal clr
As Color)
For Each ctrl As Control In ctrlContainer.Controls

If TypeOf ctrl Is TextBox Then

CType(ctrl, TextBox).BackColor = clr

Else

If ctrl.Controls.Count 0 Then

'this one is a container so process it recursive

SetAllTextBoxBackColor(ctrl, clr)

End If

End If

Next

End Sub

usage :

SetAllTextBoxBackColor(Me, Color.Red)

the above code will find anny textbox on a form wether it is placed on a
panell , tabcontrol or whatever and set the backcolor to red
you can just as easy set or retrieve anny property of the controls you want

HTH

Michel Posseth
http://www.vbdotnetcoder.com


"Shawn" <sa***********@gmail.comschreef in bericht
news:48**********************************@u57g2000 hsf.googlegroups.com...
I'm trying to go through a large number of logically named textboxes
using a loop and cannot figure out how short of explicitly referencing
every single one.

Oct 27 '08 #3
Shawn wrote:
I'm trying to go through a large number of logically named textboxes
using a loop and cannot figure out how short of explicitly referencing
every single one.
The days of being able to sensibly "get at" a Control based solely on
its /Name/ are long gone.

Generally speaking, having the Name of something isn't much use.
Having a /reference/ to the thing itself (through which, of course, you
can ask the object its Name) is much, /much/ better.

Create yourself an array of TextBoxes - or an ArrayList containing
TextBoxes or (VB'2005+) a List(Of TextBox) - containing the ones you
want to work with and loop through that, as in:

Private m_allTextBoxes as New List(Of TextBox)

Private Sub InitialiseFields()
m_allTextBoxes.Add( Me.TextBox1 )
. . .
m_allTextBoxes.Add( Me.TextBox<n)
End Sub

''' <remarks type="rant" >
''' Is it just me that finds it annoying having "<" and ">" as part
''' of the language these days?
''' How are we supposed to mark non-syntactic bits of example code?
''' "<" and ">" are used for Attributes, "[" and "]" protect reserved
''' words and "(" and ")" have always been part and parcel of Basic...
''' Anyway ...
''' </remarks>

Private Sub Zap()
For Each tb As TextBox in m_allTextBoxes
tb.Text = String.Empty
Next
End Sub

HTH,
Phill W.
Oct 28 '08 #4

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

Similar topics

7
by: Rodney King | last post by:
Hi, I have developed an ASP page which dynamically displays a list of checkbox options based on a SQL statement. Here is my code: <div style="OVERFLOW:auto; Height: 150px"> <table> <% dim...
9
by: Ant | last post by:
Hi, I'm new to .NET, though program with VB. I'm used to selecting an event from the right side drop down box of the VB IDE. That seems to have changed with ..NET. How do you choose an event, say...
4
by: Sami | last post by:
I hope someone will tell me how to do this without having to do any VB as I know nothing in that area. I am a rank beginner in using Access. I have created a database consisting of student...
1
by: Bob Loveshade | last post by:
I am looking for an example that shows how to select and highlight multiple rows in a DataGrid. My DataGrid is part of a Web User Control which is contained in an ASPX page. I haven't been...
10
by: peter hansen | last post by:
I tried a couple of weeks ago but I'll try again :D I old VB6-days it was possible to unload a form bu using the Unload(Form) command. I have tried the Close-function but it'll unload the entire...
2
by: areef.islam | last post by:
Hi, I am kinda new to javascript and I am having this problem with selecting multiple options from a select tag. Hope someone can help me out here. here is my code...
5
by: sherifffruitfly | last post by:
Hi, I've got main() running in on a form. When a button on the form is clicked, an object from a class I made is instantiated, and does some stuff. I would like the stuff it does to be reflected...
0
by: jeremy | last post by:
Had a tough time figuring this one out and couldn't find a good solution, so I thought I would post this and hopefully it will help someone out. When using DataBind to dynamically bind a list to...
3
by: jambonjamasb | last post by:
Hi I have two tables: email_tbl Data_table Data table is is used to create a Form Data_form
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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.