Answered by:
Group a bunch of picture boxes together?

Question
-
I want to group a bunch of pictures boxes together so instead of calling each picture box and changeing its background color for each one, I want to just make a call to one grouped object and set all their background color at once. I know there is a easy way to do it I just can't remember for some reason.Friday, May 31, 2013 6:56 AM
Answers
-
Dim PBs() As PictureBox = {Rail_1, Rail_2, Rail_3, Rail_4, Rail_5, Rail_6, Rail_7, Rail_8, Rail_8, Rail_9, Rail_10}
For index As Integer = 1 To 10
PBs(index - 1).BackColor = Color.Black
Next
- Marked as answer by mholmes_3038 Friday, May 31, 2013 11:35 PM
- Edited by JohnWein Saturday, June 1, 2013 12:04 AM
Friday, May 31, 2013 8:24 PM
All replies
-
for each textb in TheGroupBox.Controls if TypeOf textb is Textbox then textb.Backcolor = color.Green end if end for
Maybe you should for this question use internet.
I think that alone in this forum it is about 5000 times.
Be aware I typed the code above in this message so watch typos.
Success
Cor- Proposed as answer by newbieinVB Friday, May 31, 2013 4:57 PM
Friday, May 31, 2013 7:01 AM -
Hey it worked for me, except for "end for" I put Next.
You've taught me everything I know but not everything you know.
Friday, May 31, 2013 7:28 AM -
yeah u need a next cause its a loop lol yeah ill try it thank you.Friday, May 31, 2013 3:01 PM
-
oo wait this reffers to a group box. I have a bunch of picture boxes not group boxes.Friday, May 31, 2013 3:04 PM
-
Ok so here is what Im attempting to shorten up
Rail_1.BackColor = Color.Black Rail_2.BackColor = Color.Black Rail_3.BackColor = Color.Black Rail_4.BackColor = Color.Black Rail_5.BackColor = Color.Black Rail_6.BackColor = Color.Black Rail_7.BackColor = Color.Black Rail_8.BackColor = Color.Black Rail_9.BackColor = Color.Black Rail_10.BackColor = Color.Black For index As Integer = 1 To 10 Rail_(index).backcolor = Color.Black Next
For loop gives error obvious reasons but that is what I'm attempting to do in a nutshell.Friday, May 31, 2013 3:12 PM -
oo wait this reffers to a group box. I have a bunch of picture boxes not group boxes.
Put the PictureBoxes in a GroupBox, and in Cor's code change TextBox ro PictureBox.
If, for some reason, you don't want to put the PictureBoxes in a GroupBox, create an Array of PicureBox that contains all the PictureBoxes you want to group together.
Friday, May 31, 2013 3:15 PM -
Problem is they are already drawn to the GUI. So i guess there is not way unless I created them via code instead of just using GUI placement. Owell no biggie, I figured I'd run into this problem.Friday, May 31, 2013 3:55 PM
-
Maybe this?
Public Class Form1 Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Dim cControl As Control For i = 1 To 4 For Each cControl In Me.Controls If cControl.Name = "PictureBox" & i.ToString Then cControl.BackColor = Color.Turquoise End If Next cControl Next End Sub End Class
You've taught me everything I know but not everything you know.
- Edited by Mr. Monkeyboy Friday, May 31, 2013 5:14 PM
Friday, May 31, 2013 4:45 PM -
Mholes.
Do u understand cor is talking about? Or u do not noe what he is trying to show? If u place ur picture box in a panel or group panel or flowlayoutpanel . By using for each loop.
u are able to get all the control in the panel. The if else statement is to check if it is picture box. If is a picture box. U can do what ever u wan.in this case ,he put control name as textb, but u can name it to anything u wan. Itempicturebox. Example : for each itempicturebox in group panel.controls. Next
he is using a text box as a example. So ur if else statement will be picture box instead of a text boxFriday, May 31, 2013 4:56 PM -
Hi,
If you only have these 10 pictureboxes placed on your form then you can use Button1 code. If you have other pictureboxes on the form and you want to change only the ones that begin with the name (Rail_) then you can use Button2 code.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click For Each pb As PictureBox In Me.Controls.OfType(Of PictureBox)() pb.BackColor = Color.Black Next End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click For Each pb As PictureBox In Me.Controls.OfType(Of PictureBox)() If pb.Name.StartsWith("Rail_") Then pb.BackColor = Color.Black Next End Sub
Friday, May 31, 2013 6:29 PM -
Dim PBs() As PictureBox = {Rail_1, Rail_2, Rail_3, Rail_4, Rail_5, Rail_6, Rail_7, Rail_8, Rail_8, Rail_9, Rail_10}
For index As Integer = 1 To 10
PBs(index - 1).BackColor = Color.Black
Next
- Marked as answer by mholmes_3038 Friday, May 31, 2013 11:35 PM
- Edited by JohnWein Saturday, June 1, 2013 12:04 AM
Friday, May 31, 2013 8:24 PM -
Problem is they are already drawn to the GUI. So i guess there is not way unless I created them via code instead of just using GUI placement.
You don;t have to create them in code to include them in a collection. Just add them to the collection in code by name.
Dim PictureBoxes As New List(Of PictureBox)
PictureBoxes.Add(PictureBox1)
PictureBoxes.Add(PictureBox2)and so on. Then change all the colors in a loop. Creating this list will be worthwhile for reasons other than changing the backcolor.
Friday, May 31, 2013 10:00 PM -
Thanks guys for all the suggestions/help above. This should cut down on the size and clean it up.Friday, May 31, 2013 11:36 PM