I'm just getting into working on some graphics on forms and have copied a few examples and have been able to make them work.
The PaintEventHandler works fine. I have methods called from the PaintEventHandler that will paint one of two graphics. These work fine. They are commented out in the following code.
I am questioning why these same method calls based upon the status of a checkbox do not work. I am expecting that I can check a checkbox and resize the window and if the checkbox is checked, the appropriate graphic will be drawn. If the checkbox is not checked, the graphic would not be drawn.
I'm guessing I have a conceptual error here. Would the graphics just be left there stagnant maybe?
public Form1()
{
InitializeComponent();
this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
}
private void Form1_Paint(object sender,
System.Windows.Forms.PaintEventArgs pe)
{
Graphics g = pe.Graphics;
if (chkBxOutlinedSquare.CheckState == CheckState.Checked)
{
outlinedSquare(g);
}
if (chkBxSomeGraphics.Checked)
{
someGraphic(g);
}
}
private void someGraphic(Graphics g)
{
LinearGradientBrush linGrBrush = new LinearGradientBrush(
new Point(0, 10),
new Point(200, 10),
Color.FromArgb(255, 255, 0, 0),
Color.FromArgb(255, 0, 0, 255));
Pen pen = new Pen(linGrBrush);
g.DrawLine(pen, 0, 10, 200, 10);
g.FillEllipse(linGrBrush, 0, 30, 200, 100);
g.FillRectangle(linGrBrush, 0, 155, 500, 30);
}
private void outlinedSquare(Graphics g)
{
Pen skyBluePen = new Pen(Brushes.DeepSkyBlue);
skyBluePen.Width = 8.0F;
skyBluePen.LineJoin = System.Drawing.Drawing2D.LineJoin.Bevel;
g.DrawRectangle(skyBluePen,
new Rectangle(40, 40, 150, 200));
skyBluePen.Dispose();
}
What I have tried:
I have put breakpoints in both methods and the code is definitely running at the correct times. It's just not clearing the graphics when the checkboxes are cleared.