[Visualbasic 2010] Clear form of runtime objects

Discussion in 'Mixed Languages' started by stevemk14ebr, Dec 31, 2011.

  1. stevemk14ebr

    stevemk14ebr MDL Senior Member

    Jun 23, 2010
    267
    48
    10
    #1 stevemk14ebr, Dec 31, 2011
    Last edited by a moderator: Apr 20, 2017
    So i created a few picture boxes and rich text boxes during runtime and i want the user to be able to click 1 button to remove every single picture box or richtextbox. I've already tried a for each loop with me.controls and that doesn't work (leaves some behind and only removes 1 or 2) :
    Code:
    Dim c As Control
            For Each c In Me.Controls
                If (TypeOf c Is PictureBox) Then
                    c.Dispose()
                End If
            Next
            For Each c In Me.Controls
                If (TypeOf c Is RichTextBox) Then
                    c.Dispose()
                End If
            Next
    I really need a way to remove those objects, but that's all i got
     
  2. Calistoga

    Calistoga MDL Senior Member

    Jul 25, 2009
    420
    199
    10
    #2 Calistoga, Jan 1, 2012
    Last edited by a moderator: Apr 20, 2017
    Give this a try:
    Code:
    For Each c In Me.Controls
         If (TypeOf c Is RichTextBox Or TypeOf c Is RichTextBox) Then c.Visible = False
    Next
    It doesn't erase the Controls, but it hides them so that you may make them visible again later if you want ;)

    If you want to remove the Controls completely, check out Me.Controls.Remove(c) where c is one of the controls (in the For-loop). Remember to set c to Nothing so that the Garbage Collector removes the unused object upon its next run.
     
  3. stevemk14ebr

    stevemk14ebr MDL Senior Member

    Jun 23, 2010
    267
    48
    10
    Ok so setting the visibility to false is working how it should,but i really want to remove the control itself but that doesn't work how it should(same code as above, but replaced c.visible=false with me.controls.remove(c) and also tried c.dispose). I almost need a way to check if that control still exists then if it still exists try to remove it again and again untill it doesn't exist (I think it doesn't remove them all because it takes to long to remove them all and sort of times out in a sense)
     
  4. Calistoga

    Calistoga MDL Senior Member

    Jul 25, 2009
    420
    199
    10
    #4 Calistoga, Jan 1, 2012
    Last edited by a moderator: Apr 20, 2017
    I think I know why, we're trying to remove elements from a collection while at the same time looping over that collection. The enumerator doesn't like that very much :p

    One way to solve it is:
    Code:
    Dim list As List(Of Control) = New List(Of Control)
    
    For Each c In Me.Controls
         If (TypeOf c Is RichTextBox Or TypeOf c Is RichTextBox) Then list.Add(c)
    Next
    
    For Each c In list
         Me.Controls.Remove(c)
    Next
    
    This should theoretically speaking work :D
     
  5. stevemk14ebr

    stevemk14ebr MDL Senior Member

    Jun 23, 2010
    267
    48
    10
    AHAH as usual u got it exactly right,works perfect...I feel like such a noob. Thanks for all your help man you solved all of my major problems in this program thanks :D:biggrin::rolleyes::worthy:
     
  6. Calistoga

    Calistoga MDL Senior Member

    Jul 25, 2009
    420
    199
    10
    I know you'll get the hang of it ;)

    Keep us posted if you encounter any other difficulties :)
     
  7. stevemk14ebr

    stevemk14ebr MDL Senior Member

    Jun 23, 2010
    267
    48
    10
    do you think it would be ok if i made a thread about some of the harder thing for me to learn for other people to see (like a centralized how-to guide). More advanced stuff of course like a snap to grid function and drawing to form and stuff like this and the idea of arrays and lists
     
  8. Calistoga

    Calistoga MDL Senior Member

    Jul 25, 2009
    420
    199
    10
    Yeah I'm sure a lot of people would find that very useful. You could ask jlgager to link to your thread from his repository :)

    I think most people struggles a lot when they start doing custom drawing especially, so having a nice step by step guide would probably be most helpful.
     
  9. stevemk14ebr

    stevemk14ebr MDL Senior Member

    Jun 23, 2010
    267
    48
    10
    ok that sounds like a good idea it'll be a while till i make my thread though(maybe 1 to 3 weeks) cuz i got other stuff 2 do