user924069
user924069

Reputation:

How to get proper bounds for a C# Windows Forms Panel to draw a border

So I am trying to draw a bottom border for a panel. I have this code:

private void pnl_Paint(object sender, PaintEventArgs e)
{
    ControlPaint.DrawBorder(e.Graphics, ((Panel)sender).ClientRectangle, Color.Transparent, 0, ButtonBorderStyle.None, Color.Transparent, 0, ButtonBorderStyle.None, Color.Transparent, 0, ButtonBorderStyle.None, SystemColors.ControlDarkDark, 1, ButtonBorderStyle.Solid);
}

I have also replaced ClientRectange with DisplayRectangle and Bounds, and all of them produce the same result, the one in the picture.

I am trying to achieve a bottom border going all the way across the peach background (used to display the size of the panel)

screenshot

Upvotes: 1

Views: 1608

Answers (1)

LarsTech
LarsTech

Reputation: 81610

Change your Padding property to the following:

pnl.Padding = new Padding(0, 0, 0, 1);

Since you are just trying to draw a line, draw a line:

private void pnl_Paint(object sender, PaintEventArgs e) {
  e.Graphics.DrawLine(Pens.Black, new Point(0, pnl.ClientSize.Height - 1), new Point(pnl.ClientSize.Width, pnl.ClientSize.Height - 1));
}

And make sure to invalidate on the resize:

private void pnl_Resize(object sender, EventArgs e) {
  pnl.Invalidate();
}

Upvotes: 1

Related Questions