-
Oct 4th, 2021, 03:19 PM
#1
Thread Starter
Member
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 ?
-
Oct 4th, 2021, 03:28 PM
#2
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
-
Oct 4th, 2021, 05:08 PM
#3
Thread Starter
Member
Re: Remove Dynamic created labels
Originally Posted by peterst
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
-
Oct 5th, 2021, 02:04 AM
#4
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.
-
Oct 5th, 2021, 02:05 AM
#5
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.
-
Oct 9th, 2021, 01:45 PM
#6
Thread Starter
Member
Re: Remove Dynamic created labels
Originally Posted by jmcilhinney
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 ?
-
Oct 9th, 2021, 02:41 PM
#7
Re: Remove Dynamic created labels
Assuming you’re using…
Private _labels = new List(Of Label)
_labels(0).Text =“whatever”
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 9th, 2021, 02:43 PM
#8
Re: Remove Dynamic created labels
Also, after…
Code:
' Delete
For Each lbl in _labels
Me.Controls.Remove(lbl)
Next
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|