Click here to Skip to main content
15,395,257 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have been looking for different solutions and when I finally thought I had it, an "Unhandled Exception" appears. I am a student and I am just starting out and I wanted to see if you could offer me alternatives.

The problem is the following:

"Design a program that determines the amount to be paid and the preview gift for a customer of the shop.

In addition, when the form is executed, it must show a photo of a client, and then when you click
The photo must show a photo of another client up to 3 photos."

What I have tried:

This is the code that I have been doing but I keep getting "unhandled exception"
C#
private void Form1_Load(object sender, EventArgs e)
        {
            picfoto.Image = Image.FromFile("Foto1.jpg");
            a = 1;
        }

 private void picfoto_Click(object sender, EventArgs e)
        {
            switch (a)
            {
                case 1:
                    picfoto.Image = Image.FromFile(Application.StartupPath + "Foto2.png");
                    a = a + 1;
                    break;
                case 2:
                    picfoto.Image = Image.FromFile(Application.StartupPath + "Foto3.png");
                    a = a + 1;
                    break;
                case 3:
                    picfoto.Image = Image.FromFile(Application.StartupPath + "Foto4.png");
                    a = 1;
                    break;

            }
Posted
Updated 8-Mar-22 9:45am
v2
Comments
PIEBALDconsult 8-Mar-22 14:16pm
   
Well, what is the Exception which is unhandled?
Estephany Enciso Chang 8-Mar-22 14:20pm
   
the code executes perfectly fine until it arrives:

picphoto.Image = Image.FromFile(Application.StartupPath + "Photo1.jpg");

i got rid of the "Application.StartupPath + " and it runs fine i think that is the problem

1 solution

It is safer to use:
Path.Combine(Application.StartupPath, "Foto2.png")

It might also be needed to dispose of the PictureBox Image, see answer here:
c# - Dispose of a pictureBox image without losing the pictureBox - Stack Overflow[^]
   
v2

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