473,714 Members | 2,605 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 14864
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.Lengt h > 0)
{
isEmpty = false;
}
}
}

Jan 12 '06 #2
"Bruce Wood" <br*******@cana da.com> wrote in news:1137027138 .003491.127660
@g43g2000cwa.go oglegroups.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.Lengt h > 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(Tex tBox tb)
{
if (tb.Text.Length == 0)
{
...
}
}

public void CheckTextBoxes( ControlCollecti on ctrls)
{
if (ctrls == null) return;
foreach(Control c in ctrls)
{
if (c is TextBox) DoSomething((Te xtBox)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.c om 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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
13315
by: Tom Fitzgibbon | last post by:
Stupid question: How do I loop through many text boxes on a page and get values for each box and put into an array? For example document.CreateEvent.test1.value will not take an array value for the object name. Tried to figure out if associative arrays might help but failed miserably. Obviously I can name the text boxes anything, but can't get the values out.
4
18830
by: Kevin H | last post by:
Apologies in advance if this sounds slow-witted, but I didn't find it here. Need to populate some textboxes on a form. While I could hard code it (the number of options aren't that high), it would be more compact/efficient to use a loop. However, I haven't found anything that describes how to reference the textboxes sequentially ... evaluating the hard part of the textbox name, with the number of the loop iteration, to address the...
4
2189
by: VB Programmer | last post by:
How can I loop thru all textboxes on a form? I want to remove any single quotes in the data entry. I was thinking something like this: For Each c As Control In Me.Controls Dim t As TextBox = CType(c, TextBox) t.Text = t.Text.Replace("'", "") Next
6
3144
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?
2
3708
by: Steve | last post by:
One of the columns in my gridview is a blank textbox (I have added it as a template field). Users can enter a number in the textbox for a multiple of the gridview records. I have a submit button which is intended to use code behind to access these textbox values and eventually add them to a database. However, I can't seem to loop through the textboxes in the gridview successfully and store their values. Does anyone know the right...
6
1569
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 on the form Dim controlOnTab As Control 'Places a control on the tab Dim controlTabPage As Control 'Places a control on the tab page Dim controlGroupBox As Control 'Places a control on the group box
1
1911
by: Kevin | last post by:
ASP.NET 2.0 I have code that updates a database from a number of textboxes on a web form. I've had to hard coded references to my web form textboxes. I'd like to know how I can reference them via a for next loop. For example, each time I give my command parameters a value I hard coded the textbox.text reference. cmd.Parameters.AddWithValue("@Rank", Me.ddlQ1.Text)
7
5072
by: Sriku | last post by:
I have created ten textboxes and name them as "txt1,txt2,..txt10" (i.e just changing suffix by consecutive number). Now I have to validate these ten textboxes on Submit event (Say..No textbox should accept alphabet). I trying to use for loop to validate this i.e. trying to change the suffix of textbox name in each iteration. for(var i=1;i<=10;i++) { if(document.myform.txt+i+.value==0)
3
1177
by: =?Utf-8?B?aUhhdkFRdWVzdGlvbg==?= | last post by:
I actually need to do the below for 10 controls ddlInsuranceCompany1.SelectedItem.Text = Value; txtPolicy1.Text = value; SO i opted for a for loop My question is how do i add contol + increment ddlInsuranceCompany + integer
3
1908
by: Shawn | last post by:
I'm trying to go through a large number of logically named textboxes using a loop and cannot figure out how short of explicitly referencing every single one.
0
8798
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8706
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9311
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9171
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9013
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6632
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5946
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4463
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
2518
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.