0

When my code runs only about 1/3 of the time it will actually draw properly, the rest it will just not draw some of the lines.

Pen pen = new Pen(Color.Black);
        Graphics graphics = MainFrm.HangmanImage.CreateGraphics();
        switch (NoOfGuesses)
        {
            case 1://Bottom left leg
                graphics.DrawLine(pen, 15, 60, 40, 60);
                break;
            case 2://Bottom right leg
                graphics.DrawLine(pen, 40, 60, 55, 60);
                break;
            case 3://Middle stock
                graphics.DrawLine(pen, 34, 20, 34, 60);
                break;
            case 4: //Top line
                graphics.DrawLine(pen, 34, 20, 55, 20);
                break;
            case 5: //Rope
                graphics.DrawLine(pen, 55, 20, 55, 25);
                break;
            case 6: //Head
                graphics.DrawEllipse(pen, (new Rectangle { X=52, Y=25, Height=5, Width=5 }));
                break;
            case 7: //Body
                graphics.DrawLine(pen, 55, 30, 55, 35);
                break;
        }
        pen.Dispose();
        graphics.Dispose();
user1763295
  • 860
  • 3
  • 16
  • 34

2 Answers2

0

Not enough information to be sure, however, I would first make sure that your variable 'NoOfGuesses' is always a value between 1-7, a value of 0 or 8 will end up doing nothing. Second, I would add a 'default' case in case your 'NoOfGuesses' is out of that range for any reason and have it also draw a line.

Lochemage
  • 3,974
  • 11
  • 11
  • Or have it print some warning message on `default`. A line doesn't convey as much information, and could blend into the application. – Mike Precup Jul 17 '13 at 22:09
  • NoOfGuesses is always in the range of this. Plus if it was an error like that, then it wouldn't draw the line some of the time and not the rest. – user1763295 Jul 17 '13 at 22:09
0

I fixed this by running this code on the form's paint event, it seems it was forgetting about it whenever it redrew the form.

user1763295
  • 860
  • 3
  • 16
  • 34