Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How can i remove all labels on the current form?

Posted on 2007-12-03
13
Medium Priority
?
200 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
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 400 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 1600 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

810 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