Hi
I created a custom System.Windows.Forms.Panel so I could override the OnPaint event and make it draw a gray border, but the inside of the panel is picking up all kinds of gray lines when the window scrolls or anything moves over top of the panel. Does anyone know how to clean this up?

protected override void OnPaint(PaintEventArgs e)
{
  e.Graphics.DrawRectangle(
     Pens.Gray,
     e.ClipRectangle.Left,
     e.ClipRectangle.Top,
     e.ClipRectangle.Width - 1,
     e.ClipRectangle.Height - 1);
  base.OnPaint(e);
}

Recommended Answers

All 6 Replies

You have overridden the forms OnPaint event.

what's this now

I used this and it draws a nice gray rectangle around the panel, not around the form.

private void panel1_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.DrawRectangle(
            Pens.Gray,
            e.ClipRectangle.Left,
            e.ClipRectangle.Top,
            e.ClipRectangle.Width - 1,
            e.ClipRectangle.Height - 1);
        }

I moved the code to the paint event, which I probably should've done from the start, but the problem's still there. It's not that the border's in the wrong place. It's hard to explain. When I scroll the screen over the panel it's like there are remnants of its old borders left behind when it draws the new one. In the actual code I'm using I call e.Graphics.Clear(this.BackColor); before drawing the border but it doesnt help at all

I dont know if it might have something to do with the fact that I'm using the control in a web page in Internet Explorer. Doesnt seem likely. Normal borders work fine. There must be another way to draw a border

Can you redraw the panel to cover up the broken lines, before you draw your new border?

I found a solution. I guess e.ClipRectangle is the visible part of the control which, if I could explain it, was the problem. I changed it to ((Panel)sender) and threw in a couple 0's to make it use the full size of the control every time

private void panel1_Paint(object sender, PaintEventArgs e)
{
  e.Graphics.DrawRectangle(
     Pens.Gray,
     0,
     0,
     ((Panel)sender).Width - 1,
     ((Panel)sender).Height - 1);
  base.OnPaint(e);
}

Thanks for the help guys

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.