views:

72

answers:

2

Guys, I know this is going to turn out to be a simple answer, but I can't seem to figure it out. I have a C# Winform application that I am trying to build. I am trying to draw a white line 60 pixels above the bottom of the form. I am using this code:

private void MainForm_Paint(object sender, PaintEventArgs e)
{
    e.Graphics.DrawLine(Pens.White, 10, this.Height-60, 505, this.Height-60);
}

Simple enough, however no line is drawn. After some debugging, I figured out that it IS drawing the line, but it is drawing it outside my form. If I change the -60 to -175, then I can see it at the bottom of my form. This would solve my problem, except as my form's height changes, the line draws closer and closer to the bottom of my form until eventually, its off the form again. What am I doing wrong? Am I using the wrong graphics unit? Or is there a more complex calculation I need to do to determine 60 pixels from the bottom of my form?

+8  A: 

You need to use ClientSize.Height instead of Height. The Height property returns the height of the entire form (including title bar and other parts of the window). The ClientSize property gives you the size of the area where you can draw.

For more information, see ClientSize property at MSDN.

Tomas Petricek
A: 

Where is this code? I noticed that it is an event handler and not necessarily a member of MainForm. So, when you reference this.Height, "this" might not be the MainForm (at least we can't tell from the code fragment you've included). In general, it's better to override OnPaint in your MainForm rather than attaching an event handler. Just be sure to call the base class's OnPaint before doing any painting of your own.

For more information, see OnPaint at MSDN.

NascarEd