473,474 Members | 1,312 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to loop through all textbox on the form and clear its values

I'm trying to clear all textbox on the form.

foreach (Control c in this.Controls)
{
//if (c.GetType() == typeof(TextBox))
if (c is TextBox)
{
// Found it
c.Text = "";
MessageBox.Show(c.Name.ToString());
}
}

However, this code can not effect with TextBox that inside groupbox in my form.
What is wrong?
Nov 16 '05 #1
6 10252
<"Thonglao Rud" <>> wrote:
I'm trying to clear all textbox on the form.

foreach (Control c in this.Controls)
{
//if (c.GetType() == typeof(TextBox))
if (c is TextBox)
{
// Found it
c.Text = "";
MessageBox.Show(c.Name.ToString());
}
}

However, this code can not effect with TextBox that inside groupbox in my form.
What is wrong?


Well, the TextBox in a GroupBox isn't directly inside your form - so
you should call the same method recursively for any container control.
It's probably easiest to actually call it recursively for *all*
controls, seeing as the Controls property is inherited from Control
itself, but that may well not be the most efficient way.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
I think you can use GetStyle on the control to determine if it's a container
(check for the style ContainerControl). This might be a bit more efficient
than iterating through the Controls collection of every control.

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP***********************@msnews.microsoft.co m...
<"Thonglao Rud" <>> wrote:
I'm trying to clear all textbox on the form.

foreach (Control c in this.Controls)
{
//if (c.GetType() == typeof(TextBox))
if (c is TextBox)
{
// Found it
c.Text = "";
MessageBox.Show(c.Name.ToString());
}
}

However, this code can not effect with TextBox that inside groupbox in my form. What is wrong?


Well, the TextBox in a GroupBox isn't directly inside your form - so
you should call the same method recursively for any container control.
It's probably easiest to actually call it recursively for *all*
controls, seeing as the Controls property is inherited from Control
itself, but that may well not be the most efficient way.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #3
If it's not a container control, the Controls count would be 0 and so, there
won't be any efficiency lost/gained.

-vJ

"John Wood" <sp**@isannoying.com> wrote in message
news:ed**************@TK2MSFTNGP12.phx.gbl...
I think you can use GetStyle on the control to determine if it's a
container
(check for the style ContainerControl). This might be a bit more efficient
than iterating through the Controls collection of every control.

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP***********************@msnews.microsoft.co m...
<"Thonglao Rud" <>> wrote:
> I'm trying to clear all textbox on the form.
>
> foreach (Control c in this.Controls)
> {
> //if (c.GetType() == typeof(TextBox))
> if (c is TextBox)
> {
> // Found it
> c.Text = "";
> MessageBox.Show(c.Name.ToString());
> }
> }
>
> However, this code can not effect with TextBox that inside groupbox in my form. > What is wrong?


Well, the TextBox in a GroupBox isn't directly inside your form - so
you should call the same method recursively for any container control.
It's probably easiest to actually call it recursively for *all*
controls, seeing as the Controls property is inherited from Control
itself, but that may well not be the most efficient way.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too


Nov 16 '05 #4
Other than creating/disposing the enumerator instance unnecessarily for each
control... which is why I suggested using GetStyle to see if it's a
container. :)

"Vijaye Raji" <no*************@hotmail.com> wrote in message
news:el**************@TK2MSFTNGP11.phx.gbl...
If it's not a container control, the Controls count would be 0 and so, there won't be any efficiency lost/gained.

-vJ

"John Wood" <sp**@isannoying.com> wrote in message
news:ed**************@TK2MSFTNGP12.phx.gbl...
I think you can use GetStyle on the control to determine if it's a
container
(check for the style ContainerControl). This might be a bit more efficient than iterating through the Controls collection of every control.

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP***********************@msnews.microsoft.co m...
<"Thonglao Rud" <>> wrote:
> I'm trying to clear all textbox on the form.
>
> foreach (Control c in this.Controls)
> {
> //if (c.GetType() == typeof(TextBox))
> if (c is TextBox)
> {
> // Found it
> c.Text = "";
> MessageBox.Show(c.Name.ToString());
> }
> }
>
> However, this code can not effect with TextBox that inside groupbox
in my form.
> What is wrong?

