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:
  1. For Each c As Control In Me.Controls
  2.  
  3.             If TypeOf (c) Is TextBox Then
  4.                 Dim t As TextBox = CType(c, TextBox)
  5.                 If t.Name.StartsWith("TextBox") Then
  6.                     AddHandler t.Click, AddressOf ControlArray_Click
  7.                     AddHandler t.KeyDown, AddressOf ControlArray_KeyDown
  8.                 End If
  9.             End If
  10.  
  11.         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:
  1. For Each c As Control In Me.Controls
  2.             If TypeOf (c) Is TextBox Then
  3.                 Dim t As TextBox = CType(c, TextBox)
  4.                 If t.Name.StartsWith("TextBox") Then
  5.                     AddHandler t.Click, AddressOf ControlArray_Click
  6.                     AddHandler t.KeyDown, AddressOf ControlArray_KeyDown
  7.                 End If
  8.             ElseIf c.Controls.Count > 0 Then
  9.                 For Each childC In c.Controls
  10.                     If TypeOf (childC) Is TextBox Then
  11.                         Dim t As TextBox = CType(childC, TextBox)
  12.                         If t.Name.StartsWith("TextBox") Then
  13.                             AddHandler t.Click, AddressOf ControlArray_Click
  14.                             AddHandler t.KeyDown, AddressOf ControlArray_KeyDown
  15.                         End If
  16.                     End If
  17.                 Next
  18.             End If
  19.         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.