Solved

How can i remove all labels on the current form?

Posted on 2007-12-03
13
197 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 86

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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…

707 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