Well, the TextBox in a GroupBox isn't directly inside your form - so
you should call the same method recursively for any container control.
It's probably easiest to actually call it recursively for *all*
controls, seeing as the Controls property is inherited from Control
itself, but that may well not be the most efficient way.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too



Nov 16 '05 #5
Thank you all! I really appreciate it.
Here my modified code:

foreach (Control c in this.Controls)
{
if (c is TextBox)
{
c.Text = "";
}
if ((c.GetType().Name == "GroupBox"))
{
foreach (Control cc in c.Controls)
if (cc is TextBox)
{
cc.Text = "";
}
}
}

Ugly code but works for me :)
Thanks again.
Nov 16 '05 #6
Ah.. true! Good point

"John Wood" <sp**@isannoying.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Other than creating/disposing the enumerator instance unnecessarily for
each
control... which is why I suggested using GetStyle to see if it's a
container. :)

"Vijaye Raji" <no*************@hotmail.com> wrote in message
news:el**************@TK2MSFTNGP11.phx.gbl...
If it's not a container control, the Controls count would be 0 and so,

there
won't be any efficiency lost/gained.

-vJ

"John Wood" <sp**@isannoying.com> wrote in message
news:ed**************@TK2MSFTNGP12.phx.gbl...
>I think you can use GetStyle on the control to determine if it's a
>container
> (check for the style ContainerControl). This might be a bit more efficient > than iterating through the Controls collection of every control.
>
> "Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
> news:MP***********************@msnews.microsoft.co m...
>> <"Thonglao Rud" <>> wrote:
>> > I'm trying to clear all textbox on the form.
>> >
>> > foreach (Control c in this.Controls)
>> > {
>> > //if (c.GetType() == typeof(TextBox))
>> > if (c is TextBox)
>> > {
>> > // Found it
>> > c.Text = "";
>> > MessageBox.Show(c.Name.ToString());
>> > }
>> > }
>> >
>> > However, this code can not effect with TextBox that inside groupbox in > my form.
>> > What is wrong?
>>
>> Well, the TextBox in a GroupBox isn't directly inside your form - so
>> you should call the same method recursively for any container control.
>> It's probably easiest to actually call it recursively for *all*
>> controls, seeing as the Controls property is inherited from Control
>> itself, but that may well not be the most efficient way.
>>
>> --
>> Jon Skeet - <sk***@pobox.com>
>> http://www.pobox.com/~skeet
>> If replying to the group, please do not mail me too
>
>



Nov 16 '05 #7

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

Similar topics

3
by: Joe | last post by:
I'm wondering how to loop through controls in VB.NET. I have the code from VB6 ok, but I can't figure out how to do it correctly in .NET. This is an example from my VB6 code that loops through...
5
by: JollyK | last post by:
Hello all, I have always been having this issue and wondering what the solution is. When I set the enableviewstate property to false for a textbox, the textbox always retains its value after a...
4
by: Mark Broadbent | last post by:
This one has got me absolutely stumped. At work I have created a simple web form that gets data from a sql backend and puts this to a dataset. I have an edit button that when clicked will set...
6
by: Paul D. Fox | last post by:
I want to be able to loop through all the TextBoxes on a page and clear their values. How can I write a function to do that?
6
by: david | last post by:
I try to use "for" loop to retrieve and assign values in web form. The code is in the following. But it can not be compiled. What I want to do is: txtQ1.Text =...
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...
16
by: Jen | last post by:
Hi. I have this problem that I think should be easy but have been struggling with this for days. I have a list based on a recordset from a database. This list consists of records meeting a certain...
1
by: colleen1980 | last post by:
Hi: Can any one please tell me that how to i pass the two textbox values in the new page. If i use the form action in the popup window page then the new page is open in the same popup window as i...
2
by: rn5a | last post by:
In a ASP applicatiuon, the FOrm has a textbox & a select list where the admin can select multiple options. Basically the admin has to enter the name of a new coach in the textbox & select the...
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
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,...
1
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...
0
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...
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 ...
0
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...

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.