Browse DevX
Sign up for e-mail newsletters from DevX

Tip of the Day
Language: Visual Basic
Expertise: Beginner
Aug 8, 1997



Building the Right Environment to Support AI, Machine Learning and Deep Learning

Identifying a Generic Control at Run Time

When a procedure can operate on multiple types of controls, you can use VB's TypeOf function to determine a control type at run time:

Function myFunc(ctl as Control)

        ' This code works in both VB3 & VB4
        If TypeOf ctl Is TextBox Then
                ' Code for text boxes here…
        ElseIf TypeOf ctl Is CommandButton _
                ' Code for command buttons here…
        End if

End Function

VB4 adds the new TypeName function, which allows you to test the control's type once, then branch based on the result:

Function myFunc(ctl As Control)

        Dim sCtlType as String

        ' TypeName is new to VB4
        sCtlType = TypeName(ctl)
        Select Case sCtlType
                Case "TextBox"
                        ' Code for text boxes here…
                Case "CommandButton"
                        ' Code for command buttons 
        End Select
End Function

To learn the type (or class) name of a given control, highlight it at design time and look at VB's Properties window. The type name appears to the right of the control's name in the combo box at the top of the window.

Senthil Shanmugham
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



Thanks for your registration, follow us on our social networks to keep up-to-date