Control eunumeration

A

axapta

Hi,
I need to check whether at least ONE control has been filled in prior to the
user hitting a seacrh button. I have a series of textboxes and a couple of
drop-down list boxes. I've seen in MS Access (yes I know old skool) where
you can do a loop around the different types of controls by their controlid
and check whether they are blank or not. Is there a similar sort of thing
for .net.

Any hints, most welcomed.
 
T

tommaso.gastaldi

Hi,
I need to check whether at least ONE control has been filled in prior to the
user hitting a seacrh button.  I have a series of textboxes and a coupleof
drop-down list boxes.  I've seen in MS Access (yes I know old skool) where
you can do a loop around the different types of controls by their controlid
and check whether they are blank or not.  Is there a similar sort of thing
for .net.

Any hints, most welcomed.


You can loop on controls, forms or whatever. For instance:

For Each c As Control In Me.Controls

If TypeOf c Is TextBox Then
' ...
End If

Next c


Depending on the case, you may also need recursion.


Tommaso
 
S

SurturZ

Tommaso is correct but note that if a textbox is inside a container (e.g.
inside a GroupBox, Panel, TabControl etc) then it won't show up - you need
to go through each container on the form.

(This is what I think Tommaso meant when he mentioned recursion).
 
P

Phill W.

axapta said:
I need to check whether at least ONE control has been filled in prior to
the user hitting a seacrh button. I have a series of textboxes and a
couple of drop-down list boxes. I've seen in MS Access (yes I know old
skool) where you can do a loop around the different types of controls by
their controlid and check whether they are blank or not. Is there a
similar sort of thing for .net.

Control Arrays.

Yes, I really did mean to say that.

Use an Array (or similar "list" construct) containing the Controls, in
which you're interested, something like:

Private m_EditControls as Control() = {}

Sub Form_Load( ... ) Handles MyBase.Load

' Put all the controls you care about into the array
m_EditControls = New Control() = _
{ Me.TextBox1 _
, Me.TextBox2 _
, Me.Combobox3 _
}
End Sub

Then, at any point thereafter, you can test for any one of them being
non-blank using something like :

Function AnyValueEntered() as Boolean
For Each ctl as Control in m_EditControls
If ctl.Text <> String.Empty Then
Return True
End If
Return False
Next
End Function

If you move into other controls where you have to check more than just
the Text, you'll have to start down-casting to the correct type to get
the right properties, as in:

For Each ctl as Control in m_EditControls
if Typeof ctl Is TextBox Then
if ( DirectCast( ctl, TextBox ).Text <> String.Empty Then
Return True
End If
End If

HTH,
Phill W.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top