Clearing collections of TextBoxes 
Author Message
 Clearing collections of TextBoxes

I have a form with several GroupBoxes, each of which contains several
TextBoxes. I want the latter to be cleared of contents at the click of a
button. The following code generates an Invalid Cast exception, and I don't
understand why... Any help is appreciated!

foreach (GroupBox g in this.Controls)
{
 foreach (TextBox t in g.Controls)
 {
  t.Text = "";
  t.BackColor = System.Drawing.Color.White;
 }

Quote:
}



Fri, 31 Dec 2004 01:08:23 GMT  
 Clearing collections of TextBoxes
Hi,

You must look if it Control is TextBox. Note: there are other con trols too
!!!

foreach(Control ctrl in Controls){
    if(ctrl  is TextBox ){
        TextBox  t =  (TextBox  )ctrl;

        t.Text = "";
       t.BackColor = System.Drawing.Color.White;

    }

Quote:
}

same goes to GroupBox too.

Quote:

> I have a form with several GroupBoxes, each of which contains several
> TextBoxes. I want the latter to be cleared of contents at the click of a
> button. The following code generates an Invalid Cast exception, and I
don't
> understand why... Any help is appreciated!

> foreach (GroupBox g in this.Controls)
> {
>  foreach (TextBox t in g.Controls)
>  {
>   t.Text = "";
>   t.BackColor = System.Drawing.Color.White;
>  }
> }



Fri, 31 Dec 2004 02:22:21 GMT  
 Clearing collections of TextBoxes
There is a control in this.controls that cannot be cast to a groupbox or
There appears to be a control in one of your groupboxes that is not a
textbox (doesnt have a .text or a .backgrounf property).
Setup a try catch loop and ignore them is a quick solution..
 foreach (GroupBox g in this.Controls)
 {
  foreach (TextBox t in g.Controls)
  {

   t.Text = "";
   t.BackColor = System.Drawing.Color.White;
  }
 }

 Hop this helps
Craig Ison

Quote:

> I have a form with several GroupBoxes, each of which contains several
> TextBoxes. I want the latter to be cleared of contents at the click of a
> button. The following code generates an Invalid Cast exception, and I
don't
> understand why... Any help is appreciated!

> foreach (GroupBox g in this.Controls)
> {
>  foreach (TextBox t in g.Controls)
>  {
>   t.Text = "";
>   t.BackColor = System.Drawing.Color.White;
>  }
> }



Fri, 31 Dec 2004 01:33:08 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. TextBox.Clear and IP address control

2. How to clear all TextBoxes in a WebForm

3. clearing a textbox

4. A collection inside a collection inside a collection...

5. Changing textbox.text for bound textbox doesn't update database

6. checking if textbox.text is integer value (or only allow integers in textbox)

7. Web form textbox Vs Windows form textbox.....

8. Populating collection Items within the collection

9. collection inside of a collection

10. Collections of collections

 

 
Powered by phpBB® Forum Software