Click to See Complete Forum and Search --> : knowing the Control Type dynamically.


Parthasarathi
September 29th, 1999, 02:33 PM
Hi All,
Recently I was programming in VB and I came across a requirement. I wanted to know programatically what type each control is in a screen . Suppose a form has a command button, a checkbox, a textbox and a listbox. In my program code ( say open window event of the form) I want to know what type of control is Controls(3). I adressed the problem in an indirect way by naming the controls in a predefined format (e.g all textbox controls were named as txt...). In the program I checked controls(i).name . Can anybody give me a more general solution to this problem. If it is not possible in VB can VC++ handle this situation more gracefully ?

Thanking you in advance.

Parthasarathi.

czimmerman
September 29th, 1999, 03:55 PM
Try TypeName(Controls(3)). The TypeName function returns the Object Type when passed an object.

Chris Eastwood
September 29th, 1999, 04:00 PM
Hi

You can check the type of each control on your form by using either the TypeName function that returns a string of the control's type, or the 'TypeOf .. Is ..' command, eg:


Dim ctl as Control

for Each ctl In me.Controls
Debug.print TypeName$(ctl)
If TypeOf ctl is TextBox then
' DO something
ElseIf TypeOf ctl is CommandButton then
' Do something else
'
' etc.
End If
next





Chris Eastwood

CodeGuru - the website for developers
http://codeguru.developer.com/vb