2

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...

BigBug
  • 6,202
  • 23
  • 87
  • 138
  • Are you saying you want to put the drawing logic in your panel class and not in your form's? As it is, your question makes no sense to me. – Jeff Mercado Nov 07 '11 at 23:37
  • I don't want the rectangle to be drawn on the form. I want it to show up on the panel - which, currently, nothing shows up when i draw on the panel... Does that make sense? Hope so.. – BigBug Nov 07 '11 at 23:39
  • have you looked at this: [http://stackoverflow.com/questions/282838/drawing-on-top-of-controls-inside-a-panel-c-winforms](http://stackoverflow.com/questions/282838/drawing-on-top-of-controls-inside-a-panel-c-winforms) – Udomaki Nov 07 '11 at 23:47
  • Also you could use a pictureBox instead since you could just draw an image directly onto the control... – Udomaki Nov 07 '11 at 23:50
  • Is that event handler the Form's Paint event, or the Panel's Paint event? Are you sure that it's being called? – Jim Mischel Nov 07 '11 at 23:58

2 Answers2

4

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
}
LarsTech
  • 80,625
  • 14
  • 153
  • 225
1

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

Eric J.
  • 147,927
  • 63
  • 340
  • 553