VB.NET - Iterating through controls in a container object

后端 未结 10 1250
别那么骄傲
别那么骄傲 2020-12-03 05:18

I have a form with a \"Clear\" button.

When the user clicks \"Clear\", I want to clear the value of all the visible elements on the form. In the case of date contro

相关标签:
10条回答
  • 2020-12-03 05:34
    Public Sub raz(lst As Control.ControlCollection, Optional recursive As Boolean = True)
        For Each ctrl As Control In lst
            If TypeOf ctrl Is TextBox Then
                CType(ctrl, TextBox).Clear()
            End If
    
            If TypeOf ctrl Is MaskedTextBox Then
                CType(ctrl, MaskedTextBox).Clear()
            End If
    
            If TypeOf ctrl Is ComboBox Then
                CType(ctrl, ComboBox).SelectedIndex = -1
            End If
    
            If TypeOf ctrl Is DateTimePicker Then
                Dim dtp As DateTimePicker = CType(ctrl, DateTimePicker)
                dtp.CustomFormat = " "
            End If
    
            If TypeOf ctrl Is CheckedListBox Then
                Dim clbox As CheckedListBox = CType(ctrl, CheckedListBox)
                For i As Integer = 0 To clbox.Items.Count - 1
                    clbox.SetItemChecked(i, False)
                Next
            End If
    
            If TypeOf ctrl Is RadioButton Then
                CType(ctrl, RadioButton).Checked = False
    
            End If
    
            If recursive Then
                If TypeOf ctrl Is GroupBox Then
                    raz(CType(ctrl, GroupBox).Controls)
                End If
            End If
        Next
    End Sub
    
    0 讨论(0)
  • 2020-12-03 05:38

    This May Help in Future Development ...

    GetAllButtons(Me)
    
    Public Sub GetAllButtons(ByRef forms As Object)
        Dim list As New List(Of Button)
        Dim iIndx As Integer
        For Each c In forms.Controls
            For iIndx = 0 To forms.Controls.Count - 1
                If (TypeOf forms.Controls(iIndx) Is Button) Then
                    list.Add(forms.Controls(iIndx))
                End If
                If (TypeOf forms.controls(iIndx) Is Panel) Then
                    For Each cntrl As Control In forms.controls(iIndx).Controls
                        If TypeOf cntrl Is Button Then
                            list.Add(cntrl)
                        End If
                    Next
                End If
            Next
        Next
    
    Button(list.ToArray)
    
    End Sub
    
    Public Sub Button(btn() As Button)
        For Each bt In btn
           Do Something with Buttons
        next
    End Sub
    
    0 讨论(0)
  • 2020-12-03 05:45

    here is the code to get all control of a Form's All GroupControls and you can do something in the GroupBox Control

    Private Sub GetControls()
        For Each GroupBoxCntrol As Control In Me.Controls
            If TypeOf GroupBoxCntrol Is GroupBox Then
                For Each cntrl As Control In GroupBoxCntrol.Controls
                    'do somethin here
    
                Next
            End If
    
        Next
    End Sub
    
    0 讨论(0)
  • 2020-12-03 05:45

    Here it works for all inner controls.
    Add if any other controls do you need to clear.

    Private Sub ClearAll()
        Try
            For Each ctrl As Control In Me.Controls
                If ctrl.[GetType]().Name = "Panel" Then
                    ClearControls(ctrl)
                End If
    
                If ctrl.[GetType]().Name = "GroupBox" Then
                    ClearControls(ctrl)
                End If
                If ctrl.[GetType]().Name = "ComboBox" Then
                    Dim tb As ComboBox = TryCast(ctrl, ComboBox)
                    tb.SelectedText = ""
                End If
    
    
                If ctrl.[GetType]().Name = "TabControl" Then
                    ClearControls(ctrl)
                End If
    
                If ctrl.[GetType]().Name = "TextBox" Then
                    Dim tb As TextBox = TryCast(ctrl, TextBox)
                    tb.Clear()
                End If
    
                If ctrl.[GetType]().Name = "RadioButton" Then
                    Dim tb As RadioButton = TryCast(ctrl, RadioButton)
                    tb.Checked = False
                End If
    
                If ctrl.[GetType]().Name = "CheckBox" Then
                    Dim tb As CheckBox = TryCast(ctrl, CheckBox)
                    tb.Checked = False
                End If
    
                If ctrl.[GetType]().Name = "ComboBox" Then
                    Dim tb As ComboBox = TryCast(ctrl, ComboBox)
                    tb.SelectedIndex = 0
                End If
    
                If ctrl.[GetType]().Name = "RichTextBox" Then
                    Dim tb As RichTextBox = TryCast(ctrl, RichTextBox)
                    tb.Clear()
    
                End If
            Next
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error Message", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub
    
    
    Private Sub ClearControls(ByVal Type As Control)
    
        Try
            For Each ctrl As Control In Type.Controls
    
                If ctrl.[GetType]().Name = "TextBox" Then
                    Dim tb As TextBox = TryCast(ctrl, TextBox)
                    tb.Clear()
                End If
    
                If ctrl.[GetType]().Name = "Panel" Then
                    ClearControls(ctrl)
                End If
    
                If ctrl.[GetType]().Name = "GroupBox" Then
                    ClearControls(ctrl)
                End If
    
                If ctrl.[GetType]().Name = "TabPage" Then
                    ClearControls(ctrl)
                End If
    
                If ctrl.[GetType]().Name = "ComboBox" Then
                    Dim tb As ComboBox = TryCast(ctrl, ComboBox)
                    tb.SelectedText = ""
                End If
    
                If ctrl.[GetType]().Name = "RadioButton" Then
                    Dim tb As RadioButton = TryCast(ctrl, RadioButton)
                    tb.Checked = False
                End If
    
                If ctrl.[GetType]().Name = "CheckBox" Then
                    Dim tb As CheckBox = TryCast(ctrl, CheckBox)
                    tb.Checked = False
                End If
    
                If ctrl.[GetType]().Name = "RichTextBox" Then
                    Dim tb As RichTextBox = TryCast(ctrl, RichTextBox)
                    tb.Clear()
    
                End If
            Next
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error Message", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub
    
    0 讨论(0)
  • 2020-12-03 05:48

    You can skip the GetType and CType dance with TryCast:

    Dim dtp as DateTimePicker = TryCast(ctrl, DateTimePicker)
    If dtp IsNot Nothing then dtp.Value = Now()
    

    That'll save you about 10 lines.

    An extension method off the Control class should keep it pretty tidy:

    <Extension()> _
    Public Shared Sub ClearValue(c as Control, recursive as Boolean)
       Dim dtp as DateTimePicker = TryCast(c, DateTimePicker)
       If dtp IsNot Nothing Then dtp.Value = Now()
       ' Blah, Blah, Blah
    End Sub
    

    Edit: If the thought of Evil extension methods that ignore NullReferenceExceptions don't make you cringe:

    <Extension()> _
    Public Shared Sub ClearValue(c as CheckBox)
       If c IsNot Nothing Then c.Checked = False
    End Sub
    
    TryCast(ctrl, CheckBox).ClearValue()
    
    0 讨论(0)
  • 2020-12-03 05:49

    I present you my ControlIterator Class

    Source: http://pastebin.com/dubt4nPG

    Some usage examples:

     ControlIterator.Disable(CheckBox1)
    
     ControlIterator.Enable({CheckBox1, CheckBox2})
    
     ControlIterator.Check(Of CheckBox)(Me)
    
     ControlIterator.Uncheck(Of CheckBox)(Me.GroupBox1)
    
     ControlIterator.Hide(Of CheckBox)("1")
    
     ControlIterator.PerformAction(Of CheckBox)(Sub(ctrl As CheckBox) ctrl.Visible = True)
    
     ControlIterator.AsyncPerformAction(RichTextBox1,
                                        Sub(rb As RichTextBox)
                                            For n As Integer = 0 To 9
                                                rb.AppendText(CStr(n))
                                            Next
                                        End Sub)
    
     ControlIterator.PerformAction(Me.Controls, Sub(c As Control)
                                                    c.BackColor = Color.Green
                                                End Sub)
    
    0 讨论(0)
提交回复
热议问题