TypeOf question

M

Muckeypuck

hello, i would like to write a function that takes a webcontrol type as a
parameter and returns an array of controls based on the type

some thing like:

GetAllControls(typeof(system.web.ui.webcontrols.textbox)) 'returns textboxes
GetAllControls(typeof(system.web.ui.webcontrols.dropdownlist)) 'returns
dropdowns
GetAllControls(typeof(system.web.ui.webcontrols)) 'returns everything

however i can not find the proper way to write the signature

Public sub GetAllControls(Byref tType as ??????????)

thanks
 
K

Karl Seguin [MVP]

Something like:

Public Function GetAllControlsOfType(ByVal type As Type, ByVal parent As
Control) As ArrayList
Dim arr As New ArrayList()
For Each c As Control In parent.Controls
If (c.HasControls) Then
arr.AddRange(GetAllControlsOfType(type, c))
End If
If (c.GetType().IsAssignableFrom(type)) Then
arr.Add(c)
End If
Next
Return arr
End Function

should work..

You can call it via:

GetAllControlsOfType(new TextBox().GetType(), Page)

If you don't like the "new TextBox().GetType()", you cna get the type via:

Type.GetType("System.Web.UI.WebControls.TextBox, System.Web", true, true)

but you'll need to specify the full assembly name (token, culture,
version..) since System.Web sits in the GAC..

Karl
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top