Graphics does not draw over the whole screen

This is a discussion on Graphics does not draw over the whole screen within the C# Programming forums, part of the General Programming Boards category; I wrote a snake game using WinForms and Graphics object.
During the game, I draw somewhere on the form, but ...

Graphics does not draw over the whole screen

I wrote a snake game using WinForms and Graphics object.

During the game, I draw somewhere on the form, but I've noticed that there is an area on the screen that the Graphics object can't draw there.
The form doesn't sprawl all over the screen, actually, it's size is 768x565.
So when I place it at the top-left corner of the screen it works perfectly, but at the center it's kind of cut.

You should do all drawing in OnPaint(). Otherwise when the window is resized and/or altered in someway you will be forced to call OnPaint(). Since the OnPaint() is automatically called during these events by WinForms if you override it you won't have to account for these specific cases.

Remember that most coordinates in WinForms are in local coordinates or are relative to the upper left corner of the parent form. The exception to this rule is the Cursor position which is in screen coordinates. Also keep in mind that you can use PointToClient() and PointToScreen() to convert from screen coordinates to client coordinates (coordinates relative to the upper left corner of the parent form) and vice versa.

So for instance if we have a text box inside of a form and the text box is at the upper left corner of the form the text box location is 0,0 regardless of the form's actual screen position. I cannot verify this next statement but given my experience with forms it appears that Position refers to screen coordinates and Location refers to local coordinates.

Use Console.WriteLine() to output your drawing coordinates. I think you will find that they are way off of your form. Your drawing coordinates should be within the form's ClientRectangle. Keep in mind as well that various mouse event handlers and form event handlers pass coordinates in and they are usually, but not always, in local coordinates or client coordinates.
This may mean in some mouse event handlers you might have to do some coordinate space conversion using the functions I mentioned in order to get everything to appear correct and behave correctly.

So from my point of view you have two distinct unrelated problems:

Your painting is not appearing at times

Your painting is not in the right location in the form.

My approach would be to forget the coordinates for now and simply override the OnPaint() and either put your drawing code in there or call out to your current Draw method. This will most likely fix your lack of painting issues. After that you can tackle the coordinate space issues. WinForms programming can be a pain but you can make it simpler by understanding the exact problems and if they are related or not. Sometimes a simple change in structure or change in method call order fixes everything. I would recommend buying a book but most of them are for .NET 2.0 and are older b/c MS has now recommended devs move to WPF.

You need to connect a drawing method to the Paint event for the form, that redraws the snake and everything when it is called. What happends is that every time you move a window, resizes it or whatever it has to be redrawn, but since you dont redraw it will just clear it and it will appear like nothing is working.

Edit: Bubba gave a more indepth reason what the problem is but I would strongly suggest that instead of overriding OnPaint connect a handler to Paint event.

What Shakti has mentioned is another approach so that you do not have to completely refactor your code. Either way you need to make sure that when WM_PAINT comes in to the WndProc of your form that your form paints itself using your drawing code. This will fix a whole host of drawing issues. His approach is a better one and requires less re-factoring of your existing code.

Oh, thanks.
But I draw the snake when the user clicks the 'start new game' button. How can I now draw it from the Paint method? Should I set a Boolean variable as true when he click the button, and check the variable in the Paint method, draw and set it as false?

You draw the snake like you always draw it, based on its location and so on. It isn't enough to draw the snake once and then change the variables that you use to keep track of the snake, each time you change one of these variables you want to redraw the snake.