473,466 Members | 1,301 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to loop thru all textboxes on form?

How can I loop thru all textboxes on a form?

I want to remove any single quotes in the data entry. I was thinking
something like this:

For Each c As Control In Me.Controls
Dim t As TextBox = CType(c, TextBox)
t.Text = t.Text.Replace("'", "")
Next

But the CType is giving me a 'Specified cast is not valid'.

Thanks.
Nov 19 '05 #1
4 2164
Your page will contain many controls, so you have to check each
control's type before you can cast to a textbox. You can use the "Is"
operator - in C# it would look like:

if (c is TextBox) {
TextBox t = (TextBox) c;
t.Text = t.Text.Replace ("'", "");
}

but I dont know VB syntax.

An alternative would be to surround the statement in a try/catch
statement and catch the InvalidCastException and continue the loop,
however it's usually better to check for expected exceptions.

Nov 19 '05 #2
Hey,

A couple ideas:

Use the GetType() method to then test whether or not it is of type TextBox,
only taking action if it is.

A bit more brute force would be to wrap it in a try...catch and check for
the invalid cast, but that's not too pretty.

- John

"VB Programmer" <Do*****************@jEmail.com> wrote in message
news:ee**************@tk2msftngp13.phx.gbl...
How can I loop thru all textboxes on a form?

I want to remove any single quotes in the data entry. I was thinking
something like this:

For Each c As Control In Me.Controls
Dim t As TextBox = CType(c, TextBox)
t.Text = t.Text.Replace("'", "")
Next

But the CType is giving me a 'Specified cast is not valid'.

Thanks.

Nov 19 '05 #3
What everyone else said is accurate, but I wanted to point out that you'll
need to recursively scan through all controls - your code doesn't show that
you are doing this (doesn't show that you aren't either).

private sub StripFromTextbox(parent as Control, searchFor as string,
replaceWith as string)
for each c as control in parent
if c.HasControls then
StripFromTextbox(c, searchFor, replaceWith)
end if
if c is TextBox then
dim t as textbox = ctype(c, textbox)
t.text = t.text.replace(searchFor, replaceWith)
end if
end sub

or something to that effect...

Karl

--
MY ASP.Net tutorials
http://www.openmymind.net/
"VB Programmer" <Do*****************@jEmail.com> wrote in message
news:ee**************@tk2msftngp13.phx.gbl...
How can I loop thru all textboxes on a form?

I want to remove any single quotes in the data entry. I was thinking
something like this:

For Each c As Control In Me.Controls
Dim t As TextBox = CType(c, TextBox)
t.Text = t.Text.Replace("'", "")
Next

But the CType is giving me a 'Specified cast is not valid'.

Thanks.

Nov 19 '05 #4
check out this faq,
http://www.extremeexperts.com/Net/Ar...hControls.aspx

--
-Saravana
http://dotnetjunkies.com/WebLog/saravana/
www.ExtremeExperts.com

"VB Programmer" <Do*****************@jEmail.com> wrote in message
news:ee**************@tk2msftngp13.phx.gbl...
How can I loop thru all textboxes on a form?

I want to remove any single quotes in the data entry. I was thinking
something like this:

For Each c As Control In Me.Controls
Dim t As TextBox = CType(c, TextBox)
t.Text = t.Text.Replace("'", "")
Next

But the CType is giving me a 'Specified cast is not valid'.

Thanks.

Nov 19 '05 #5

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

Similar topics

1
by: Jeremy Langworthy | last post by:
Hi I have a dynamicly generated form (well the elements are at least) that looks something like this: while( not end of returned records): <input name="plan_id" type="checkbox" id=""...
3
by: brett | last post by:
Using DOM in IE, how can I loop through FORMs and access FORM elements in a specific form? For example, www.hotmail.com has about 13 forms. I believe the one displayed is dependent on the URL. If...
4
by: Kevin H | last post by:
Apologies in advance if this sounds slow-witted, but I didn't find it here. Need to populate some textboxes on a form. While I could hard code it (the number of options aren't that high), it...
2
by: staeri | last post by:
I have an unknown number of textboxes which I want to sum. They are named something like this: "ctl00$ContentPlaceHolder1$DataList1$ctl03$DataList2$ctl03$txtSumB" The only thing i know for...
6
by: doncee | last post by:
Using a multi select list box to open several records in a pre - defined form. Most of the code that follows is taken from a posting by Alan Browne on his web site. The click routine is supposed...
6
by: kberry | last post by:
I am clearing Textboxes on a form... this is loop I have came up with but was wondering if it can be shorter or not as long... Can anyone help? Dim controlOnForm As Control 'Places a control...
1
by: Kevin | last post by:
ASP.NET 2.0 I have code that updates a database from a number of textboxes on a web form. I've had to hard coded references to my web form textboxes. I'd like to know how I can reference them...
2
by: ckerns | last post by:
I have a page with a bunch of drop down boxes. They are named: MonEquipAMWk1 MonEquipPMWk1 thru FriEquipAMWk1 FriEquipPMWk1
3
by: Shawn | last post by:
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.
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
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,...
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,...
1
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
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,...
0
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.