How can I modify it so it only removes the IMAGES without erasing the pictureboxes?

Heavenly

Well-known member
Joined
Aug 22, 2005
Messages
53
Location
Puerto Rico
Programming Experience
1-3
This code erases all 18 pictureboxes and Images from the form:



How can I modify it so it only removes the IMAGES without erasing the pictureboxes?





Sub RemovePhotos()

Dim Pic As Control

For Each Pic In formSelectedItems.Controls

If TypeOf Pic Is PictureBox Then

formSelectedItems.Controls.Remove(Pic)

Pic.Dispose()

End If

Next

End Sub



 
VB.NET:
[size=2][color=#0000ff] 
Dim[/color][/size][size=2] myPic [/size][size=2][color=#0000ff]As [/color][/size][size=2][color=#0000ff]Object[/color][/size]
[color=#0000ff][/color] 

[size=2][color=#0000ff]
[/color][/size][size=2][color=#0000ff]For [/color][/size][size=2][color=#0000ff]Each[/color][/size][size=2] myPic [/size][size=2][color=#0000ff]In [/color][/size][size=2][color=#0000ff]Me[/color][/size][size=2].Controls [color=green]'replace "Me" if you refer to another form[/color]
 
[/size][size=2][color=#0000ff]If[/color][/size][size=2] myPic.GetType [/size][size=2][color=#0000ff]Is [/color][/size][size=2][color=#0000ff]GetType[/color][/size][size=2](PictureBox) [/size][size=2][color=#0000ff]Then
 
[/color][/size][size=2]myPic.Image = [/size][size=2][color=#0000ff]Nothing
 
[/color][/size][size=2][color=#0000ff]End [/color][/size][size=2][color=#0000ff]If
 
[/color][/size][size=2][color=#0000ff]Next [/color][/size]


Regards ;)
 
kulrom:





Thank you for your solution, I tested I my project and works really fine, I was surprised to find to find this strange behavior.





I’m using VB.NET 2005 Express



When I wrote this line



myPic.Image





the pop up came with only the following choices (methods)



Equals

GetHashCode

GetType

ToString





Image did not appear. Do you know why there were only 4 methods and the Image was not listed, yet I could write it and get working just fine?



I’m very curious; up to know I thought that the choices shown, in this method pop ups, were the only choices you could use.



Thanks for clarifying this point to a newbie.
 
Show me what your code really looks like. Becuase, if you getType of the object and if condition has been satisfied then it should be threated like pictureBox already and give you all methods including "image" one. In other words, if you go directly for myPic.method without gettype it is obviously that you can't see all those methods as it still doesn't know the type of that object.
However, post the code and i try to give more suggestions if you need ...


Regards ;)
 
Here is the code

:(
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

'Dim i As Short

'For i = 0 To 17

' arrProductNumber(i) = 0

'Next

Dim MyPic As Object

For Each MyPic In Me.Controls

If MyPic.GetType Is GetType(PictureBox) Then

MyPic.Image = Nothing

End If

Next

NextArrayNumber = 0

Me.Close()

End Sub

 
You've declared MyPic as type Object, so Intellisense will only show you the members of type Object. You should declare it as the most specific type you can, which in your case would be Control, and then cast it as the correct type to use it. Also, you should be Disposing the Image rather than just setting the property to Nothing, assuming you aren't using the same Image object elsewhere:
VB.NET:
For Each ctl As Control In Me.Controls
	If TypeOf ctl Is PictureBox Then
		DirectCast(ctl, PictureBox).Image.Dispose()
	End If
Next ctl
By performing the cast, the compiler knows that you want to treat ctl as a PictureBox so Intellisense will show you the members of the PictureBox class.

What you are doing is called "late-binding" and should be avoided if possible, as it makes your code run more slowly. I suggest you go to the Property Pages for the project and set Option Strict to On. This will prevent late-binding and also prevent most implicit conversions. You may well find that if you do this you will suddenly have lots of errors in your code. These are the places that, with Option Strict turned Off, could be compiled OK but cause an exception to be thrown at run time, or at the very least cause your code to run more slowly because it will continually checking the types of objects. These errors are all easily fixed by casting or converting the object in question to the correct type, using DirectCast, CType, CStr, CInt, ToString, etc.
 
Back
Top