Forum: VB.NET |
Thema:
Re: Zur Laufzeit erstellen... |
Von:
Berthold Neumann (
03.10.2004 09:09) |
Folgendes Mini-Programm erstellt 5 dynamische Labels, anschließend erhält Label-2 eine neue Farbe, Label-3 wird zerstört und Label-4 bekommt einen neuen Text.
Das entscheidende Proberty ist Me.Controls, das alle Controls der aktuellen Klasse als Array enthält.
Es wird der Index zu dem übergebenen Namen ermittelt und das aktuelle Label (LA) wird darauf instanziert.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim i As Integer
For i = 1 To 5
AddLabel("Label-" + i.ToString, 20 + 10, i * 30)
Next
Dim La As Label
La = Me.Controls(GetControlByName("Label-2"))
la.BackColor = Color.Blue
La = Me.Controls(GetControlByName("Label-3"))
la.Dispose()
La = Me.Controls(GetControlByName("Label-4"))
La.Text = "Neuer Text"
End Sub
Public Sub AddLabel(ByVal nLabel As String, ByVal x As Int64, ByVal y As Int64)
Dim nL As Label = New Label
nL.Name = nLabel
nL.Text = nLabel
nL.BackColor = Color.Goldenrod
nL.Location = New Point(x, y)
Controls.Add(nL)
End Sub
Private Function GetControlByName(ByVal ControlName As String) As Integer
Dim j As Integer
For j = 0 To Me.Controls.Count - 1
If Me.Controls(j).Name = ControlName Then Exit For
Next
Return j
End Function
Antworten
Vorsicht bei der Eingabe: Die Zeichen ' oder -- sind nicht erlaubt!