C# – Problem with using Paint event in c#


I have used this below code before and was working perfectly. When I use the same
in one of my window form, the color of the form is not changing.

I mean after the page loads it shows the default color of the form. But when I try to debug the code below, it changes the color of the form perfectly. The problem is, after executing the last line of the code the color of the form goes back to the default color.

Am I missing something?

The form looks like a windows taskbar, and it has one tab control in it.

private void TaskBar_Paint(object sender, PaintEventArgs e)
    Graphics mGraphics = e.Graphics;
    Pen pen1 = new Pen(Color.FromArgb(96, 155, 173), 1);

    Rectangle Area1 = new Rectangle(0, 0, this.Width - 2, this.Height - 2);
    LinearGradientBrush LGB = new LinearGradientBrush(Area1,
        Color.FromArgb(96, 155, 173),
        Color.FromArgb(245, 251, 251),
    mGraphics.FillRectangle(LGB, Area1);
    mGraphics.DrawRectangle(pen1, Area1);

Best Solution

There isn't much to go on here. What is this handler attached to? The Paint event of the Form? If so, you should override OnPaint() instead of attaching to the handler. My guess is that some other method is doing some painting too. You need to track that down. Without more code, it is not very likely that anyone here can help you. Sorry.

Related Question