473,568 Members | 2,795 Online
Bytes | Software Development & Data Engineering Community
+ 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 2179
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 InvalidCastExce ption 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.co m> wrote in message
news:ee******** ******@tk2msftn gp13.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 StripFromTextbo x(parent as Control, searchFor as string,
replaceWith as string)
for each c as control in parent
if c.HasControls then
StripFromTextbo x(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.co m> wrote in message
news:ee******** ******@tk2msftn gp13.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.co m> wrote in message
news:ee******** ******@tk2msftn gp13.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
6249
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="" value="<?= $row_plans->planid ?>">
3
5192
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 I want to access the submit button of the visible form, which is usually the last one, how is that done? Currently, I look for type=submit using...
4
18811
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 would be more compact/efficient to use a loop. However, I haven't found anything that describes how to reference the textboxes sequentially ......
2
5173
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 sure is that "txtSumB" is somewhere in the name. The page can contain anything from 1 to 50 textboxes. Can someone please help me with a script which...
6
1949
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 to loop thru all of the reports, or in this case records, selected in the list box & display them for previewing or editing. In my situation it only...
6
1563
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 on the form Dim controlOnTab As Control 'Places a control on the tab Dim controlTabPage As Control 'Places a control on the tab page Dim...
1
1903
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 via a for next loop. For example, each time I give my command parameters a value I hard coded the textbox.text reference. ...
2
5059
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
1903
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
7660
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...
0
7962
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6275
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...
1
5498
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...
0
5217
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3651
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...
0
3631
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2101
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
0
932
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.