Results 1 to 6 of 6

Thread: [RESOLVED] [2005] me.controls.remove() problem (HELP!!! :-( )

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2006
    Posts
    734

    Resolved [RESOLVED] [2005] me.controls.remove() problem (HELP!!! :-( )

    Hey guys,

    I am setting up several controls on a form at run time using
    Code:
    dim lblMessage as label = new label
    and then after setting up its location and text, etc and adding it to the forms controls using
    Code:
    me.controls.add(lblMessage)
    Now when the form is closed i call a function which disposes of all the controls on the form.

    In this function i am using the code

    Code:
    For Each ctrl as Control in Me.Controls
    Me.Controls.Remove(ctrl)
    ctrl.dispose()
    Next
    but for some reason to which i am not aware it is only removing and disposing half of all the controls on the form. If i get rid of the code and message out the name of all the controls on the form then it lists all of them but when trying to delete them, it appears to stop half way through.

    I have also output a message after the code to remove the controls stating how many controls are left on the form (incase it somehow wasn't actually removing them and just hiding the instead) and the message that is displayed shows that there are indeed several controls remaining. I have racked my brain as to the reasons it is doing this and have gained nothing except for a headache, so can anyone else shed some light on the situation and possible tell me where things are going wrong and what i can possibly do to get this to work properly?

    Thank you in advance for any help
    If your problem has been solved then please mark the thread [RESOLVED].
    If i have helped then please Rate my post

  2. #2
    Fanatic Member
    Join Date
    Feb 2007
    Location
    Eindhoven
    Posts
    828

    Re: [2005] me.controls.remove() problem (HELP!!! :-( )

    isn't the prblem comming from the fact thatyou are modifying the cllection that your are actually looping in? When you remove a control, the pointer goes to the next position in the collection. On the other side the collection is modified and the supposely next becomes previous.

    Basically doing what your are actually wrote will mean that you want to remove at position 0 all the time.

    By disposing of the form, doesn't it dispose of all its controls automatically?

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2006
    Posts
    734

    Re: [2005] me.controls.remove() problem (HELP!!! :-( )

    Hi, thanx, I have used this method before with it working fine, i dont want to dispose of the form because it is the main form of the program.

    I will try creating a new form and then disposing of the old form, but if anybody else has a different way that would prevent me from doing this then i appreciate the input.

    Thanks again.
    If your problem has been solved then please mark the thread [RESOLVED].
    If i have helped then please Rate my post

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [2005] me.controls.remove() problem (HELP!!! :-( )

    vb.net Code:
    1. For Each ctl As Control In Me.Controls
    2.     ctl.Dispose()
    3. Next ctl
    4.  
    5. Me.Controls.Clear()
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    VBA Nutter visualAd's Avatar
    Join Date
    Apr 2002
    Location
    Ickenham, UK
    Posts
    4,906

    Re: [2005] me.controls.remove() problem (HELP!!! :-( )

    Quote Originally Posted by talkro
    isn't the prblem comming from the fact thatyou are modifying the cllection that your are actually looping in? When you remove a control, the pointer goes to the next position in the collection. On the other side the collection is modified and the supposely next becomes previous.

    Basically doing what your are actually wrote will mean that you want to remove at position 0 all the time.

    By disposing of the form, doesn't it dispose of all its controls automatically?
    The problem is exactly that and it is also documented in MSDN:
    When Remove deletes an element from a collection, it decrements the collection's Count Property (Collection Object) by one. It also decrements the Index value of every element that formerly followed the deleted element in the collection.
    You can see below a demonstration of how it behaves and why the problem would occur by looking at your loop in more deatil This is a collection with siz elements names 1 to 6.

    Code:
    Pass 1
    
    --> 1
        2
        3
        4
        5
        6
    
        Remove 1
    --> 2
        3
        4
        5
        6
    
       Advance Pointer
    
        2
    --> 3
        4
        5
        6
    
    Pass 2
    
        2
    --> 3
        4
        5
        6
    
        Remove 3
        2 
    --> 4
        5
        6
    
       Advance Pointer
    
        4
    --> 5
        6
    
    Pass 2
    
        4
    --> 5
        6
    
        Remove 5
    
        2 
    --> 6
    
       Advance Pointer
    
    /// END OF COLLECTION: EXIT
    PHP || MySql || Apache || Get Firefox || OpenOffice.org || Click || Slap ILMV || 1337 c0d || GotoMyPc For FREE! Part 1, Part 2

    | PHP Session --> Database Handler * Custom Error Handler * Installing PHP * HTML Form Handler * PHP 5 OOP * Using XML * Ajax * Xslt | VB6 Winsock - HTTP POST / GET * Winsock - HTTP File Upload

    Latest quote: crptcblade - VB6 executables can't be decompiled, only disassembled. And the disassembled code is even less useful than I am.

    Random VisualAd: Blog - Latest Post: When the Internet becomes Electricity!!


    Spread happiness and joy. Rate good posts.

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2006
    Posts
    734

    Re: [2005] me.controls.remove() problem (HELP!!! :-( )

    Thanks for all your help. It makes sense the way it works, it just confused me because i had used this method before and it appeared to work fine.

    But anyways thanks again.
    If your problem has been solved then please mark the thread [RESOLVED].
    If i have helped then please Rate my post

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width