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)
?