remove picturebox completly

Joined
Dec 12, 2005
Messages
7
is there a way to completly remove a picturebox from a form while its running? Im making a game and its drawing pictures and when a button is clicked I need it to completly delete the pictureboxes.

im using visual basic .net

any help is greatly appreciated.
 
Last edited by a moderator:
How are you referencing the PictureBox? If you want to remove it by the name used in the designer, code like this will work fine:
[VB]
Substitute the actual controls name for PictureBox1
Me.Controls.Remove(PictureBox1)
[/VB]
Or an event handler...
[VB]
Private Sub PictureBox_Click(sender As Object, e As EventArgs) _
Handles PictureBox1.Click, PictureBox2.Click, PictureBox3.Click

Me.Controls.Remove(DirectCast(sender, Control))
End Sub
[/code]
Youll want to call the Dispose method of the control if you are done with it for good.
 
I used the first code you provided. that should be what I need but its still not quite working right. I have this right now.

[VB]
Me.Controls.Remove(ball1)
ball1.Dispose()
[/VB]

EDIT: didnt quite explain what wasnt working... heh well the image dissapears and looks to have gone away but the boundaries for it are still there. when I move over that spot the collision detection kicks in even though the object is supposedly gone.

is there something I need to put into the dispose parenthesis or am I writing it wrong? im not familiar with it
 
Last edited by a moderator:
You could also assign "Nothing" to balll. This would ensure that reference to this object would be erased and be ready for garbage collection.

but it should work.
 
You may need to detatch event handlers. How you do it depends on how the event handler was attatched in the first place. If you use the Handles clause (or created a handler with the IDE) you need to set the variable to Nothing so VB removes the handler.
[VB]
Ball1 = Nothing
[/VB]

If you used AddHandler, use the RemoveHandler keyword like so...
[VB]
Substitute appropriate event
RemoveHandler Ball1.Click, AddressOf Ball1_Click
[/VB]
[/VB]
 
Back
Top