-
Jan 12th, 2015, 11:01 AM
#1
Thread Starter
New Member
Array of PictureBoxes (Error)
Code:
Public Sub picBoard_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles picBoard00.Click, _
picBoard01.Click, picBoard02.Click, picBoard03.Click, picBoard04.Click, picBoard05.Click, picBoard06.Click, picBoard07.Click, _
picBoard10.Click, picBoard11.Click, picBoard12.Click, picBoard13.Click, picBoard14.Click, picBoard15.Click, picBoard16.Click, picBoard17.Click, _
picBoard20.Click, picBoard21.Click, picBoard22.Click, picBoard23.Click, picBoard24.Click, picBoard25.Click, picBoard26.Click, picBoard27.Click, _
picBoard30.Click, picBoard31.Click, picBoard32.Click, picBoard33.Click, picBoard34.Click, picBoard35.Click, picBoard36.Click, picBoard37.Click, _
picBoard40.Click, picBoard41.Click, picBoard42.Click, picBoard43.Click, picBoard44.Click, picBoard45.Click, picBoard46.Click, picBoard47.Click, _
picBoard50.Click, picBoard51.Click, picBoard52.Click, picBoard53.Click, picBoard54.Click, picBoard55.Click, picBoard56.Click, picBoard57.Click, _
picBoard60.Click, picBoard61.Click, picBoard62.Click, picBoard63.Click, picBoard64.Click, picBoard65.Click, picBoard66.Click, picBoard67.Click, _
picBoard70.Click, picBoard71.Click, picBoard72.Click, picBoard73.Click, picBoard74.Click, picBoard75.Click, picBoard76.Click, picBoard77.Click
Dim picBoard As PictureBox = sender
Static board(7, 7) As PictureBox
For row As Integer = 0 To 7
For col As Integer = 0 To 7
picBoard.Tag(row, col) = board(row, col)
Next
Next
End Sub
I'm trying to assign a 8x8 grid of PictureBoxes to a two-dimensional array. When I run the program and click on a picturebox to execute the code, I get this error.
"Overload resolution failed because no accessible 'Chars' accepts this number of arguments."
On the " picBoard.Tag(row, col) = board(row, col)" line.
Any help?
Last edited by dday9; Jan 12th, 2015 at 11:59 AM.
Reason: Changed quote tags to code tags
-
Jan 12th, 2015, 11:23 AM
#2
Re: Array of PictureBoxes (Error)
The array is on the picture boxes... not the tag property even still, PicBoard is just an instance of a SINGLE picture box. Also you say you're trying to assing a grid of picture boxes to a 2-dimentional array, but the code doesn't do that... so I'm still not sure what the intent here is.
-tg
-
Jan 12th, 2015, 12:01 PM
#3
Re: Array of PictureBoxes (Error)
Another point is that you are doing things in that code that are decidedly dicey. If you turn Option Strict ON, as you should, you'll have lots of things that need to be cleaned up, but those things REALLY need to be cleaned up. If you had put a 2D array into the tag property of each picturebox, then the code you have might work, but only because you'd be using some implicit casting that is unreliable. I don't see any reason for you to put a 2D array into the tag property of each picturebox, though, so perhaps you did not, in which case the implicit cast is just an error. With Option Strict OFF, there is no problem making mistakes like that, you just get to find them at runtime rather than during development time, which is one of the reasons that Option Strict OFF is a bad idea (the other is that implicit conversions are slower than explicit conversions).
My usual boring signature: Nothing
-
Jan 12th, 2015, 09:26 PM
#4
Thread Starter
New Member
Re: Array of PictureBoxes (Error)
Each picturebox has a tag like this : 0,0 0,1 0,2 and so on corresponding to it's place on the grid.
And since this code doesn't look like it's doing what I'm trying to do, how would I go about adding these pictureboxes to the array? And thanks for the fast replies, sorry for the late response.
-
Jan 12th, 2015, 11:02 PM
#5
Re: Array of PictureBoxes (Error)
Originally Posted by koref
Each picturebox has a tag like this : 0,0 0,1 0,2 and so on corresponding to it's place on the grid.
If its a grid then you shouldn't even need to store anything in the Tag property, and can just do the math to get its location in the grid, something like,..
Code:
Public Class Form1
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
' create a 8x8 grid of pic boxes, add to form.
For row As Integer = 0 To 7
For col As Integer = 0 To 7
Dim pb As New PictureBox
pb.BorderStyle = BorderStyle.FixedSingle
pb.BackColor = Color.White
pb.Size = New Size(30, 30)
pb.Location = New Point(col * pb.Size.Width, row * pb.Size.Height)
AddHandler pb.Click, AddressOf PictureBox_Click
Me.Controls.Add(pb)
Next
Next
End Sub
Private Sub PictureBox_Click(sender As Object, e As EventArgs)
Dim pb = DirectCast(sender, PictureBox)
' show what column and row picbox was clicked.
MessageBox.Show(String.Format(" Col:{0}, Row:{1}", pb.Location.X \ pb.Size.Width, pb.Location.Y \ pb.Size.Height))
End Sub
End Class
Last edited by Edgemeal; Jan 12th, 2015 at 11:05 PM.
-
Jan 13th, 2015, 09:22 AM
#6
Re: Array of PictureBoxes (Error)
A the 'tag' property is an object. What type of object is it?
"Ok, my response to that is pending a Google search" - Bucky Katt.
"There are two types of people in the world: Those who can extrapolate from incomplete data sets." - Unk.
"Before you can 'think outside the box' you need to understand where the box is."
-
Jan 13th, 2015, 10:09 AM
#7
Thread Starter
New Member
Re: Array of PictureBoxes (Error)
I probably should provide some context for the grid.
I'm trying to make reversi, and having the pictureboxes in an array and being able to traverse it would make the rest of the code easier.
And what do you mean by what type of object is it?
I can also individually assign each picturebox to the array, but I feel as though that's not how I should do it...
-
Jan 13th, 2015, 10:53 AM
#8
Re: Array of PictureBoxes (Error)
You can put anything you want into the .Tag property. You appear to have been putting a string that consisted of two numbers separated by a comma into the .Tag property. That would work, but it wouldn't be particularly efficient. You couldn't access that property with .Tag(x,y) as you were doing, though, because the .Tag isn't a 2D array, it's just a pair of values in a string. So, what you would have to do (and it's not a good idea) is to take the string from the .Tag property and turn it back into two numbers:
dim temp() As String = .Tag.ToString.Split(","c)
The two elements in temp could then be converted to integers and used as indexes into the array. It would be more efficient to use a Drawing.Point structure to hold the two elements, as no splitting or converting to integer would be needed. There would be a conversion from object to Drawing.Point, but that would be cheap.
Frankly, I prefer the solution Edgemeal used, though I suspect that the Drawing.Point is a more efficient solution, since it avoids the math in favor of a simple DirectCast().
It's only part of an answer, though, because in your original snippet, you were assigning to an apparent array in the .Tag property of the picturebox. I can understand the picturebox .Tag property holding a value, or a point, but I don't know what your expectation was with the way you were using it.
My usual boring signature: Nothing
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|