VB - List all controls by using the name of the controls

Veröffentlicht von am 07.01.2008
(1 Bewertungen)
Ever tried to use the array objects like in VB6? VB.Net is much better if you use this function. Just send a new List(Of Control) to this function and you get the controls by the keyword. You can use the "*" tag to find similiar controls.

Example: TextBox1, TextBox2, TextBox3, TextElement1, TextElement2

'Create a new List
Dim New L as List(Of Control)
Dim TxtB as TextBox
If MyGetControl(Me, "TextBox*", L) = True Then
'All 3 TextBoxes can now be used
For Each ctl as Control in L
TxtB = Ctype(ctl, Textbox)
TxtB.Text = Now.ToString
End If
GFU-Schulungen  [Anzeige]

Visual Studio Team Foundation Server 2017/2015 (TFS) für Projektmitglieder - Kompakt

Nach Teilnahme an dieser Schulung sind Ihnen die Grundlagen von ALM geläufig. Sie planen und steuern Projekte effizient mit dem Visual Studio Team Foundation Server.

C# 2017/2015/2013 Aufbau

In dieser Schulung lernen Sie fortgeschrittene Techniken im Bereich .Net C#. Dabei stehen neben den eigentlichen Techniken auch architektonische Aspekte im Mittelpunkt.

 ''' <summary>
    ''' Add a control to the L List by name.
    ''' Use * at front or at the end if you know only a fragment 
    ''' of the controls name.
    ''' </summary>
    ''' <param name="BaseControl"></param>
    ''' <param name="Key"></param>
    ''' <param name="L"></param>
    ''' <returns>True if one or more controls are found with the 
    ''' requested name</returns>
    ''' <remarks></remarks>
    Public Function MyGetControl(ByVal BaseControl As Control, ByVal Key As String, ByRef L As List(Of Control), Optional ByVal ReturnAtFirstElement As Boolean = False) As Boolean
        If L Is Nothing Then L = New List(Of Control)
        Dim Gut As Boolean
        Dim ReturnFlag As Boolean = False
        If Key IsNot Nothing Then Key = Key.ToLower

        If BaseControl.HasChildren = True Then
            For Each ctl As Control In BaseControl.Controls
                Gut = False
                If Key Is Nothing Then
                    Gut = True
                    If ctl.Name.Length >= Key.Length Then
                        Key = Key.ToLower
                        If Key.StartsWith("*") Then
                            If Key.Substring(1) = ctl.Name.ToLower.Substring(ctl.Name.Length - (Key.Length - 1), Key.Length - 1) Then Gut = True
                        ElseIf Key.EndsWith("*") Then
                            If Key.Substring(0, Key.Length - 1) = ctl.Name.ToLower.Substring(0, Key.Length - 1) Then Gut = True
                            If Key = ctl.Name.ToLower Then Gut = True
                        End If
                    End If
                End If

                If Gut = True Then
                    If ReturnAtFirstElement = True Then ReturnFlag = True
                End If
                If ReturnFlag = False Then
                    Call MyGetControl(ctl, Key, L)
                End If
        End If

        If L.Count - 1 > -1 Then
            Return True
            Return False
        End If
    End Function

Abgelegt unter controls, find, string, get, control, list, array.

1 Kommentare zum Snippet

jack schrieb am 24.02.2010:
what I have searched for

Logge dich ein, um hier zu kommentieren!