Rectangle can't be drawn on the panel by mouse dragging runtime

556 Views Asked by At

I am trying to draw a rectangle on a panel, but nothing is drawn. Below is the code to show how I draw a rectangle on the panel. In my code SetSelectionRect() is used to set the rectangle to be drawn. For these I use following methods.

   private void Panel_MouseDown(object sender, MouseEventArgs e)
    {
        Point point = new Point();
        this.mouseDown = true;
        this.Panel.SendToBack();

        point = this.Panel.PointToClient(Cursor.Position);
        point.X = e.X;
        point.Y = e.Y;
        this.selectionStart.X = point.X;
        this.selectionStart.Y = point.Y;
    }

    private void Panel_MouseMove(object sender, MouseEventArgs e)
    {
        if (!this.mouseDown)
        {
            return;
        }
        else
        {
            this.mouseMove = true;
            Point point = this.Panel.PointToClient(Cursor.Position);
            point.X = e.X;
            point.Y = e.Y;
            this.selectionEnd.X = point.X;
            this.selectionEnd.Y = point.Y;
            this.SetSelectionRect();
            ////this.Panel.Invalidate();
            ////this.Invalidate();
        }
    }

    private void Panel_MouseUp(object sender, MouseEventArgs e)
    {
        SetSelectionRect();
        this.GetSelectedControls();
        this.mouseDown = false;
        this.mouseMove = false;
        ////this.Panel.Invalidate();
        ////this.Invalidate();
        this.Panel.Refresh();
    }

    private void Panel_Paint(object sender, PaintEventArgs e)
    {
        ////base.OnPaint(e); drawRect = true when RectangleToolStripMenuItem is Clicked.
        if (this.drawRect)
        {
            using (Pen pen = new Pen(Color.Black, 1F))
            {

                    this.rectangle = new RectangleShape();
                    this.Panel.SendToBack();
                    this.shapeContainer1.Shapes.Add(this.rectangle);
                    this.rectangle.Location = this.selection.Location;
                    this.rectangle.Size = this.selection.Size;
                    this.rectangle.Name = "rectShape";
                    this.shapeContainer1.Size = this.Panel.Size;
                    this.shapeContainer1.Location = this.Panel.Location;
                    this.rectangle.Enabled = false;
                    this.rectangle.MouseClick += new MouseEventHandler(this.mouseclick);
                    this.rectangle.MouseMove += new MouseEventHandler(this.mouseMove);
                    this.rectangle.MouseDown += new MouseEventHandler(this.mouseDown);
                    this.rectangle.MouseUp += new MouseEventHandler(this.mouseUp);
                    this.drawRect = false; 
            }
        }
    }

    protected override void OnPaint (PaintEventArgs e)
    {
        base.OnPaint(e);
    }

What's wrong with the code?

1

There are 1 best solutions below

7
On

The problem is base.OnPaint(e); is raising an Paint event, where you placed base.OnPaint(e);, so it calling itself again and again.

private void panel_Paint(object sender, PaintEventArgs e)
{
    //base.OnPaint(e); // remove it from here
    // something to do.
}

base.OnPaint(e); should be called in overriden method:

protected override void OnPaint(PaintEventArgs e)
{
    base.OnPaint(e);
}