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

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 10244
<"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
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.