Click here to Skip to main content
15,895,011 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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();
   // chkBxOutlinedSquare.CheckState = CheckState.Checked;
    this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
}

private void Form1_Paint(object sender,
    System.Windows.Forms.PaintEventArgs pe)
{
    // Declares the Graphics object and sets it to the Graphics object
    // supplied in the PaintEventArgs.
    Graphics g = pe.Graphics;

    //outlinedSquare(g);
    //someGraphic(g);

    // Insert code to paint the form here.
    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),   // Opaque red
    Color.FromArgb(255, 0, 0, 255));  // Opaque blue

    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)
{
    // Create a new pen.
    Pen skyBluePen = new Pen(Brushes.DeepSkyBlue);

    // Set the pen's width.
    skyBluePen.Width = 8.0F;

    // Set the LineJoin property.
    skyBluePen.LineJoin = System.Drawing.Drawing2D.LineJoin.Bevel;

    // Draw a rectangle.
    g.DrawRectangle(skyBluePen,
        new Rectangle(40, 40, 150, 200));

    //Dispose of the pen.
    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.
Posted
Updated 21-Apr-18 9:35am

First, you're not disposing the Pen you created in someGraphic.

Second, you're not clearing out the area you're drawing to in outlinedSquare(). That area, nor then entire form, is cleared waiting for you to paint it. If you have to remove the area that is filled just to draw an empty outline, you have to clear that area yourself. It's not going to be done for you.
 
Share this answer
 
Your question is not quiet clear for me.
Generally if you want that a Control (or a Form) repaints itself sometimes you must tell that to it. This could be done (for example) by the myControl.Invalidate. This method forces the call of the Paint-method from the Control (or Form).
This answer is additional to the Solution, mentioned by Dave ...
 
Share this answer
 
Comments
buckskinner 21-Apr-18 15:49pm    
What I was looking for is that on the repaint, if a checkbox was not selected and the associated outlinedSquare or someGraphic method was not called, that the repaint of the form would have painted the form background over the graphics created previously by outlinedSquare or someGraphic. I thought the form repaint would do this. The first response seems to indicate that I have to paint over what I had previously painted. I had messed with this years ago and do not recall that to be the case.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900