473,657 Members | 2,587 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1903
"Shawn" <sa***********@ gmail.comwrote in message
news:48******** *************** ***********@u57 g2000hsf.google groups.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 SetAllTextBoxBa ckColor(ByVal ctrlContainer As Control,ByVal clr
As Color)
For Each ctrl As Control In ctrlContainer.C ontrols

If TypeOf ctrl Is TextBox Then

CType(ctrl, TextBox).BackCo lor = clr

Else

If ctrl.Controls.C ount 0 Then

'this one is a container so process it recursive

SetAllTextBoxBa ckColor(ctrl, clr)

End If

End If

Next

End Sub

usage :

SetAllTextBoxBa ckColor(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.comschree f in bericht
news:48******** *************** ***********@u57 g2000hsf.google groups.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 InitialiseField s()
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
1868
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 adOpenForwardOnly, adLockReadOnly dim adCmdTable, ctr, checkboxID
9
1832
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 for example, the click event of a form object so as to write a method for that event. Thanks for your help on this Regards Ant
4
4717
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 athletes.  I have now learned how to join two different tables in a query so that I might generate a report. Specifically, student athletes at a community college are required to graduate with an AA or AS degree.  Consequently, various steps are in...
1
3341
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 able to find any examples which demonstrate how to do this. My Code:
10
1741
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 projekt. The reason why I am asking is that I am creating a program and I need a Splash-screen (like the tiny screen that appears when programs like Word starts), and when the Splash-screen has been loaded for a couple of seconds it should be...
2
3650
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 /////////////////////////////////////////////////////////////////////////////////////// <form action="whatever.php" method="post"> <select name="zip_code" onchange="makeRequest('getCity.php?state='+this.form.zip_code.options.value)" multiple="multiple" size="20">
5
1330
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 in the form's status bar. For example, "Now deleting file 35 of 234." if the object was deleting a bunch of files. I have no idea how to do this, but I understand there's some thread-violation trickiness involved with having objects calling
0
10999
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 the CheckBoxList control in ASP .NET, I had a hard time figuring out how to dynamically select the items that needed to be selected. Specifically, I was displaying checkboxes for Roles, and I wanted to check the boxes of the roles that were...
3
3002
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
8394
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8306
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8825
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8503
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7327
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6164
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4152
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1955
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.