BigBug
BigBug

Reputation: 6290

Draw rectangle on Panel C# Form

Question: How do i draw a rectangle on a Panel, rather than a form. Here is what my code looks like:

 /* 
  * based on a some flags i determine which shape i want to draw. 
  * All shapes are stored in a list. I loop through the list 
  * and call each shape specific draw method - as shown below:.
  *
  */
namespace myDrawProgram
{
     private void panelArea_Paint(object sender, PaintEventArgs e)
        {
            if (drawWithPaint == true)
            {
                Pen p = new Pen(Color.Blue);
                p.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;

                if (IsShapeRectangle == true)
                {
                    e.Graphics.DrawRectangle(p, rect);
                }
                else if (IsShapeCircle == true)
                {
                    e.Graphics.DrawEllipse(p, rect);
                }

            }
            foreach (Shapes shape in listOfShapes)
            {

                shape.Draw(e.Graphics);
            }
        }
}

/*
 * In another file i have my class which deals with 
 * drawing rectangles. It is as follows: 
 *
 */

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using SETPaint; 

namespace myDrawProgram
{
    class TheRectangles : Shapes
    {
        public Rectangle MyRect { set; get; }

        public TheRectangles(Rectangle rect, Color colour, Color boarderColour, Int32 brushThickness)
            : base(colour, boarderColour, brushThickness)
        {
            MyRect = rect;

        }


        public override void Draw(Graphics g)
        {
            base.Draw(g);


            g.FillRectangle(new SolidBrush(Shapes.c), MyRect);
            g.DrawRectangle(new Pen(bc, MyBrushThickness), MyRect);
        }
    }
}

i'm assuming i need to do something like this:

using (Graphics g = this.panel1.CreateGraphics()) {}

I'm just not sure how to implement this with regards to my code...

Upvotes: 2

Views: 15924

Answers (2)

LarsTech
LarsTech

Reputation: 81675

It sounds like you haven't hooked up the paint event of the panel:

panelArea.Paint += new PaintEventHandler(panelArea_Paint);

If panelArea is the name of your form, then just change it to your panel:

panel1.Paint += new PaintEventHandler(panel1_Paint);

and then move your painting logic to that method:

private void panel1_Paint(object sender, PaintEventArgs e) {
  // the rest of your drawing
}

Upvotes: 4

Eric J.
Eric J.

Reputation: 150228

It looks like the parent (form?) is responsible for drawing each of its controls.

I would not do it that way.

If you just need a control that draws shapes (and doesn't necessarily need other behavior of a Panel), I would just create a User Control that has a property indicating what shape to draw and make it responsible for its own rendering.

If you do need the behavior of a panel, you can subclass Panel and implement the drawing behavior in your subclassed control. Again, that makes the control responsible for its own rendering.

For info on user drawn controls see

http://msdn.microsoft.com/en-us/library/b818z6z6(v=vs.71).aspx

Upvotes: 1

Related Questions