Results 1 to 5 of 5

Thread: How to check a control's type?

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2000
    Location
    Texas
    Posts
    56

    Cool

    oki dokey, one more question...

    I've got a form with 200+ controls (don't ask .) I've created a class to loop through all the controls on the form and save their values to an .ini file. I'm only interested in saving textboxes, combo lists, etc. - not every control.

    How should I go about determining what type of control a control is?

    Thanks a million!

  2. #2
    Guest
    Code:
    Private Sub Command1_Click()
    
        Dim MyControl As Control
        Dim iList As Integer
        For Each MyControl In Me.Controls
            If TypeOf MyControl Is TextBox Then 
                'save code
                Open "C:\MyFile.txt" For Append As #1
                    Print #1, MyControl.Text
                Close #1
            End If
            If TypeOf MyControl Is ComboBox Then  
                Open "C:\MyFile.txt" For Append As #1
                    For iList = 0 To MyControl.ListCount - 1
                        Print #1, MyControl.List(iList)
                    Next iList
                Close #1
            End If
        Next
    
    End Sub



    Small mistake, thanks for correcting me Megatron .

  3. #3
    Guest
    Try this:
    Code:
    For Each Ctrl In Me.Controls
        If TypeName(Ctrl) = "TextBox" Then
            'Do something
        End If
    Next
    Hope this helps.

  4. #4
    Guest
    Oooh... I never get to post first.

  5. #5
    Guest
    Matthew Gates: Your line for saving the ComboBox reads For iList = 0 To Combo1.ListCount - 1; It should be For iList = 0 To MyControl.ListCount - 1

    Freefly21:
    Since ComboBoxes and Listboxes are the same (with respect to their list properties), you can save both of them with the same code.
    Code:
        Dim ctl As Control
    
        For Each ctl In Me.Controls
            If TypeOf ctl Is TextBox Then
                Open "C:\MyFile.txt" For Append As #1
                Print #1, ctl.Text
                Close #1
            End If
            If (TypeOf ctl Is ComboBox) Or (TypeOf ctl Is ComboBox) Then
                Open "C:\MyFile.txt" For Append As #1
                    For i = 0 To ctl.ListCount - 1
                        Print #1, ctl.List(i)
                    Next i
                Close #1
            End If
        Next ctl

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width