Khronos
Khronos

Reputation: 381

Flickering on background colour change

I have a large panel with many child panels inside of it. Inside of those child panels are two text fields with a transparent background. It is essentially ListBox built from scratch.

What I am trying to do is loop through each of those child panels and change their background colour to a selected colour when a user clicks on one.

However, when I click on a new child panel, there is a very noticable flicker between the old background colour and the new background colour.

http://i.imgur.com/ROHYu.png

Note: The light blue colour is a highlighting colour when the user hovers over a panel.

I have tried setting DoubleBuffered to true for the main panel and the form itself without much luck. I've also tried setting ControlStyles.AllPaintingInWmPaint, ControlStyles.UserPaint and ControlStyles.OptimizedDoubleBuffer to true as well.

public class List : Panel
{
    private Panel[] items;
    private Label[] header; // Children of items
    private Label[] footer; // Children of items

    public List()
    {
        SetStyle(ControlStyles.AllPaintingInWmPaint, true);
        SetStyle(ControlStyles.UserPaint, true);
        SetStyle(ControlStyles.OptimizedDoubleBuffer, true);

        AutoScroll = true;
        BackColor = Color.White;
        //DoubleBuffered = true;
        HorizontalScroll.Visible = false;
        HorizontalScroll.Enabled = false;
        VerticalScroll.Visible = true;
        VerticalScroll.Enabled = true;
    }

    public void renderItemsSelected(Color color)
    {
        for (int q = 0; q < itemsSelected.Count; q++)
        {
            int i = getPos();

            items[i].BackColor = color;
        }
    }
}

So I was wondering if anyone has any ideas?

Upvotes: 0

Views: 2168

Answers (1)

BFree
BFree

Reputation: 103740

See my answer here:

WinForms - Does the Form.DoubleBuffered property influence controls placed on that form?

Basically, setting DoubleBuffered on a parent control doesn't trickle down to child controls. Try the hack that I proposed in that answer, see if that works for you.

Upvotes: 2

Related Questions