im trying to clear all textboxes in a tabbed form, im using:
foreach (Control tbpage in this.tabControl1.TabPages)
{
do
foreach (Control txt in this.tbpage.Controls)
{
if (txt.GetType() == typeof(System.Windows.Forms.TextBox))
{
txt.Text = "";
}
}
while (this.tabControl1.TabCount > loop);
loop = loop + 1;
}
but it is not working!
can anyone help?
sorry if this is really basic but i am a newbie!
rainywoPosted Mar 3, 2008, 3:30 AM
????!
Phil LanePosted Feb 24, 2008, 1:08 PM
AlanPosted Feb 24, 2008, 10:09 AM
You can do the same sort of thing with CheckBoxes, Phil, but the reason why your code doesn't work is because the Control class doesn't have a Checked property. You therefore need to cast it to a CheckBox first as follows:
foreach (TabPage tbpage in this.tabControl1.TabPages)
{
foreach (Control c in tbpage.Controls)
{
if (c is CheckBox)
{
((CheckBox)c).Checked = false ;
}
}
}
Before, we didn't need the cast because the Control class does have a virtual Text property which the TextBox class overrides.
Phil LanePosted Feb 24, 2008, 8:18 AM
thats great, thanks.
can you do they same with check boxes (unckeck them) i have tried replacing
if (c is TextBox)
{
c.Text = "";
}
with
if (c is CheckBox)
{
c.Checked = false;
}
and it doesn;t work!!
AlanPosted Feb 24, 2008, 6:52 AM
Try this:
foreach (TabPage tbpage in this.tabControl1.TabPages)
{
foreach (Control c in tbpage.Controls)
{
if (c is TextBox)
{
c.Text = "";
}
}
}