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
Next
End If
''' <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
Else
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
Else
If Key = ctl.Name.ToLower Then Gut = True
End If
End If
End If
If Gut = True Then
L.Add(ctl)
If ReturnAtFirstElement = True Then ReturnFlag = True
End If
If ReturnFlag = False Then
Call MyGetControl(ctl, Key, L)
End If
Next
End If
If L.Count - 1 > -1 Then
Return True
Else
Return False
End If
End Function
1 Kommentare zum Snippet