Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with displaying picturesboxes at runtime 1

Status
Not open for further replies.

angelpro

Programmer
Dec 20, 2011
6
0
0
Hello, I am new to VB.NET 2005 and what I am trying to do is to create an array of picture boxes at runtime and to display them to the form. Here is my code I have tried so far. If I run it step by step, when the first item of the array is showed on the form the counter increases at 1 then next item appears normally but the previous item disappears. Any help will be much appreciated. Thank you in advanced.

Private Sub loadqueens()
Dim pics(2) As PictureBox
Dim cnt As Integer = pics.Length
Dim i As Integer
Dim pb As New PictureBox
For i = 0 To cnt - 1
pics(i) = New PictureBox
pics(i) = pb
Next
i = 0


For i = i To cnt - 1
pics(i).Parent = Me
pics(i).Image = My.Resources.black_king_2d_icon
x = x + pics(i).Width
y = y

pics(i).Location = New System.Drawing.Point _
(x, y)


MsgBox(pics(i).Location.X)

pics(i).Visible = True
Me.Controls.Add(pics(i))
Next


End Sub
 
The problem is the line marked in red below. Remove that line and the code works.

Private Sub loadqueens()
Dim pics(2) As PictureBox
Dim cnt As Integer = pics.Length
Dim i As Integer
Dim pb As New PictureBox
For i = 0 To cnt - 1
pics(i) = New PictureBox
[red]pics(i) = pb <--------------REMOVE THIS LINE[/red]
Next
i = 0


For i = i To cnt - 1
pics(i).Parent = Me
pics(i).Image = My.Resources.black_king_2d_icon
x = x + pics(i).Width
y = y

pics(i).Location = New System.Drawing.Point _
(x, y)


MsgBox(pics(i).Location.X)

pics(i).Visible = True
Me.Controls.Add(pics(i))
Next


End Sub


The problem is that each time you use pics(i) = pb, you are not using a new picturebox in the array, but reusing a single object. Since a single object can't be in more than one place at a time, you get the behavior you've been seeing.


I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
Thank you so much. I don't believe it was something so simple.
I thought it could work with pics(i)=pb because I wanted every item of the array to be each one the same object as pb.
But I can work for me in your way too just only with some lines of code about some values of array item's properties.
Just a question only, as an alternative solution, Would I have problem too, if I would try pb passing it to the function as parameter by refference and using it to my code too as pics(i)=pb or would it be a bad idea? Thank you very much in advanced.
 

The only way you could do it is like so:

For i = 0 To cnt - 1
pb = New PictureBox
pics(i) = pb
Next

This is the same as doing what you have now:

For i = 0 To cnt - 1
pics(i) = New PictureBox
Next

Except you're adding an extra step with the code on top. You can have the same object as every element in an array, but that object can only be at one location on your form at a time.


I used to rock and roll every night and party every day. Then it was every other day. Now I'm lucky if I can find 30 minutes a week in which to get funky. - Homer Simpson

Arrrr, mateys! Ye needs ta be preparin' yerselves fer Talk Like a Pirate Day!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top