1

I've created a form with a PictureBox on it and would like to dynamically create another PictureBox on the form while the program runs (to the left of the static one). I've written this code:

Dim temp As PictureBox
temp = New PictureBox
temp.Image = StaticPictureBox.Image
temp.Visible = True
temp.Top = StaticPictureBox.Top
temp.Width = StaticPictureBox.Width
temp.Height = StaticPictureBox.Height
temp.Left = StaticPictureBox.Left - 20
temp.BringToFront()

When I run this code I can detect that the temp PictureBox does get created. However, it is not rendered onto the form. It seems like it's there but is invisible.

Does anyone have an idea of what I'm doing wrong?

3 Answers 3

4

You need to add it to the form's control collection:

Me.Controls.Add(temp)
Sign up to request clarification or add additional context in comments.

2 Comments

This makes a lot of sense but still doesn't work. I tried this and I see that temp gets added to the form's children. But, StaticPictureBox is not a child, it is a child of some Panel object that's a child of the form. So I tried this: code StaticPictureBox.Parent.Controls.Add(temp) I figured it would work, but didn't. Any ideas?
@SanderSmith Your code should work. I tested this on my code: PictureBox1.Parent.Controls.Add(temp) and it worked fine. I would suspect something is wrong with StaticPictureBox.
0

Why don't you just remove that code and place a picturebox next to the other one and set:

newpicturebox.visible = false

Then whenever you have the action completed you have it change:

newpicturebox.visible = true

1 Comment

That works for this simplified case, but what I really need to do is create them dynamically/programmatically. I just tried to make the case simple so you wouldn't need to read 2 pages of the entire program description.
0

I know this is old but... you got an error here:

temp.Left = StaticPictureBox.Left - 20 

should be:

temp.Left = StaticPictureBox.right + 20

or:

temp.Left = StaticPictureBox.right

hope it helped.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.