Bestimmen, von welchem Typ ein Control ist

RamonR

Erfahrenes Mitglied
Hallo Leute,

was ist die wichtigste Eigenschaft eines Programmierers ? Richtig, FAULHEIT ;)

Hier also mein Problem:

Ich will sämtliche Textboxen einer Form beim Load_Ereignis leeren und dachte mir ich benutze die Controls-Auflistung. Allerdings weiß ich jetzt nicht, wie ich bei einem Control den Typ bestimme.

Hier der Code zur Verdeutlichung:

Code:
Private Sub Form_Load()
    Dim T As Control
    
    'On Local Error Resume Next
    
    For Each T In Form_Einstellungsbildschirm.Controls
        T.Text = ""
    Next T
        
End Sub

Ohne die Fehlerbehandlung laufe ich natürlich immer auf einen Fehler, da ich auch andere Steuerelemente verwende.

Mit der Fehlerbehandlung geht's zwar, ist aber keine saubere Lösung :)

Hat jemand eine Idee ?

Danke im Vorraus.
 
Zuletzt bearbeitet:
Eigentlich ganz simpel :)

Code:
Private Sub Form_Load()

Dim box As TextBox

    For Each box In Form1
        box.Text = "bla"
    Next box
 
End Sub
 
Da ist sie, wie Du Dir denken kannst heißt meine Form 'Form_Einstellungsbildschirm'

Code:
Private Sub Form_Load()

    Dim box As TextBox

    For Each box In Form_Einstellungsbildschirm
        box.Text = ""
    Next box

End Sub
 
Ok, hast ja recht :)

Versuche es dann doch mal mit TypeOf. Dabei werden dann nur die Textboxen angesprochen.

Code:
Private Sub Form_Load()

    Dim Ctrl As Control
    
    For Each Ctrl In Form1
    
        If TypeOf Ctrl Is TextBox Then Ctrl.Text = ""
        
    Next Ctrl

End Sub
 
Hier meine Lösung dazu:
(Gibt den Control Code aus)

Code:
 Sub ControlTyp()
 	 Dim ctl as Control
 
 	 For each ctl in Form.Controls
 		  Debug.Print ctl.ControlType
 	 Next ctl
 End Sub

109 zum Beispiel ist hier eine Textbox!
 

Neue Beiträge

Zurück