Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Compact Framework / April 2006

Tip: Looking for answers? Try searching our database.

Removing controls on the fly

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Safiiru no Baka - 03 Apr 2006 17:04 GMT
Hi I'm having issues removing controls on the fly.  Here's my code:
=================================================================
        public Form1()
        {
            //
            // Required for Windows Form Designer support
            //
            InitializeComponent();

            for(int i = 0; i < this.Controls.Count; i++)
            {
                System.Windows.Forms.Control tempText;
                tempText = this.Controls[i];

                if(tempText is System.Windows.Forms.TextBox)
                {
                    this.Controls.Remove(tempText);
                    tempText.Dispose();
                }

            }
            //
            // TODO: Add any constructor code after InitializeComponent call
            //
        }
=================================================================

It is working somewhat, some text boxes are removed while some are not.
I tried directly using the "this.Controls[i]" handle but that behaves
the same way, except different fields are left behind.

Thanks in advance.
Tim Wilson - 03 Apr 2006 17:33 GMT
Are some of the TextBox's parented by another control such as a Panel?

Signature

Tim Wilson
.NET Compact Framework MVP

> Hi I'm having issues removing controls on the fly.  Here's my code:
> =================================================================
[quoted text clipped - 28 lines]
>
> Thanks in advance.
Daniel Moth - 03 Apr 2006 17:39 GMT
Hint: Modifying a collection while iterating through it is probably not a
good idea...

In your case, every time you remove a textobox, the next control doesn't get
a look in.

See this for a hack:
http://www.danielmoth.com/Blog/2005/02/hack-prevent-enumerator-throwing.html

If you cannot redesign so you can do the removal outside the loop (and also
avoid the slow Remove method that does a full iteration every time), then
walk the loop backwards i.e.
for (int i = this.Controls.Count-1; i >=0 ; i--)

(note, in my hack solution on my blog, iterating backwards was not an option
for other reasons but in your case it should be fine)

Cheers
Daniel
--
http://www.danielmoth.com/Blog/

> Hi I'm having issues removing controls on the fly.  Here's my code:
> =================================================================
[quoted text clipped - 28 lines]
>
> Thanks in advance.
Safiiru no Baka - 03 Apr 2006 22:05 GMT
Hi, thanks for the replies.

I figure i'd update the solution i found.  The call to Controls.Count
was changing every iteration as I called Controls.Remove so the loop
was being cut off.

Also... once you remove a Control the indexing of all controls "shift"
down to fill the gap so that was screwing things up as well.

Indexing backwards might do the trick however! One last question
however, what do you mean with "Remove" being slow? Thanks again.
Daniel Moth - 03 Apr 2006 22:48 GMT
> however, what do you mean with "Remove" being slow? Thanks again.
Not much more to expand on that. The Remove method on collections (e.g.
ArrayList) performs badly. RemoveAt is much faster. So wherever possible I
try to use the latter when I know the index (like in this case).

Cheers
Daniel
--
http://www.danielmoth.com/Blog/

> Hi, thanks for the replies.
>
[quoted text clipped - 7 lines]
> Indexing backwards might do the trick however! One last question
> however, what do you mean with "Remove" being slow? Thanks again.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.