2016-12-01 3 views
0

Im Versuch, all das Feld im Textfeld zu löschen und alle Kontrollkästchen deaktivieren, wenn das Formular auf LastWie für jedes in Multiple Control zu verwenden?

meine Checkbox sind innerhalb Table All, aber ich habe eine Menge von Table

Aktuell im, mit dieser Methode, aber es ist zu viele doppelten Code

Private Sub ResetPage()  

    Dim ctrl As Control 

    For Each ctrl In tlp_userInfo.Controls 
     If TypeOf ctrl Is TextBox Then 
      ctrl.Text = Nothing 
     End If 
     If TypeOf ctrl Is ComboBox Then 
      ctrl.Text = Nothing 
     End If 
    Next 

    For Each ctrl In tlp_chkb1.Controls 
     If TypeOf ctrl Is CheckBox Then 
      DirectCast(ctrl, CheckBox).Checked = False 
     End If 
    Next 

    For Each ctrl In tlp_chkb2.Controls 
     If TypeOf ctrl Is CheckBox Then 
      DirectCast(ctrl, CheckBox).Checked = False 
     End If 
    Next 

    For Each ctrl In tlp_chkb3.Controls 
     If TypeOf ctrl Is CheckBox Then 
      DirectCast(ctrl, CheckBox).Checked = False 
     End If 
    Next 

End Sub 

ist, dass jede andere Art und Weise alle das Kontrollkästchen in diffrent Table zu deaktivieren?

Edited:

Schließlich ich es bekommen getan, aber ich glaube nicht, dass dies eine gute Idee ist, wenn das Formular so viel Schicht aufweist.

 For Each ctrl_layer1 As Control In Me.Controls 
     If TypeOf ctrl_layer1 Is TableLayoutPanel Then 
      For Each ctrl_layer2 As Control In ctrl_layer1.Controls 
       If TypeOf ctrl_layer2 Is TableLayoutPanel Then 
        For Each ctrl_layer3 As Control In ctrl_layer2.Controls 
         If TypeOf ctrl_layer3 Is TableLayoutPanel Then 
          For Each ctrl_layer4 In ctrl_layer3.Controls 
           If TypeOf ctrl_layer4 Is TextBox Then 
            ctrl_layer4.Text = "" 
           ElseIf TypeOf ctrl_layer4 Is ComboBox Then 
            ctrl_layer4.Text = "" 
           ElseIf TypeOf ctrl_layer4 Is TabControl Then 
            For Each ctrl_layer5 As Control In ctrl_layer4.controls 
             If TypeOf ctrl_layer5 Is TabPage Then 
              For Each ctrl_layer6 In ctrl_layer5.Controls 
               If TypeOf ctrl_layer6 Is TableLayoutPanel Then 
                For Each ctrl_layer7 In ctrl_layer6.controls 
                 If TypeOf ctrl_layer7 Is TableLayoutPanel Then 
                  For Each ctrl_layer8 In ctrl_layer7.controls 
                   If TypeOf ctrl_layer8 Is CheckBox Then 
                    DirectCast(ctrl_layer8, CheckBox).Checked = False 
                   End If 
                  Next 
                 End If 
                Next 
               End If 
              Next 
             End If 
            Next 
           End If 
          Next 
         End If 
        Next 
       End If 
      Next 
     End If 
    Next 
+0

Sie sollten Looping starten von 'ctrl_layer3' nur, was ist der Punkt der Schleife 'ctrl_layer1' und' ctrl_layer2'? –

Antwort

1

Schleife durch alle TableLayoutPanel in Ihrer Form und Schleife durch textboxes, checkboxes in jedem TableLayoutPanel

For Each ctrl_tlo As Control In Me.Controls 
     If TypeOf (ctrl_tlo) Is TableLayoutPanel Then 
     For Each ctrl As Control In ctrl_tlo.Controls 
      If TypeOf (ctrl) Is TextBox Then 
       ctrl.Text = "" 
      ElseIf TypeOf (ctrl) Is CheckBox Then 
       DirectCast(ctrl, CheckBox).Checked = False 
      End If 
     Next 
     End If 
    Next 
+0

funktioniert nicht, ich benutze Haltepunkt, um während des Debug-Modus zu überprüfen, kann das System keine Textbox oder Kontrollkästchen finden, wenn bis Zeile 4 ausgeführt – vbnewbie

+0

Ist es passiert durch 'TableLayoutPanel'? –

+0

Ich denke, ich weiß, was das Problem bereits ist, weil ich eine andere tablelayoutpanel innerhalb der tabellayoutpanel haben ... – vbnewbie

0

versuchen, diese

Dim SetofPanels = {Name of your panels with comma between each controls} 
For Each ctrl In SetofPanels 

If TypeOf ctrl Is TextBox Then 
     ctrl.Text = Nothing 
End If 

If TypeOf ctrl Is ComboBox Then 
     ctrl.Text = Nothing 
End If 

If TypeOf ctrl Is CheckBox Then 
     DirectCast(ctrl, CheckBox).Checked = False 
End If 

Next 
+0

es funktioniert nicht, ich bekomme diesen Fehler Ende der Anweisung erwartet. rote Unterstreichung unter Komma – vbnewbie

+0

Möchten Sie einen Code, der alle Steuerelemente in diff löscht. Panel in nur einem Code? –

+0

ja, weil ich mehr als 10 TableLayoutPanel mit Kontrollkästchen in diesem Formular haben – vbnewbie

Verwandte Themen