Click here to Skip to main content
15,882,278 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a program with 16 blank picturebox's on the main form, when a user clicks one box it opens another form (form2) with 17 picturebox's each with a specific image, I want the specific image to go into the picturebox on the main form.

I used this code on form2 for clicking on one of the 17 picturebox's

With Mainform.picturebox1
     .Image = My.Resources._apicture01
     End With

However when I click on picturebox2 on the main form it will add the picture to Mainform.picturebox1 again.

I need a way of having it figure out which of the 16 picturebox's on the main form was pressed, then it adds the specific image to that picturebox.

What I have tried:

I was going to use a boolean variable on the mainform have it true depending on what picturebox was clicked, but then I would need to use 272 if statements on form2. (16x17) I thought there must be an easier way?
Updated 4-Oct-19 2:56am

1 solution

Either create a Form2 constructor that accepts an Image instance, and display it, or provide an Image Property, and pass the Image to the newly constructed form via that.

Form1 can know about Form2 - it has to, it displays it - but Form2 should not know anything about Form1, it can;t even be sure that Form1 displayed it!

See here: Transferring information between two forms, Part 1: Parent to Child[^] - teh code is in C#, but it's pretty obvious.
Share this answer
funkymunkey99 4-Oct-19 9:44am    
HiI don't really understand that, I'm just a beginner :)
OriginalGriff 4-Oct-19 9:57am    
You can;t be that much of a beginner and be writing Windows Forms apps!

You know what a constructor is?
You know what a Property is?

Or at least I assume you do ...
funkymunkey99 4-Oct-19 10:08am    
I can do very basic things, I'm only using VB Community 2019 as a hobby, I don't make many applications and only for myself to use.
No don't know what a constructor is
Is a properly a part of a component like button1.text

Even the code I showed was from an example online I found!
OriginalGriff 4-Oct-19 10:29am    
If you don't know those basics, then you are trying to run before you can walk (or more accurately, trying to win a F1 race before you learn to drive!)

Seriously, even as a hobby, you need to learn this stuff properly - if you don't you'll never understand how to do it yourself, or why the code you showed us is so poor in every way.
Get a book. Wrox do some good ones, as do Addison Wesley and Microsoft Press (or get this and learn C# instead of VB:
It's free, and pretty good at the basics (though the Wrox, AD, and MSP ones will go a lot further into the capabilities of .NET they are pretty expensive - but some libraries carry them).

You need the basics pretty thoroughly, and that does include constructors, properties, events, delegates, lambdas, and so forth. Without them, you'll never even know what was available that might have made your life a whole load easier.
Think about it: yes, you can learn to drive by stealing a car and mashing the pedal to the metal - but that doesn't teach you what speed to go round a corner, or what to do when a bus load of nuns turns into your path. :laugh:
And it means that when you do get your own car, it's going to get very expensive in repairs, very quickly!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900