473,659 Members | 2,656 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Clearing textboxes..

hi there, i am writing a program with LOTS of textboxes in it.. my problem
is when i try to clean the textboxes with foreach it gives error.. heres how
my code looks like:

private void ClearText ()

{
foreach (textbox t in this.controls)
{
t.text = ""
}
}

but it doesnt work! it was working quite good on visual basic.net but this
code seems not working with c#?!
Nov 16 '05 #1
4 5353
Not all the controls in this.Controls are TextBox controls. When you
iterate through them, you get an InvalidCastExce ption because a non-TextBox
control is being assigned to t, a TextBox variable.

foreach (Control c in this.Controls)
{
if (c is TextBox)
{
TextBox t = (TextBox) c;
t.Text = "";
}
}

"Serdar C." <se*****@intera ktif.gen.tr> wrote in message
news:OQ******** ******@TK2MSFTN GP11.phx.gbl...
hi there, i am writing a program with LOTS of textboxes in it.. my problem
is when i try to clean the textboxes with foreach it gives error.. heres how my code looks like:

private void ClearText ()

{
foreach (textbox t in this.controls)
{
t.text = ""
}
}

but it doesnt work! it was working quite good on visual basic.net but this
code seems not working with c#?!

Nov 16 '05 #2
Scott English wrote:

Not all the controls in this.Controls are TextBox controls. When you
iterate through them, you get an InvalidCastExce ption because a non-TextBox
control is being assigned to t, a TextBox variable.

foreach (Control c in this.Controls)
{
if (c is TextBox)
{
TextBox t = (TextBox) c;
t.Text = "";
}
}


Couldn't you do:

foreach (TextBox t in this.Controls)

?
Nov 16 '05 #3
No. The foreach command will iterate through ALL the objects in the
Controls collection and attempt to cast them to a TextBox. If any of them
aren't a TextBox, then an InvalidCastExce ption will occur. Declaring the
type of variable as TextBox doesn't filter the contents of Controls
(although that would be really nice).

"Julie" <ju***@nospam.c om> wrote in message
news:40******** *******@nospam. com...
Scott English wrote:

Not all the controls in this.Controls are TextBox controls. When you
iterate through them, you get an InvalidCastExce ption because a non-TextBox control is being assigned to t, a TextBox variable.

foreach (Control c in this.Controls)
{
if (c is TextBox)
{
TextBox t = (TextBox) c;
t.Text = "";
}
}


Couldn't you do:

foreach (TextBox t in this.Controls)

?

Nov 16 '05 #4
In addition to the problem Scott E pointed out, if you have your
textboxes inside of other controls you may need a little more code to
get to all of the textbox controls (see
http://www.odetocode.com/Code/71.aspx).

--
Scott
On Fri, 2 Apr 2004 20:44:34 +0300, "Serdar C."
<se*****@intera ktif.gen.tr> wrote:
hi there, i am writing a program with LOTS of textboxes in it.. my problem
is when i try to clean the textboxes with foreach it gives error.. heres how
my code looks like:

private void ClearText ()

{
foreach (textbox t in this.controls)
{
t.text = ""
}
}

but it doesnt work! it was working quite good on visual basic.net but this
code seems not working with c#?!


Nov 16 '05 #5

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

Similar topics

2
1853
by: Savvas | last post by:
Hi everybody, I have a lot of textboxes on my form and a "Clear" button. Is there a way with a for loop or something to clear the textboxes, instead of writing textboxName.clear? Thanks a lot
4
302
by: Serdar C. | last post by:
hi there, i am writing a program with LOTS of textboxes in it.. my problem is when i try to clean the textboxes with foreach it gives error.. heres how my code looks like: private void ClearText () { foreach (textbox t in this.controls) { t.text = ""
4
4422
by: Ali | last post by:
I used to clear my page's control in Visual Studio 2003 using code like this: Dim c As Control For Each c In Page.Controls(1).Controls If TypeOf c Is TextBox Then CType(c, TextBox).Text = Nothing End If If TypeOf c Is DropDownList Then CType(c, DropDownList).SelectedIndex = 0 End If Next
1
1235
by: Alex | last post by:
Hi I'm a beginner in ASP.NET. I'm using 'AddContact.aspx' form to add a contact to DB, 'litStatus' literal to show the result and Server.Transfer to clear inputs in the form. The problem is i don't see the status message. The example is from a book about ASP.NET. Am i doing something wrong? If the code is wrong how can i clear the textboxes and show the status? Thanks. try
2
4930
by: Eytch | last post by:
Hey: I am a rookie developer so please bear with me. I want to develop a small application for my office but I am having trouble clearing a page that has required fields. If I make a textbox required and then trigger it by not entering in any data the page seems to be totally locked. (Which makes sense because the Textbox is required) My question is: How can I code a button that overrides the required fields
9
2185
by: Michael.Suarez | last post by:
foreach (TextBox tb in this.Controls) tb.Dispose(); What this does is clears every other textbox on the form. Suppose there are 10 textboxes... this will dispose the 1st one, then i guess whatever list of textboxes it's maintaining gets shifted, so the 2nd is now the 1st in the list, so when it goes to the next one, it is destroying the 3rd textbox, leaving the 2nd one safe...
3
1663
by: cmrhema | last post by:
This is reference to Vandana Sridhar's refreshing the form http://www.thescripts.com/forum/thread619976.html The code displayed below did work. Dim Ctrl As Control For Each Ctrl In Me.Controls If TypeOf Ctrl Is TextBox Then Ctrl.Text = "" Next But I have three frames inside the form
5
10796
jgroos
by: jgroos | last post by:
Here is the situation: I have 25 textboxes that are all handled by the same TextChanged event handler. My problem is, when I click my "Clear" button on the form, it clears all the textboxes and then fires my TextChanged event handler for each of the 25 textboxes, because their text is being "changed". I know this is how it is supposed to work, but it makes clearing my form take WAY too long! :) Is there any way I can clear the event queue...
0
1375
by: asumal | last post by:
Hello I am new to ASP.NET. I am using CreateUserWizard control to register & create a user. I am using Membership API. I have a Cancel button on the CreateUserWizard step 2 alongwith Create User button & Previous button. Actually, i have added some more textboxes on the default control for taking user's personal information. The problem is that when i call the below mentioned code all textboxes for user's personal information gets cleared...
0
8427
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...
1
8525
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
8627
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7356
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
6179
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
4175
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
2750
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
1975
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1737
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.