0

I doing my school project and i can't get multiple pictures from form 1 passing to form 2. More specifically, on form 1, users will click on any pictures they liked and on the next form, the picture will slowly show. I've tried to research but for most of the topic outside, they can only pass an image. This is the code i try to put down on every pictureBox click event. This will run but showing error when i try to click on picture.

Form1:

    private void pictureBox1_Click(object sender, EventArgs e)
    {
        pictureBox2.Visible = false;
        pictureBox3.Visible = false;
        PictureBox pb = pictureBox1 as PictureBox;
        Form2 f1 = new Form2(pb.Image, textBox1.Text);
        f1.Show();
    }

    private void pictureBox2_Click(object sender, EventArgs e)
    {
        pictureBox1.Visible = false;
        pictureBox3.Visible = false;
        PictureBox pb1 = pictureBox2 as PictureBox;
        Form2 f2 = new Form2(pb1.Image, textBox1.Text);
        f2.Show();
    }

    private void pictureBox3_Click(object sender, EventArgs e)
    {
        pictureBox2.Visible = false;
        pictureBox1.Visible = false;
        PictureBox pb2 = pictureBox3 as PictureBox;
        Form2 f3 = new Form2(pb2.Image, textBox1.Text);
        f3.Show();
    }

Form2:

public partial class Form2 : Form
{
    public Form2(Image pic, string username)
    {
        InitializeComponent();
        pictureBox1.Image = pic;
    }
}

Edit: This is the error when i tried to click on any picture on form 1, and the full detail of the error can be seen here

Shy.n Pham
  • 23
  • 5
  • You really don't show enough code (or describe your issue well enough) to give you an answer. Try adding more code and more detail. Remember, your reader doesn't have the context that you have about your problem – Flydog57 Jan 11 '20 at 00:24
  • Thanks for remind me, i have edit the code. – Shy.n Pham Jan 11 '20 at 00:57
  • @Shy.nPham, welcome to Stack Overflow! Could you edit the question to also provide the error message you are receiving when clicking on the picture? It would be very helpful for people willing to help. Thank you! – dandev486 Jan 11 '20 at 01:02
  • 1
    @Shy.nPham, since this is a school project, assuming that you are beginning with programming, so just a few tips that may or may not be helpful, feel free to judge. I see that you declare the reference to `Form2` as `f1`, `f2` and `f3`, but there is no need to name them differently (unless you want it to), since they are in different scopes, which are in this case the event handlers for your PictureBoxes. – dandev486 Jan 11 '20 at 01:32
  • 1
    You can also skip the `PictureBox pb = pictureBox1 as PictureBox;` line that declares another reference named `pb` to the `pictureBox1` and just pass the image directly to the `Form2`, something such as `Form2 form2 = new Form2(pictureBox1.Image, textBox1.Text);`, unless your intent was to achieve some sort of readability, in which case a possible approach could be declaring variables to store the image and the text themselves and send them as such `Form2 form2 = new Form2(image, text);`. – dandev486 Jan 11 '20 at 01:37
  • @danieldeveloper001 Thank you for the notice, I was complete an idiot when I forgot to copy my program into gg cloud. But when I tried the code on my personal laptop, it worked. P/s: I have created exactly like the one I did at my school an hour ago. Just the difference is the picture. – Shy.n Pham Jan 11 '20 at 01:42
  • @Shy.nPham, no problem, thank you for editing your question, just make sure to add the error message when you are able to, so that help comes faster! – dandev486 Jan 11 '20 at 01:55
  • Put the error message [in your question](https://idownvotedbecau.se/imageofanexception/), not on an external site. Then consult [What is a NullReferenceException](https://stackoverflow.com/questions/4660142/). – Dour High Arch Jan 13 '20 at 19:34

0 Answers0