0

I am trying to take a value from a user then every click on the button it must print statement and a picture that represent a number of trying. The if statement is working but it always shows the pic of last case.

private void button1_Click(object sender, EventArgs e)
{
    if (textBox1.Text != "")
    {    
        int gussednum= Convert.ToInt16( textBox1.Text); 

        for (int i = 0; i < 7; ++i)
        {
                if (interednum < num)
                {
                    label1.Text = "Should be greater than" + num;
                    switch (i)
                    {
                        case (0) :
                            Image image = Image.FromFile("1.png");
                            pictureBox1.Image = image;
                            break;

                        case (1):
                            Image im2 = Image.FromFile("2.png");
                            pictureBox1.Image = im2;
                            break;

                        case (2):
                            Image im3 = Image.FromFile("3.png");
                            pictureBox1.Image = im3;
                            break;

                        case (3):
                            Image image4 = Image.FromFile("4.png");
                            pictureBox1.Image = image4;
                            break;

                        case (4):
                            Image image5 = Image.FromFile("5.png");
                            pictureBox1.Image = image5;
                            break;

                        case (5):
                            Image image6 = Image.FromFile("dead.gif");
                            pictureBox1.Image = image6;
                            break;

                        case (6):
                            Image image7 = Image.FromFile("red.png");
                            pictureBox1.Image = image7;
                            break;
                    }
                }
4
  • 2
    What interednum and num are? Can you provide the code were you assign them? Commented Nov 3, 2013 at 20:27
  • 2
    That's a pretty gross misuse of a switch statement. Where are interednum and num defined? Commented Nov 3, 2013 at 20:27
  • i assigned num to a random number static Random R = new Random(); int num = R.Next(100); Commented Nov 3, 2013 at 20:30
  • and I take the interednum from the textBox ! Commented Nov 3, 2013 at 20:31

3 Answers 3

1

You always assign an image to pictureBox1. And obviously the last time in the loop the last switch/case is executed (#7)

pictureBox1.Image = image5;

So, you either mistyped pictureBox1 on copy/pasting, or you should break a loop as well (from your switch/case break).

I am not sure if that's even StackOverflow question but just some copy/paste issue.

Sign up to request clarification or add additional context in comments.

Comments

0

Since you've wrapped the switch/case with this for loop:

for (int i = 0; i < 7; ++i)

Whenever, the following is true:

interednum < num

You'll always have i equal to 6 because it loops to it every run. This is why it'll always output the last case.

This may or may not be an issue, but worth looking at. It seems you always change the same object (pictureBox1):

pictureBox1.Image = image7;

You may have more than one pictureBox object, so it may look like this instead:

pictureBox7.Image = image7;//do this for every pictureBox in every case statement

2 Comments

i removed foor and i used while ... it works thank you very much :D
No problem. |=^] If you feel this is a good/right answer, an upvote or check mark would be good.
0

It's run in loop so it's insert first image, next second and so on until the last. Possible it's to fast to see it and that's why u see only last image. Try to debug that program to see it's true.

My advice: just change all break's commands for "return" in switch case. That's all.

Ps. This code doesn't have sense in my opinion (i saying about switch case in for loop ;)).

1 Comment

yup , I got it I don't need to for loop :D I used a while( with global var i) it is incremented every time the uset press the button

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.