By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,480 Members | 1,214 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,480 IT Pros & Developers. It's quick & easy.

for each...in...

P: n/a
How can I test all my textboxes in my form wether they
are empty are not by using a for each... in .. structure
in VB.Net?

Nov 20 '05 #1
Share this Question
Share on Google+
12 Replies


P: n/a
Cor
Hi Java,
How can I test all my textboxes in my form wether they
are empty are not by using a for each... in .. structure
in VB.Net?


When all textboxes are on the main form (windowform)
Rough written so don't look at typo's
\\\
dim ctr as control
for each ctr in me.controls
if typeof ctr is textbox then
if ctr.text <>"" then
'do something
end if
end if
next
///
If it is a webform "text" is not in the control and you have to cast it
first, but that is a problem itself, but I thought I have somewhere saved a
solution, so tell that than again.

I hope this helps a little bit,

Cor
Nov 20 '05 #2

P: n/a
Hi,

Dim ctrl As Control

For Each ctrl In Me.Controls

If TypeOf (ctrl) Is TextBox Then

Dim tb As TextBox = ctrl

If tb.Text = "" Then

tb.Text = "Empty"

End If

End If

Next

Ken

---------------------------------------

"koffietje" <an*******@discussions.microsoft.com> wrote in message
news:07****************************@phx.gbl...
How can I test all my textboxes in my form wether they
are empty are not by using a for each... in .. structure
in VB.Net?

Nov 20 '05 #3

P: n/a
* "Ken Tucker [MVP]" <vb***@bellsouth.net> scripsit:
If TypeOf (ctrl) Is TextBox Then


Just FYI: You don't need the "(", ")" around 'ctrl'.

;-)

--
Herfried K. Wagner
MVP VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #4

P: n/a
* "koffietje" <an*******@discussions.microsoft.com> scripsit:
How can I test all my textboxes in my form wether they
are empty are not by using a for each... in .. structure
in VB.Net?


<http://www.mvps.org/dotnet/dotnet/samples/controls/downloads/EnumerateControls.zip>

--
Herfried K. Wagner
MVP VB Classic, VB.NET
<http://www.mvps.org/dotnet>
Nov 20 '05 #5

P: n/a
"koffietje" <an*******@discussions.microsoft.com> schrieb
How can I test all my textboxes in my form wether they
are empty are not by using a for each... in .. structure
in VB.Net?


You do need a loop. Either for each..next, for..next, do while...
--
Armin

Nov 20 '05 #6

P: n/a
Hi Ken, exactly what I was looking for! Thanks!

-----Original Message-----
Hi,

Dim ctrl As Control

For Each ctrl In Me.Controls

If TypeOf (ctrl) Is TextBox Then

Dim tb As TextBox = ctrl

If tb.Text = "" Then

tb.Text = "Empty"

End If

End If

Next

Ken

---------------------------------------

"koffietje" <an*******@discussions.microsoft.com> wrote in messagenews:07****************************@phx.gbl...
How can I test all my textboxes in my form wether they
are empty are not by using a for each... in .. structure in VB.Net?

.

Nov 20 '05 #7

P: n/a
Hi, Cor, all the information I needed ! thanks
-----Original Message-----
Hi Java,
How can I test all my textboxes in my form wether they
are empty are not by using a for each... in .. structure in VB.Net?
When all textboxes are on the main form (windowform)
Rough written so don't look at typo's
\\\
dim ctr as control
for each ctr in me.controls
if typeof ctr is textbox then
if ctr.text <>"" then
'do something
end if
end if
next
///
If it is a webform "text" is not in the control and you

have to cast itfirst, but that is a problem itself, but I thought I have somewhere saved asolution, so tell that than again.

I hope this helps a little bit,

Cor
.

Nov 20 '05 #8

P: n/a
Hi Fergus,
Great information, thanks a lot!

-----Original Message-----
Hi Koffietje,

You need one of two methods depending on whether your TextBoxes aredirectly on the Form or whether some are held within other Controls such asPanels.

If all the TextBoxes are directly on the Form then the methods shown byCor and Ken will be fine.

If there are any TextBoxes inside other Controls then a recursive methodis required: An example is shown below. Form is a Control so it starts offwith the Form passing itself (Me).

In the Form:
CheckTheTextBoxes (Me)
Sub CheckTheTextBoxes (oControl As Control)
Dim oChildControl As Control
For Each oChildControl in oControl.Controls
If TypeOf (oChildControl) Is TextBox _
AndAlso oChildControl.Text = "" Then 'or use <> 'Take some action.
End If
CheckTheTextBoxes (oChildControl) 'Check Panels, etc. Next
End Sub

You can use:
Dim oTextBox As TextBox = oChildControl
in the 'Take some action' section if you actually need to do anything'textboxy'.

Regards,
Fergus

If you use Option Strict On, you'll need
Dim oTextBox As TextBox = DirectCast (oChildControl, TextBox)

.

Nov 20 '05 #9

P: n/a
Thanks a lot !!
-----Original Message-----
* "koffietje" <an*******@discussions.microsoft.com> scripsit:
How can I test all my textboxes in my form wether they
are empty are not by using a for each... in .. structure in VB.Net?


<http://www.mvps.org/dotnet/dotnet/sa...controls/downl

oads/EnumerateControls.zip>
--
Herfried K. Wagner
MVP VB Classic, VB.NET
<http://www.mvps.org/dotnet>
.

Nov 20 '05 #10

P: n/a
* "koffietje" <an*******@discussions.microsoft.com> scripsit:
Hi Ken, exactly what I was looking for! Thanks!


Notice that this code will not enumerate nested controls.

--
Herfried K. Wagner
MVP VB Classic, VB.NET
<http://www.mvps.org/dotnet>

Improve your quoting style:
<http://learn.to/quote>
<http://www.plig.net/nnq/nquote.html>
Nov 20 '05 #11

P: n/a
Cor
Hi Herfried,

Notice that this code will not enumerate nested controls.


But the answer from Fergus does and Java was answering that also

Cor
Nov 20 '05 #12

P: n/a
* "Cor" <no*@non.com> scripsit:
Notice that this code will not enumerate nested controls.


But the answer from Fergus does and Java was answering that also


I know that...

--
Herfried K. Wagner
MVP VB Classic, VB.NET
<http://www.mvps.org/dotnet>

Improve your quoting style:
<http://learn.to/quote>
<http://www.plig.net/nnq/nquote.html>
Nov 20 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.