for each...in...

  • Thread starter Thread starter koffietje
  • Start date Start date
K

koffietje

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?
 
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
 
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 said:
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...
 
Hi Koffietje,

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

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

If there are any TextBoxes inside other Controls then a recursive method
is required: An example is shown below. Form is a Control so it starts off
with 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)
 
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

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




.
 
Hi, Cor, all the information I needed ! thanks
-----Original Message-----
Hi Java,

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


.
 
Hi Fergus,
Great information, thanks a lot!

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

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

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

If there are any TextBoxes inside other Controls then a recursive method
is required: An example is shown below. Form is a Control so it starts off
with 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
 
Back
Top