|
-
Jul 15th, 2004, 06:26 AM
#17
Hyperactive Member
Originally posted by <ABX
What controls did you put on your form.... i did test it... just not with anthing other than textboxes on the form.
Here is a modified version that does work properly...
VB Code:
For Each c As Control In Me.Controls
If TypeOf (c) Is TextBox Then
Dim t As TextBox = CType(c, TextBox)
If t.Name.StartsWith("TextBox") Then
AddHandler t.Click, AddressOf ControlArray_Click
AddHandler t.KeyDown, AddressOf ControlArray_KeyDown
End If
End If
Next
One problem I see with most code examples for looping through the controls collection in this thread is that nowhere does anyone take into account the possibility that a control may be a container control for other controls eg. a panel or tab, the code in the examples provided would never get a reference to those controls that are contained in other controls.
This code will access those controls, you would of course need to add further levels of nesting for each additional level of nested controls. eg, panel contains a groupbox which contains several checkboxes or radiobuttons.
VB Code:
For Each c As Control In Me.Controls
If TypeOf (c) Is TextBox Then
Dim t As TextBox = CType(c, TextBox)
If t.Name.StartsWith("TextBox") Then
AddHandler t.Click, AddressOf ControlArray_Click
AddHandler t.KeyDown, AddressOf ControlArray_KeyDown
End If
ElseIf c.Controls.Count > 0 Then
For Each childC In c.Controls
If TypeOf (childC) Is TextBox Then
Dim t As TextBox = CType(childC, TextBox)
If t.Name.StartsWith("TextBox") Then
AddHandler t.Click, AddressOf ControlArray_Click
AddHandler t.KeyDown, AddressOf ControlArray_KeyDown
End If
End If
Next
End If
Next
Taxes,
Years ago I used control arrays in like VB3 or maybe 4, but I haven't had much need of them as of late, I never personally found them very useful, so your comment about not missing them is right on target.
Whadayamean it doesn't work....
It works fine on my machine!

Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|