Solved

How can i remove all labels on the current form?

Posted on 2007-12-03
13
195 Views
Last Modified: 2010-04-23
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
0
Comment
Question by:dr_dudd
  • 5
  • 3
  • 3
  • +2
13 Comments
 
LVL 3

Expert Comment

by:cforant
ID: 20396599
find/replace
0
 
LVL 2

Author Comment

by:dr_dudd
ID: 20396614
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
 
LVL 27

Expert Comment

by:VBRocks
ID: 20396777
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
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 2

Author Comment

by:dr_dudd
ID: 20396849
Hi VBRocks

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

very strange
0
 
LVL 34

Assisted Solution

by:Sancler
Sancler earned 100 total points
ID: 20396853
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
 
LVL 34

Expert Comment

by:Sancler
ID: 20396860
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
 
LVL 27

Expert Comment

by:VBRocks
ID: 20396885
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
 
LVL 27

Accepted Solution

by:
VBRocks earned 400 total points
ID: 20396914
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
 
LVL 27

Expert Comment

by:VBRocks
ID: 20397065
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
 
LVL 34

Expert Comment

by:Sancler
ID: 20397192
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
 
LVL 27

Expert Comment

by:VBRocks
ID: 20397210
Oh, yes sir...  You were absolutely correct.

0
 
LVL 85

Expert Comment

by:Mike Tomlinson
ID: 20397300
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
 
LVL 2

Author Closing Comment

by:dr_dudd
ID: 31412363
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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question