Results 1 to 8 of 8

Thread: Remove Dynamic created labels

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2017
    Posts
    36

    Remove Dynamic created labels

    Hi all,

    i am creating dynamic labels with

    Code:
     
     For i = 1 To amount
                lbl_nodeinfo = New System.Windows.Forms.Label
                Dim spacer As String = ""
                If i < 11 Then spacer = " "
                lbl_nodeinfo.Text = "NODE " & spacer & Str(i) - 1 & "   Awaiting Connect  TELNET"
                lbl_nodeinfo.Location = New Point(15, ypos)
                lbl_nodeinfo.Size = New System.Drawing.Size(500, 20)
                ypos += 20
                Me.Controls.Add(lbl_nodeinfo)
                nodecounter = nodecounter + 1
    Next
    amount is send by a listbox with alternating values.

    Now i am searching for a Solution to delete all previously dynamic created labels. I tried different suggestions which i ported from forums but obviusly failed :-)

    Anybody who can help out ?

  2. #2
    Fanatic Member
    Join Date
    Jun 2019
    Posts
    558

    Re: Remove Dynamic created labels

    Add labels to second list and use it to delete when required. Fix the code where needed to suit your needs.
    Code:
    ' Module level variable
    Private _labels = new List(Of Label)
    
    ' Add
    _labels.Clear()
    For ...
        Me.Controls.Add(lbl_nodeinfo)
        _labels.Add(lbl_nodeinfo)
    Next
    
    ' Delete
    For Each lbl in _labels
       Me.Controls.Remove(lbl)
    Next

  3. #3

    Thread Starter
    Member
    Join Date
    Jun 2017
    Posts
    36

    Re: Remove Dynamic created labels

    Quote Originally Posted by peterst View Post
    Add labels to second list and use it to delete when required. Fix the code where needed to suit your needs.
    Code:
    ' Module level variable
    Private _labels = new List(Of Label)
    
    ' Add
    _labels.Clear()
    For ...
        Me.Controls.Add(lbl_nodeinfo)
        _labels.Add(lbl_nodeinfo)
    Next
    
    ' Delete
    For Each lbl in _labels
       Me.Controls.Remove(lbl)
    Next
    This works for deleting, but when i add my labels, the Funtion just places text to the last build Label

    Code:
            Dim nodecounter As Integer = 0
            nodecount = 0
      
        For i = 1 To amount
                Dim spacer As String = ""
                If i < 11 Then spacer = " "
    
                Me.Controls.Add(lbl_nodeinfo)
                _labels.Add(lbl_nodeinfo)
                lbl_nodeinfo.Text = "NODE " & spacer & Str(i) - 1 & "   Awaiting Connect  TELNET"
                lbl_nodeinfo.Location = New Point(15, ypos)
                lbl_nodeinfo.Size = New System.Drawing.Size(500, 20)
                ypos += 20
                nodecounter = nodecounter + 1
    
            Next

  4. #4
    Fanatic Member
    Join Date
    Jun 2019
    Posts
    558

    Re: Remove Dynamic created labels

    If you reorder your original code, my single line will not help. Take a breath and look at your two samples, analyze what happens and you will find where is the problem in the second one.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,352

    Re: Remove Dynamic created labels

    Why would you change the code you already had? It was working and you broke by doing something you weren't told to do. Just do what you were told to do, i.e. assign a list to a field, add the Labels to the list when you create them and then remove them from that list when you're done with them.

  6. #6

    Thread Starter
    Member
    Join Date
    Jun 2017
    Posts
    36

    Re: Remove Dynamic created labels

    Quote Originally Posted by jmcilhinney View Post
    Why would you change the code you already had? It was working and you broke by doing something you weren't told to do. Just do what you were told to do, i.e. assign a list to a field, add the Labels to the list when you create them and then remove them from that list when you're done with them.
    Allright, that was indeed m Fault. Got it working meanwhile fixing with adding the missing command.

    Now that is working, i tried changing the dynamic label text but couldnt figure how to access the dynamic labels,
    or to be exact i cant figure the correct access names of the dynamic labels for changing text individually.

    I had a look at some samples but dont understand how that works.
    I know thats the past but with vb6 i was accessing indexed labels like label(0).text ="sometext" and hoped there was a simmilar access method in vb.net ?

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,481

    Re: Remove Dynamic created labels

    Assuming you’re using…

    Private _labels = new List(Of Label)

    _labels(0).Text =“whatever”

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,481

    Re: Remove Dynamic created labels

    Also, after…

    Code:
    ' Delete
    For Each lbl in _labels
       Me.Controls.Remove(lbl)
    Next
    Code:
    _labels.Clear

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width