Wednesday, December 17, 2008

[vb6] Generic control handling procedure

Sometimes it is useful to perform a certain action or set a certain property for all the controls of a certain type on a form. Say you needed to disable all controls of a certain type:
    Private Sub DisableControls(Frm As Form, CtrlTypeName As String)
Dim nCount As Integer
For nCount = 0 To Frm.Controls.Count - 1
If TypeName(Frm.Controls(nCount)) = CtrlTypeName Then
Frm.Controls(nCount).Enabled = False
End If
Next
End Sub
Of course, the actual modification(s) done to the controls inside the loop can be anything, as long as the methods and properties are defined for the type of control that you pass. Use of the function would be something like: DisableControls Me, "TextBox"
That would disable all TextBoxes on the form.

No comments:

Post a Comment