For Each picturebox...

Trouble using For Each

Page 1 of 1

12 Replies - 11020 Views - Last Post: 17 March 2010 - 01:53 PM Rate Topic: -----

#1 chiefofgxbxl   User is offline

  • New D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 43
  • Joined: 24-June 09

For Each picturebox...

Posted 14 March 2010 - 08:27 AM

I have this code:
For Each pic As PictureBox In Form1.Controls
                If pic.Tag = 0 Then
                    TextBox7.Text += 0
                ElseIf pic.Tag = 1 Then
                    TextBox7.Text += 1
                ElseIf pic.Tag = 2 Then
                    TextBox7.Text += 2
                ElseIf pic.Tag = 3 Then
                    TextBox7.Text += 3
                ElseIf pic.Tag = 4 Then
                    TextBox7.Text += 4
                ElseIf pic.Tag = 5 Then
                    TextBox7.Text += 5
                ElseIf pic.Tag = 6 Then
                    TextBox7.Text += 6
                ElseIf pic.Tag = 7 Then
                    TextBox7.Text += 7
                ElseIf pic.Tag = 8 Then
                    TextBox7.Text += 8
                ElseIf pic.Tag = 9 Then
                    TextBox7.Text += 9
                ElseIf pic.Tag = 10 Then
                    TextBox7.Text += 10
                End If
            Next


It is supposed to pick all the pictureboxes on form 1 and add their tag to textbox7. Since the tags are integers going only up to 10, and other pictureboxes may have tags that I don't want to add, I decided to add If and ElseIf statements to cut out the ones I want to add. However, this crashes when I try to run it, because it says that 'object Combobox1 cannot be converted to picturebox'. How can I fix this code?

Is This A Good Question/Topic? 0
  • +

Replies To: For Each picturebox...

#2 CharlieMay   User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1729
  • View blog
  • Posts: 5,708
  • Joined: 25-September 09

Re: For Each picturebox...

Posted 14 March 2010 - 08:33 AM

You need to ensure it is a picturebox. Using TypeOf will allow you to perform that check
If TypeOf pic is picturebox then...
 if pic.tag...

end if

Was This Post Helpful? 0
  • +
  • -

#3 chiefofgxbxl   User is offline

  • New D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 43
  • Joined: 24-June 09

Re: For Each picturebox...

Posted 14 March 2010 - 08:38 AM

do I still keep the For Each Pic... stuff?
I added your bit to this code:
For Each pic As PictureBox In Form1.Controls
                If TypeOf pic Is PictureBox Then
                    If pic.Tag = 0 Then
                        TextBox7.Text += 0
                    ElseIf pic.Tag = 1 Then
                        TextBox7.Text += 1
                    ElseIf pic.Tag = 2 Then
                        TextBox7.Text += 2
                    ElseIf ... (ETC)
                    End If
                End If
            Next


but it still won't get the tags and add them to textbox7
Was This Post Helpful? 0
  • +
  • -

#4 CharlieMay   User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1729
  • View blog
  • Posts: 5,708
  • Joined: 25-September 09

Re: For Each picturebox...

Posted 14 March 2010 - 08:53 AM

Yes, because even though you are saying for each pic as PictureBox in Form1.Controls, you are not only getting pictureboxes. You are getting every control contained in form1. The As Picturebox is basically like Dimming the pic variable to picturebox so that you can use the picturebox properties. So, since you are iterating through each control and not all controls are pictureboxes, you have to enclose picturebox-specific properties inside the if TypeOf so that you don't try to use a property that is not valid.

This ensures that if you get to a commandbutton, or textbox it will skip it.
Was This Post Helpful? 0
  • +
  • -

#5 CharlieMay   User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1729
  • View blog
  • Posts: 5,708
  • Joined: 25-September 09

Re: For Each picturebox...

Posted 14 March 2010 - 09:02 AM

Are you getting any errors? One problem is that you are performing math against strings, a textbox hold the string representation of a number. When a textbox contains "6" it is not necessarily the value of 6. You would be better off assigning the calculations to a variable and then set the textbox to the result when everything is done calculating.

dim picTagValues as double

textbox7.text = cstr(picTagValues)

       Dim picValues As Double = 0
        For Each pic As Control In Me.Controls
            If TypeOf pic Is PictureBox Then
                If pic.Tag = 0 Then
                    picValues += 0
                ElseIf pic.Tag = 1 Then
                    picValues += 1
                ElseIf pic.Tag = 2 Then
                    picValues += 2
                ElseIf pic.Tag = 3 Then
                    picValues += 3
                ElseIf pic.Tag = 4 Then
                    picValues += 4
                ElseIf pic.Tag = 5 Then
                    picValues += 5
                ElseIf pic.Tag = 6 Then
                    picValues += 6
                ElseIf pic.Tag = 7 Then
                    picValues += 7
                ElseIf pic.Tag = 8 Then
                    picValues += 8
                ElseIf pic.Tag = 9 Then
                    picValues += 9
                ElseIf pic.Tag = 10 Then
                    picValues += 10
                End If
            End If
        Next
        TextBox7.Text = CStr(picValues)
    End Sub


