Help with picturebox grid creating *AND ONCE AGAIN, ANOTHER NEW QUESTION*

How do I create a grid of pictureboxes (25 x 25) on Visual Basic 2005 and auto-name them pbx(row)_(column) for the picture boxes? It is to slow and hard to manually rename all 625 of them. Ok question answered, thanks.
New question:
OK, I ran into another problem. I want it be so that if I click the picture box with the person in it, you can move him by clicking on another tile. How do I do this? (I tried (got fromDustin_H's sample code) :


PrivateSub PicBox_On_Click(ByVal senderAsObject,ByVal eAs EventArgs)
If PicBox(x, y).Image = Tiles.picPerson.ImageThen
'BlaBlaBla
End If
End Sub


but it didn't work)
In other words: How can you tell VB to do something to the picture box the user clicked?
Thanks question answered.
Another question:
I am trying to let the user change all the Grass (frmTiles.picGrass.Picture) tiles to Dirt (frmTiles.picDirt.Picture) tiles, how do I do this?
And usingDustin_H's sample code (see page 3 of this topic), how do I tell vb where to put the grid of picture boxes (its always in the top left corner of the form)?
Another question:
(See post#39)
[1950 byte] By [YifanLu] at [2007-12-17]
# 1
Adding 625 controls to the Form could make the designer slow, and it is easy to mess it up. I think you should consider creating those controls dynamically, so they could be managed in one array.
LifengLuMS at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 2
Lifeng Lu MS wrote:
Adding 625 controls to the Form could make the designer slow, and it is easy to mess it up. I think you should consider creating those controls dynamically, so they could be managed in one array.

How? I am making a RPG game with a grid.
YifanLu at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 3

the individual above is right.... It may be slow.....

But believe it or not... I wrote something like this.

I'd like to think I'm getting better. However, this is the first .Net piece of code I ever wote and I don't have a clue how I did this. Yes I set some of the way's I do things in there. But goodness, I'm going to have to stare at this for a while...

Public Class Form1

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

Dim row As Integer
Dim column As Integer

For row = 0 To 24
For column = 0 To 24
addnewpicturebox(column, row, 20, 20)
Next
Next
addnewpicturebox(0, 0, 20, 20)
addnewpicturebox(10, 0, 20, 20)
End Sub

Private Sub addnewpicturebox(ByVal x As Integer, ByVal y As Integer, Optional ByVal Xbase As Integer = 0, Optional ByVal Ybase As Integer = 0)
Const cCellHeight As Integer = 10
Const cCellWidth As Integer = 10
Dim a As New ColumnEditor
Dim b As New System.Windows.Forms.picturebox
b = a.Getpicturebox
b.Enabled = True
b.BackColor = Color.White
Me.Controls.Add(b)
b.Left = (x * cCellWidth) + Xbase
b.Top = (y * cCellHeight) + Ybase
End Sub
End Class
Public Class ColumnEditor
Protected Const cCellHeight As Integer = 10
Protected Const cCellWidth As Integer = 10

Friend withevents tb As New System.Windows.Forms.picturebox

Public Function Getpicturebox() As System.Windows.Forms.PictureBox
Dim CellID As New DataDefinitions.CellDescriptor

tb.Visible = True
tb.Enabled = True
tb.Height = cCellHeight
tb.Width = cCellWidth
tb.BorderStyle = BorderStyle.None
'CellID.ColumnNumber()
'CellID.RowNumber()
Getpicturebox = tb
End Function

Private Sub tb_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles tb.Click

MsgBox("Hello", MsgBoxStyle.DefaultButton1, "Click Event")

End Sub


Public Sub New()

End Sub

Public Class DataDefinitions

Public Structure CellDescriptor
Public RowNumber As Integer
Public ColumnNumber As Integer
Public Selected As Boolean
Public character As Char
End Structure
End Class


End Class

ReneeC at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 4
How do I edit (program) the individual picture boxes? Say I want to change one of the picture box's color or picture. Also, since I'm making a rpg game, is there a easy way to make: when the user clicks on the picturebox containing the person and then click another picture box, the persion will teleport to that picturebox.
YifanLu at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 5
I didn't try, but the code provided by ReneeC is basically right. However, you can merge the code in ColumnEditor to your Form class, and remove both ColumnEditor and DataDefinitions. I don't see clear reason why they are needed.

I do think you want to keep those controls in an array, so you can access them. You can declare an array as a member of your form class:
Private m_PictureControls(24, 24) As MyPictureBox

In the AddNewPictureBox, you can do
m_PictureControls(x, y) = newPictureBox

After that, you could access the control in the array. You can use code like
AddHandler newPictureBox.Click, AddressOf xxx
to handle events as well.

You see why I use MyPictureBox here? You can create your own class which inherits from the PictureBox to add your own properties and functions. For example, you can add row, column information into it:

Public Class MyPictureBox
Inherits PictureBox
Public Column As Integer ' you might want to use Property, I just want to provide a simple sample...
Public Row As Integer
...
End Class

In this way, when you handle the events, you know where it comes from easily, right? You can also overrides functions in the PictureBox to handle events.

Frankly, I don't exactly know whether the performance of a form with so many controls will meet the requirement of a game. I personally will think to build one control which paints all of those pictures, of course, you need handle Mouse events and calculate where it is. It could be a little bit complex when you start to do it, but would give you the right performance for a game.

Thanks
Lifeng Lu MS

LifengLuMS at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 6
That helped, but I don't get some of it. I'm not an expert on .net (I am pretty good on VB6) so can someone give me an sample code or something?
YifanLu at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 7
you've received code.

How about asking specific questions?

ReneeC at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 8
Yes, I'm asking if there is a way to program one of the picturebox to do something when the user clicks on it.
YifanLu at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 9

If you look at the current code it is now declared as friend withevents.

The picture boxes produced by class column editor now has events.

Additionally, you now have a click event as an example. Its been added to the code.

Play with this in the debugger to learn by setting a break point in this routine, running the program and clicking on a random picture box. Also pay special attention to the arguments of the click event. Sender will be an object representation of the picture box that you clicked. "e" will have additional information.

Remember this is just one event of many, all accessible in the upper right hand window, when you select the Tb object in the columneditor class in the upper left window on the IDE.

ReneeC at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 10
How do I program the events of just one of the picture box? (again, I'm sorry for all this questions, and again I am new to .net)
YifanLu at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 11
if you'll notice the click event in the picture box class... that's an example of an event in the class.

You can any picture box event you want to the class. In any event you get data on the particular picture box from the sender argument, which is an object or the object in question.

Events are easy. :) !

ReneeC at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 12
How?
YifanLu at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 13
I've already added one event for you to the code that wasn't originally there.

Please see my post on 10/26.

ReneeC at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 14
I know, but that code is for what happens if you click on any of the picture boxes, I need a code that can program one of the picture box (like box# row 1, column 2).
YifanLu at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic General...