Solved

How can i remove all labels on the current form?

Posted on 2007-12-03
13
192 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
 
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This video discusses moving either the default database or any database to a new volume.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now