But wait... There's more... :)
Since your tags always seem to be the same number you're calculating, you can use the full power of the for each statement to reduce your code tremendously by using the value of the tag
       Dim picValues As Double = 0
        For Each pic As Control In Me.Controls
            If TypeOf pic Is PictureBox Then
                picValues += pic.Tag
            End If
        Next
        TextBox7.Text = CStr(picValues) 


This post has been edited by CharlieMay: 14 March 2010 - 09:10 AM

Was This Post Helpful? 0
  • +
  • -

#6 chiefofgxbxl   User is offline

  • New D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 43
  • Joined: 24-June 09

Re: For Each picturebox...

Posted 14 March 2010 - 09:07 AM

well now I am just using letters because numbers are just confusing and they already resulted in getting the sum, rather than combining it into a string. with the letters however, they do appear in the bunch of tags in the textbox, but they are just out of order...when i used your code it still adds the values rather than getting a longer string.

*edit* now I am just getting a value of 37 if i do that, or another value if the tags of the pictureboxes are changed around.

Ok ok I almost have this, except now I'm assigning letters to the tags instead of numbers. If I used numbers, instead of a string I want of: 000118582 (that is just an example string), it would be 25, since it would add everything up. I got each picturebox to have its tag added to the textbox, but it is going out of order. i want picturebox1's tag to be added, followed by picturebox2's, but now it is just putting each in a random spot. Is there a way to organize this?

This post has been edited by chiefofgxbxl: 14 March 2010 - 09:33 AM

Was This Post Helpful? 0
  • +
  • -

#7 CharlieMay   User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1729
  • View blog
  • Posts: 5,708
  • Joined: 25-September 09

Re: For Each picturebox...

Posted 14 March 2010 - 09:15 AM

read prior post above.

Actually, let me re-clarify the above post

The textbox holds a string

so lets say the tags are 1, 2, 3, 4 for four of the pictureboxes

when you use textbox1.text = "1" + "2" + "3" + "4" you end up with a result of "1234"

You need to convert the strings to numbers which is why I showed the example above.

As far as you getting a result of all 0's means that somewhere you're not setting the tags to 0 for each picturebox you have.

This post has been edited by CharlieMay: 14 March 2010 - 09:19 AM

Was This Post Helpful? 0
  • +
  • -

#8 CharlieMay   User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1729
  • View blog
  • Posts: 5,708
  • Joined: 25-September 09

Re: For Each picturebox...

Posted 14 March 2010 - 09:36 AM

OK, I guess I assumed from your code you were wanting a total value. If you would have used TextBox7.Text &= "1" I would have realized it but TextBox7.Text += 1 appears to be an attempt to perform a sum of values. You should however you (&) when concatenating strings.

I seem to remember an issue when iterating through controls and the order they are processed. I'll see what I can find and post it if someone else doesn't jump in with an answer.
Was This Post Helpful? 0
  • +
  • -

#9 CharlieMay   User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1729
  • View blog
  • Posts: 5,708
  • Joined: 25-September 09

Re: For Each picturebox...

Posted 14 March 2010 - 09:52 AM

Well, the controls are processed in reverse inside the control array. There are ways to set their indexes, but if you're result is always showing up in reverse you could just reverse the string and place it back into the textbox. I would have to play around with it myself.
Was This Post Helpful? 1
  • +
  • -

#10 chiefofgxbxl   User is offline

  • New D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 43
  • Joined: 24-June 09

Re: For Each picturebox...

Posted 14 March 2010 - 12:37 PM

View PostCharlieMay, on 14 March 2010 - 08:52 AM, said:

Well, the controls are processed in reverse inside the control array. There are ways to set their indexes, but if you're result is always showing up in reverse you could just reverse the string and place it back into the textbox. I would have to play around with it myself.

yea i kinda noticed that. i say kinda because the beginning 5 or so were in reverse, so i reversed them, but then if you place the tag on the 6th one, it will appear in the middle, so it doesn't seem to follow the pattern. additionally, i placed a tag on the last few and it worked, but then if i place em a few more back they go in the middle, yet all my controls are numbered in order. is it based off of which were placed first? because they were out of order, so i renamed them.
Was This Post Helpful? 0
  • +
  • -

#11 CharlieMay   User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1729
  • View blog
  • Posts: 5,708
  • Joined: 25-September 09

Re: For Each picturebox...

Posted 16 March 2010 - 05:13 AM

You might look into Controls.SetChildIndex and see if you can control the order with that.

You might be able to set their index in the collections to where the for each pulls them in the order you wish.
Was This Post Helpful? 1
  • +
  • -

#12 chiefofgxbxl   User is offline

  • New D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 43
  • Joined: 24-June 09

Re: For Each picturebox...

Posted 17 March 2010 - 01:21 PM

View PostCharlieMay, on 16 March 2010 - 04:13 AM, said:

You might look into Controls.SetChildIndex and see if you can control the order with that.

You might be able to set their index in the collections to where the for each pulls them in the order you wish.


yes! thank you so much, this corrects the order which i want in all the controls. now the textbox is correctly formatted. thank you for helping with this persistent problem.
Was This Post Helpful? 0
  • +
  • -

#13 CharlieMay   User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1729
  • View blog
  • Posts: 5,708
  • Joined: 25-September 09

Re: For Each picturebox...

Posted 17 March 2010 - 01:53 PM

No problem, glad I could help. :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1