|
-
Aug 19th, 2014, 04:58 PM
#1
Thread Starter
Hyperactive Member
[RESOLVED] Only Last Dynamic Picture Box Showing
Good Evening,
I'm creating a number of Picture Boxes dynamically in a Tab Control, however once the code runs only the last Picture Box is displayed?
Code:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim NewTab As New TabPage
Dim NewPic As New PictureBox
Dim lngX As Long
Dim lngY as Long
NewTab.Name = "New Tab" & CStr(Count)
NewTab.Text = NewTab.Name
Count += 1
NewPic.Image = Image.FromFile("C:\My Pictures\Picture.jpg")
NewPic.Size = New Size(200, 200)
Me.TabControl1.Controls.Add(NewTab)
lngX = 0
lngY = 0
For x = 1 To 2
NewPic.Location = New System.Drawing.Point(lngX, lngY)
NewTab.Controls.Add(NewPic)
lngX += 210
lngY += 0
Next
Me.TabControl1.SelectedIndex = Me.TabControl1.TabCount - 1
End Sub
Only the last Picture Box is displayed, when the above is run.
Any ideas as to why the first one isn't created/displayed?
Many Thanks & Regards
-
Aug 19th, 2014, 06:30 PM
#2
Re: Only Last Dynamic Picture Box Showing
you only declared one NewPic that you tried to add twice
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Aug 19th, 2014, 06:39 PM
#3
Re: Only Last Dynamic Picture Box Showing
You are only creating one picturebox. {edit: see .paul. jumped in as well}
You are adding two references to it in the Controls array, which I don't know if both references are actually added to the collection or not.
You need to create two pictureboxes.
I'm just going to modify your code a bit, but not test it as I have to leave, but hopefully it will be correct.
Code:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim NewTab As New TabPage
Dim NewPic As PictureBox 'Don't need new here, you will create them in the loop below
Dim lngX As Long
Dim lngY as Long
NewTab.Name = "New Tab" & CStr(Count)
NewTab.Text = NewTab.Name
Count += 1
Me.TabControl1.Controls.Add(NewTab)
lngX = 0
lngY = 0
Dim Pic as Image = Image.FromFile("C:\My Pictures\Picture.jpg") 'the two pictureboxes will share the same image
For x = 1 To 2
newPic = new Picturebox() 'create an instance of a picturebox
NewPic.Size = New Size(200, 200)
NewPic.Image = Pic
NewPic.Location = New System.Drawing.Point(lngX, lngY)
NewTab.Controls.Add(NewPic)
lngX += 210
lngY += 0
Next
Me.TabControl1.SelectedIndex = Me.TabControl1.TabCount - 1
End Sub
I'm not sure why you don't have to set the Visible property of the pictureboxes to True, perhaps because it is part of the Tab Control.?
-
Aug 19th, 2014, 06:49 PM
#4
Thread Starter
Hyperactive Member
Re: Only Last Dynamic Picture Box Showing
Thanks for the replies.
I thought this might be the case and now it's be pointed out it makes sense.
However, I've modified my code to create the instance of the Picturebox in the loop, as per the reply from passel but still only the last one is created/shown once the run?
Regards
-
Aug 19th, 2014, 07:00 PM
#5
Thread Starter
Hyperactive Member
Re: Only Last Dynamic Picture Box Showing
Further to my last reply I've even tried giving the new Picturebox a unique name each time, but still it doesn't create/show the first one.
Code:
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles Button1.Click
Dim NewTab As New TabPage
Dim NewPic As PictureBox
Dim lngX As Long
Dim lngY as Long
NewTab.Name = "New Tab" & CStr(Count)
NewTab.Text = NewTab.Name
Count += 1
NewPic.Image = Image.FromFile("C:\My Pictures\Picture.jpg")
NewPic.Size = New Size(200, 200)
Me.TabControl1.Controls.Add(NewTab)
lngX = 0
lngY = 0
For x = 1 To 2
NewPic = New PictureBox()
NewPic.Name = "Art" + CStr(x)
NewPic.Location = New System.Drawing.Point(lngX, lngY)
NewTab.Controls.Add(NewPic)
lngX += 210
lngY += 0
Next
Me.TabControl1.SelectedIndex = Me.TabControl1.TabCount - 1
End Sub
Regards
-
Aug 19th, 2014, 07:28 PM
#6
Re: Only Last Dynamic Picture Box Showing
this works. I think the problem was because you were trying to assign property values to an uninitialized object variable:
Code:
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim NewTab As New TabPage
Dim NewPic As PictureBox
Dim intX As Integer
Dim intY As Integer
'Dim count As Integer = 3
NewTab.Name = "New Tab" & CStr(Count)
NewTab.Text = NewTab.Name
Count += 1
Me.TabControl1.Controls.Add(NewTab)
intX = 0
intY = 0
For x = 1 To 2
NewPic = New PictureBox()
NewPic.Image = Image.FromFile("C:\My Pictures\Picture.jpg")
NewPic.Size = New Size(200, 200)
NewPic.Name = "Art" + CStr(x)
NewPic.Location = New Point(intX, intY)
NewTab.Controls.Add(NewPic)
intX += 210
Next
Me.TabControl1.SelectedIndex = Me.TabControl1.TabCount - 1
End Sub
End Class
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Aug 19th, 2014, 07:37 PM
#7
Thread Starter
Hyperactive Member
Re: Only Last Dynamic Picture Box Showing
Paul,
Many thanks!
Much appreciated.
Regards
-
Aug 19th, 2014, 08:55 PM
#8
Re: Only Last Dynamic Picture Box Showing
Just for your information, now that I'm home I tried the code I posted in post #3 and it also worked fine.
Got a new tab page, with two pictureboxes side by side (10 pixel gap between them) with the same image in each.
Because I have Option Strict On, I did change lngX and lngY to Integer, since System.Drawing.Point takes Integers as input, and not Longs.
With Option Strict Off, VB is implicitly converting those to Integer, but you should declare the variables with the correct type to start with.
In fact, I would just declare a point variable to begin with, so you don't have to create the point variable later.
Code:
Dim picLoc As New Point(0, 0) 'Start at 0,0
'...
NewPic.Location = picLoc
picLoc.X += 210
'...
A key difference between the two sets of code is in your case you're loading two copies of the bitmap from the disk, each picturebox gets its own copy, and in my code, it only loads the bitmap once, in a local variable then sets both picturebox images to that, so you're using 1/2 the memory since you only have one copy of the bitmap in memory that both pictureboxes reference.
Whether you want each picturebox to have its own copy depends on what you are going to be doing with the pictureboxes and images.
If you were doing something like a chess game, where you had eight pawns of a color, which are all the same, there would be no reason to have to load eight copies of the pawn bitmap, assigning one to each bitmap as that would be a waste of resources. You would only load the bitmap once and assign that to each pawn piece, so all eight pieces have the same reference value (usually a 4 or 8 byte value that "points" to the image), rather each having the 100s or 1000s of bytes necessary for each to have a their own copy of the image.
Last edited by passel; Aug 19th, 2014 at 09:06 PM.
Tags for this Thread
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
|