Hendrik Vis
Hendrik Vis

Reputation: 63

Color dialog causing an issue when used in a property grid

I have a spirograph application that draws spirograph patterns on a panel. The data used to generate the pattern is all in a class of properties that I display in a property grid. One of those properties is the pen color which, conveniently shows the color dialog when you want to switch pen colors. The problem is that when I first pull down the color dialog, it clears the panel of everything I've drawn previously. It only happens the first time I change colors. After that, I can change colors to my heart's content.

Using trace statements, I see that I do get a Paint event for the form when I pull down the color menu for the first time. I assume that's what is clearing my panel. After the first time though, I no longer get paint events when pulling down the color dialog.

Does anyone have any idea how to prevent this first time clearing?

A simple form with a button, panel, and property grid. Hit the button and the panel gets painted. The first time selecting a color, the panel is erased. After that, all is fine.

public partial class Form1 : Form
{
    private PropertyClass propertyClass = new PropertyClass();

    public Form1()
    {
        InitializeComponent();
        propertyClass.PanelColor = Color.Green;
        propertyGrid1.SelectedObject = propertyClass;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        Graphics g = panel1.CreateGraphics();
        g.FillRectangle(new SolidBrush(propertyClass.PanelColor), panel1.ClientRectangle);
        g.Dispose();
    }
}

public class PropertyClass
{
    public Color PanelColor { get; set; }
}

Upvotes: 0

Views: 39

Answers (0)

Related Questions