473,396 Members | 1,765 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,396 software developers and data experts.

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 5333
Not all the controls in this.Controls are TextBox controls. When you
iterate through them, you get an InvalidCastException 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*****@interaktif.gen.tr> wrote in message
news:OQ**************@TK2MSFTNGP11.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 InvalidCastException 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 InvalidCastException 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.com> 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 InvalidCastException 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*****@interaktif.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
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
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...
4
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...
1
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...
2
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...
9
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...
3
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...
5
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...
0
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 ...
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
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
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...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...

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.