Do something to all pictureboxes!

Cheetaiean

Member
Joined
Jan 20, 2014
Messages
13
Programming Experience
Beginner
I have code where I have assigned a specific Tag to certain pictureboxes.
What I want is for all picture boxes whose tag is equal to "Tree" to do something.
I have tried:

VB.NET:
If PictureBox.Tag = "Tree" Then            imgSki.Top = 10


        End If

Note that this works if it were
VB.NET:
imgSki.Tag
, but PictureBox does not count as an object or something. If someone could find a way to make this work it would be great.

For those who don't understand what i'm trying to do: something along the lines of :
If the tag of a picturebox is equal to "Tree" Then do this.
 
The simplest way to do what you want is with a bit of LINQ:
For Each pb In Me.Controls.OfType(Of PictureBox)().Where(Function(p) CStr(p.Tag) = "Tree")
    'Use pb here.
Next
Here's how that would be done without LINQ:
For Each ctrl As Control In Me.Controls
    Dim pb As PictureBox = TryCast(ctrl, PictureBox)

    If pb IsNot Nothing AndAlso CStr(pb.Tag) = "Tree" Then
        'Use pb here.
    End If
Next
Note that that assumes that the PictureBoxes are on the form itself. If they are on a different parent then you must use the Controls collection of that parent. If you want PictureBoxes from several parents then you need to do a bit more work.
 
That seems to be what i want, thank you!

Now, is there a way to apply that to intersecting with my main picturebox (which doesn't have tag tree, btw).
So, ex.
If imgSki.Bounds.IntersectsWith the pb that meets the conditions (isnot Nothing ANdAlso CSTR(pb.Tag) = "Tree" Then
Collision = True

That would be exactly what I need.
 
Using a tag

The syntax for using a tag requires "is" not the equal sign. Here are 2 examples:

VB.NET:
	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		For Each ctl As Control In Controls
			If TypeOf ctl Is TextBox Then
				Dim txt As TextBox = CType(ctl, TextBox)
				If txt.Tag Is "c" Then
					txt.Clear()
				End If
			End If
		Next
	End Sub

	Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
		For Each ctl As Control In Controls
			If TypeOf ctl Is CheckBox Then
				Dim chk As CheckBox = CType(ctl, CheckBox)
				If chk.Tag Is "c" Then
					chk.Checked = True
				End If
			End If
		Next
	End Sub
 
Great, thanks for the tip about 'Is', but how do you make:

Randomimage.Bounds.IntersectsWith (the condition [so all the fancy code you guys provided]).

Because basically I want my main sprite to intersectwith the picture boxes who have a tag of Tree, and then do something.
 
The syntax for using a tag requires "is" not the equal sign. Here are 2 examples:

VB.NET:
	Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
		For Each ctl As Control In Controls
			If TypeOf ctl Is TextBox Then
				Dim txt As TextBox = CType(ctl, TextBox)
				If txt.Tag Is "c" Then
					txt.Clear()
				End If
			End If
		Next
	End Sub

	Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
		For Each ctl As Control In Controls
			If TypeOf ctl Is CheckBox Then
				Dim chk As CheckBox = CType(ctl, CheckBox)
				If chk.Tag Is "c" Then
					chk.Checked = True
				End If
			End If
		Next
	End Sub

That's not true about how to use the Tag property. The Is operator tests referential equality on reference types but that's not what we're interested in here. What we care about is whether two Strings contain the same characters, not whether two references refer to the same String object. We want value equality, not referential equality, and that's done with the = operator. Using your code, it would be quite possible for the two Strings to be "equal" and yet that code not detect it. That probably wouldn't be the case if the Tag was set with a literal in the first place but the code should not rely on that.
 
Great, thanks for the tip about 'Is', but how do you make:

Randomimage.Bounds.IntersectsWith (the condition [so all the fancy code you guys provided]).

Because basically I want my main sprite to intersectwith the picture boxes who have a tag of Tree, and then do something.

As I posted in my code examples:
Use pb here.
If you want to know whether the Bounds of 'pb' intersects with the Bounds of some other control then you get the Bounds of each, call IntersectsWith on one of the Rectangles and pass the other as an argument.
 
Thanks a lot; you're awesome jmcilhinney! :D Everything works now!

I did what you said, I just forgot to add .Bounds to the pb. Once again, thanks!
 
Back
Top