Mehrere Labels per Button erstellen

  • VB.NET

Es gibt 12 Antworten in diesem Thema. Der letzte Beitrag () ist von ErfinderDesRades.

    Mehrere Labels per Button erstellen

    Hallo Leute,

    Wie kann ich per Knopfdrück mehrere Labels erzeugen lassen? Also ein Label pro Buttondruck

    Ich habe soweit das hier

    VB.NET-Quellcode

    1. Dim noLabel As Integer = 1
    2. Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    3. Dim nLabel As New Label
    4. nLabel = New Label
    5. With nLabel
    6. .Name = noLabel
    7. .Height = 13
    8. .Text = "Zone l/min)" & noLabel
    9. .Left = 200 + noLabel * 10
    10. .Top = 200 + Rnd(10)
    11. .Visible = True
    12. End With
    13. Controls.Add(nLabel)
    14. noLabel = noLabel + 1
    15. End Sub



    Es funktioniert jedoch nur einmal, das erste Label wird angezeigt wie es soll, aber wenn ich danach nochmal auf Button3 drücke, passiert nichts mehr
    :saint:
    Okay, das erste ist Schwachsinn, habe die zweite Zeile entfernt.

    Wie löse ich denn das Problem ?
    :saint:
    Danke, es funktioniert jetzt. 2 Sachen noch, also:

    Wegen dem Option Strict funktioniert folgende Funktion nicht mehr

    VB.NET-Quellcode

    1. Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    2. Form2.TextBox1.Text = "<html>" & vbNewLine & "<link rel='stylesheet' type='text/css' href='vb.css' />" & vbNewLine
    3. For Each ctrl As Control In Me.Controls
    4. If ctrl.Name.StartsWith("LinkLabel") Then
    5. Dim plabel As LinkLabel = ctrl
    6. Form2.TextBox1.Text &= "<font class[/color]='" & ctrl.Name & "'>" & ctrl.Text & "</font>" & vbNewLine
    7. Form2.TextBox2.Text &= "." & ctrl.Name & vbNewLine & "{" & vbNewLine & "position:absolute;" & vbNewLine & "left: " & ctrl.Location.X & ";" & vbNewLine & "top: " & ctrl.Location.Y & ";" & vbNewLine & "color: " [color=#ff0000]& plabel.LinkColor.Name &[/color] ";" & vbNewLine & "}" & vbNewLine
    8. End If
    9. Next
    10. Form2.Show()
    11. End Sub


    Mit dem Option Strict müsste ich diese Zeile entfernen: Dim plabel As LinkLabel = ctrl
    dann könnte ich jedoch nicht mehr die Farbe des LinkLabels auslesen, welche ich für die Funktion brauche

    (edited)
    :saint:
    Ich hab den Code mal nach Option Strict übersetzt und außerdem die VB6-Funktion Rnd durch ihr .Net-Äquivalent ersetzt:

    VB.NET-Quellcode

    1. Dim noLabel As Integer = 1
    2. Dim rnd As New Random
    3. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    4. Dim nLabel As New Label
    5. With nLabel
    6. .Name = "Label" & noLabel
    7. .Text = noLabel.ToString
    8. .Location = New Point(200 + noLabel * 10, 200 + rnd.Next(10))
    9. .Visible = True
    10. End With
    11. Controls.Add(nLabel)
    12. noLabel += 1
    13. End Sub

    Der funktioniert schon, nur siehst du die übrigen Labels nicht, da sie von dem ersten verdeckt werden. erhöhe mal den Abstand zwischen ihnen, dann siehst du sie.

    Und dein anderer Code sollte so aussehen:

    VB.NET-Quellcode

    1. Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    2. Form2.TextBox1.Text = "<html>" & vbNewLine & "<link rel='stylesheet' type='text/css' href='vb.css' />" & vbNewLine
    3. For Each ctrl As Control In Me.Controls
    4. If TypeOf ctrl Is LinkLabel Then
    5. Dim plabel As LinkLabel = DirectCast(ctrl, LinkLabel)
    6. Form2.TextBox1.Text &= "<font class[/color]='" & ctrl.Name & "'>" & ctrl.Text & "</font>" & vbNewLine
    7. Form2.TextBox2.Text &= "." & ctrl.Name & vbNewLine & "{" & vbNewLine & "position:absolute;" & vbNewLine & "left: " & ctrl.Location.X & ";" & vbNewLine & "top: " & ctrl.Location.Y & ";" & vbNewLine & "color: " [color=#ff0000]& plabel.LinkColor.Name &[/color] ";" & vbNewLine & "}" & vbNewLine
    8. End If
    9. Next
    10. Form2.Show()
    11. End Sub

    Artentus schrieb:


    Der funktioniert schon, nur siehst du die übrigen Labels nicht, da sie von dem ersten verdeckt werden. erhöhe mal den Abstand zwischen ihnen, dann siehst du sie.


    Hatte ich doch,oder?
    :saint:
    Nimm doch einfach ein FlowLayoutPanel und steck die Labels, wenn Du sie schon zur Laufzeit erzeugen willst, da rein. Dann musst Du Dich nicht um die Positionierung kümmern.
    Die Unendlichkeit ist weit. Vor allem gegen Ende. ?(
    Manche Menschen sind gar nicht dumm. Sie haben nur Pech beim Denken. 8o
    Okay danke für die Antworten.

    Eine Sache noch: Wie kann ich dem Programm sagen was der Label machen soll wenn ich ihn klicke?

    Ich möchte, dass ich die Label hinterher per Drag & Drop verschieben kann

    Mit den Buttons etc die ich schon im Designer erstellt habe ist das ja ganz einfach, da habe ich bei jedem Button stehen

    (Bsp: Button1)

    VB.NET-Quellcode

    1. If bMoveActive = False Then
    2. movingcontrol = Button1
    3. bMoveActive = True
    4. Else
    5. bMoveActive = False
    6. End If


    Also wenn man den Button einmal klickt, bewegt er sich mit der Maus mit, und wenn man ihn nochmal drückt, bleibt er da.
    :saint:
    Mit AddHandler kannst du alles auf eine Prozedur konzentrieren. Also dann:

    VB.NET-Quellcode

    1. ...
    2. Controls.Add(nLabel)
    3. AddHandler nLabel.MouseDown, AddressOf MoveSub
    4. End Sub
    5. Private Sub MoveSub(sender As Object, e As EventArgs)
    6. If bMoveActive = False Then
    7. movingcontrol = Directcast(sender, Label)
    8. bMoveActive = True
    9. Else
    10. bMoveActive = False
    11. End If
    12. End Sub

    Und [VB 2010] Instanziierung von Forms und Aufruf von Dialogen
    Mfg
    Vincent

    Ah danke, ich kannte zwar den AddHandler schon, aber ich konnte das mit dem sender nicht,

    hast mir sehr weitergeholfen
    :saint: