471,319 Members | 1,551 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,319 software developers and data experts.

How to loop through textboxes?

I'd like to loop through all text boxes on a winform and check if they
are empty.

private bool emptyTextBoxes()
{
bool isEmpty = true;

foreach(Control ctl in Controls)
{
if (typeof(ctl) == TextBox) //error here
if(ctl.Text != "")
isEmpty = false;
}
return isEmpty
}

I get this error:
The type or namespace name 'ctl' could not be found (are you missing a
using directive or an assembly reference?)

ctl is a typeof Control and I want to make sure it is a TextBox. Why
is this wrong?

Thanks,
Brett

Jan 12 '06 #1
4 14714
Two things.

First of all, typeof() converts a _class name_ into a Type object, so
it's expecting a type name there, for example typeof(TextBox), not a
variable.

In order to get the type of the object referenced by a variable, call
the GetType() method:

ctl.GetType()

Second, there is a much better construct for what you're trying to do:

foreach(Control ctl in Controls)
{
TextBox box = ctl as TextBox;
if (box != null)
{
if (box.Text.Length > 0)
{
isEmpty = false;
}
}
}

Jan 12 '06 #2
"Bruce Wood" <br*******@canada.com> wrote in news:1137027138.003491.127660
@g43g2000cwa.googlegroups.com:
Second, there is a much better construct for what you're trying to do:

foreach(Control ctl in Controls)
{
TextBox box = ctl as TextBox;
if (box != null)
{
if (box.Text.Length > 0)
{
isEmpty = false;
}
}
}


I would also add that you might want to add some recursion to check child
controls... things like groupBoxes and Panels.

public void DoSomething(TextBox tb)
{
if (tb.Text.Length == 0)
{
...
}
}

public void CheckTextBoxes(ControlCollection ctrls)
{
if (ctrls == null) return;
foreach(Control c in ctrls)
{
if (c is TextBox) DoSomething((TextBox)c);
else CheckTextBoxes(c.Controls);
}
}

-mdb
Jan 12 '06 #3
Change:

if (typeof(ctl) == TextBox)

to

if (ctl.GetType() == typeof(TextBox))

and it should work fine.

Dave

Jan 12 '06 #4
u7***@hotmail.com wrote:
Change:

if (typeof(ctl) == TextBox)
to
if (ctl.GetType() == typeof(TextBox))

and it should work fine.


Well, it'll work subtly differently to using "as" or "is". The above
would return false if you had a control which was derived from TextBox.
Now, it could be that that's what's actually wanted - but it usually
isn't.

Using as/is is generally preferrable, IMO.

Jon

Jan 12 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Tom Fitzgibbon | last post: by
4 posts views Thread by Kevin H | last post: by
4 posts views Thread by VB Programmer | last post: by
6 posts views Thread by Paul D. Fox | last post: by
2 posts views Thread by Steve | last post: by
3 posts views Thread by =?Utf-8?B?aUhhdkFRdWVzdGlvbg==?= | last post: by
reply views Thread by rosydwin | last post: by

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.