How can i remove all labels on the current form?

Hi experts

As the question title suggests i am trying to prgramatically remove all labels on the current form. My question is how to do this?

Probably an easy question but its urgent so 500 points up for grabs :-)

many thanks for your time

dr_dudd
LVL 2
dr_duddAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
VBRocksConnect With a Mentor Commented:
Oh, Sancler...  I just got what you said about one getting missed!  I didn't catch that when I tested it.

So, here it is revised:

        Dim i As Int16 = 0
        Do Until i = Me.Controls.Count
            If TypeOf Me.Controls(i) Is Label Then
                Me.Controls.Remove(Me.Controls(i))

            Else
                i += 1
            End If
        Loop

        MsgBox("Done")

0
 
cforantCommented:
find/replace
0
 
dr_duddAuthor Commented:
thanks for the input cforant but i dont think you understood the question properly (or i didnt explain properly)

I have labels that are added at run time. the backend database changes and the labels are recreated but in order to save memory etc i remove the old labels first. i was doing this via

for x = 1 to 200
me.controls.remove(label(x))
next

but this is quite slow and not very efficient.
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
VBRocksCommented:
Just like this:

        For Each ctrl As Control In Me.Controls
            If TypeOf ctrl Is Label Then
                Me.Controls.Remove(ctrl)
            End If
        Next

        MsgBox("Done")
0
 
dr_duddAuthor Commented:
Hi VBRocks

I have tried what you suggested but it seems to leave a few random controls behind....

very strange
0
 
SanclerConnect With a Mentor Commented:
VBRocks

I haven't tested, but won't that come across the problem that you are trying to alter the collection while using its enumerator?

On general principles re collections, it will probably need something on the lines of

   For i As Integer = Me.Controls.Count-1 to 0 Step -1
       If TypeOf Me.Controls(i) Is Label Then
            Me.Controls.Remove(i)
       End If
   Next

although I'm not sure if that precise syntax will work for controls.  Nor am I sure it will be quicker than the present method.

Roger
0
 
SanclerCommented:
Cross-post.  But I think what is happening is that, as you remove an earlier control, all subsequent controls get moved down one, and so get missed.

Roger
0
 
VBRocksCommented:
Sancler:  Yes, you're right, generally an error is generated when you try to remove items from
collections when iterating through them using For Each...Next...  However, it actually works in this
example here.   :)

Also, I wouldn't just loop through all of the controls in the Controls collection without testing for the
TypeOf control, or you may end up with errors (it only takes 1 control).  Additionally, testing for the
TypeOf control would be faster, because it wouldn't attempt to remove a control from the collection
if it wasn't a label.

0
 
VBRocksCommented:
Sancler, I looked at your example to make sure I didn't post the exact same thing as yours, and found:

Your example returns an error:

    "Value of type 'Integer' cannot be converted to 'System.Windows.Forms.Control'

0
 
SanclerCommented:
As I said, I hadn't checked the syntax.  I still haven't, but - on general principles - it would need .RemoveAt if an index number was being used.

But my point, which you've now dealt with, was simply that changing a collection whilst enumerating it can get things "our of synch"

Roger
0
 
VBRocksCommented:
Oh, yes sir...  You were absolutely correct.

0
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
Generally speaking...either enumerate the collection backwards (as Roger has shown), or add all the "matches" to a seperate collection and then iterate over that AFTERWARDS and remove the items.

Also, you would need a RECURSIVE search to catch all Labels if some are not contained directly by the Form itself (like in a Panel or GroupBox for instance).

Public Class Form1
 
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim matches As New List(Of Control)
        CollectLabels(Me, matches)
        For Each ctl As Control In matches
            ctl.Dispose()
        Next
    End Sub
 
    Private Sub CollectLabels(ByVal cnt As Control, ByVal matches As List(Of Control))
        For Each ctl As Control In cnt.Controls
            If TypeOf ctl Is Label Then
                matches.Add(ctl)
            ElseIf ctl.HasChildren Then
                CollectLabels(ctl, matches)
            End If
        Next
    End Sub
 
End Class

Open in new window

0
 
dr_duddAuthor Commented:
Thanks for the help experts was a joint effort so i have split the points.

ps.sorry for abandoning for a while i had to move offices
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.