Search code examples
vb.netvb.net-2010

How to remove all created labels on my form


I have this code and I want to know how to remove all created labels in my form.

I tried to put lbl.dispose() and ltr.dispose() on a Button but it says that it's not declared or inaccessible.

Dim break As Integer = 99


            For i = 0 To break

            If jobA > 0 And jobA > time Then
                jobA = jobA - time
                Dim lbl As Label = New Label
                Dim ltr As Label = New Label
                lbl.Location = New System.Drawing.Point(x, 280)
                lbl.Size = New System.Drawing.Point(20, 20)
                lbl.Text = time + spudow(waifuu)
                ltr.Location = New System.Drawing.Point(y, 250)
                ltr.Size = New System.Drawing.Point(20, 20)
                ltr.Text = "A"
                Me.Controls.Add(lbl)
                Me.Controls.Add(ltr)

            ElseIf jobA > 0 And jobA < time Then
                Dim lbl As Label = New Label
                Dim ltr As Label = New Label
                lbl.Location = New System.Drawing.Point(x, 280)
                lbl.Size = New System.Drawing.Point(20, 20)
                lbl.Text = jobA + spudow(waifuu)
                ltr.Location = New System.Drawing.Point(y, 250)
                ltr.Size = New System.Drawing.Point(20, 20)
                ltr.Text = "A"
                Me.Controls.Add(lbl)
                Me.Controls.Add(ltr)

            ElseIf jobA = time Then
                Dim lbl As Label = New Label
                Dim ltr As Label = New Label
                lbl.Location = New System.Drawing.Point(x, 280)
                lbl.Size = New System.Drawing.Point(20, 20)
                lbl.Text = jobA + spudow(waifuu)
                ltr.Location = New System.Drawing.Point(y, 250)
                ltr.Size = New System.Drawing.Point(20, 20)
                ltr.Text = "A"
                Me.Controls.Add(lbl)
                Me.Controls.Add(ltr)

            End If

  Me.Refresh()

Solution

  • I like Zaggler's idea of using a List(Of Label), but I'll post another approach using the .Tag property of the Labels instead. This will work well enough if all the Labels will only be added to the Form itself. If you're going to spread the Labels across multiple containers then use the List(Of Label) approach instead as it will be easier. Additionally, you can get rid of a lot of redundant code by restructuring like this (I assume there is more un-shown code that is somehow changing the "x" and "y" values!):

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim break As Integer = 99
        For i = 0 To break
    
            Dim lbl As Label = New Label
            Dim ltr As Label = New Label
            lbl.Tag = "dynamic"
            ltr.Tag = "dynamic"
            lbl.Location = New System.Drawing.Point(x, 280)
            lbl.Size = New System.Drawing.Point(20, 20)
            lbl.Text = ""
            ltr.Location = New System.Drawing.Point(y, 250)
            ltr.Size = New System.Drawing.Point(20, 20)
            ltr.Text = "A"
    
            If jobA > 0 Then
                If jobA > time Then
                    jobA = jobA - time
                    lbl.Text = time + spudow(waifuu)
                ElseIf jobA < TimeOfDay Then
                    lbl.Text = jobA + spudow(waifuu)
                End If
            ElseIf jobA = time Then
                lbl.Text = jobA + spudow(waifuu)
            End If
    
            If lbl.Text <> "" Then
                Me.Controls.Add(lbl)
                Me.Controls.Add(ltr)
            End If
    
        Next i
    End Sub
    

    Then, when you want to remove the dynamic Labels, you can use code like this:

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Dim labels = Me.Controls.OfType(Of Label).Where(Function(x) x.Tag = "dynamic").ToList
        For Each lbl As Label In labels
            lbl.Dispose()
        Next
    End Sub