Question

Can I delete the old rectangle which I have drawn and draw a new rectangle?

private void panel1_MouseClick(object sender, MouseEventArgs e)
{
        Graphics g = this.panel1.CreateGraphics();
        Pen pen = new Pen(Color.Black, 2);

        g.DrawRectangle(pen, 100,100, 100, 200);
        g.dispose();
}

No correct solution

OTHER TIPS

No, you cannot "delete" something that's already been drawn. You can overwrite it with something else, but drawing with Graphics objects is like painting in real-life: once the paint is dry, you can only paint over it with another colour, you can't "erase" it.

You probably shouldn't be drawing things in response to a MouseClick, either. It's best to only draw things in response to a Paint event. What I would do in this situation is add a Rectangle structure to a list on the MouseClick and then call panel1.Invalidate() to ask it to redraw itself. Then in the Paint event for the panel, do the drawing there.

This will kill two birds with one stone, because you will be able to "erase" thing by simply removing them from the list of stuff to draw.

This is usually done by maintaining a collection of objects you want drawn. The mouse click should update this collection and then tell the window (or the affect region) to refresh. This has the enormous advantage of preserving whatever you've drawn if the window is moved off-screen, hidden behind other windows, minimized, etc.

For a rudimentary solution, create a hierarchy of drawable shape types derived from a common abstract Shape class, and use, e.g., a List for the collection. The base Shape class will have an abstract Draw method that the derived classes override.

For a more industrial-strength solution, look around for 2-D scene graphs.

One can use Graphics.Save() and Graphics.Restore(state) methods for that. For example:

private void SaveRestore2(PaintEventArgs e)
{
    // Translate transformation matrix.
    e.Graphics.TranslateTransform(100, 0);

    // Save translated graphics state.
    GraphicsState transState = e.Graphics.Save();

    // Reset transformation matrix to identity and fill rectangle.
    e.Graphics.ResetTransform();
    e.Graphics.FillRectangle(new SolidBrush(Color.Red), 0, 0, 100, 100);

    // Restore graphics state to translated state and fill second

    // rectangle.
    e.Graphics.Restore(transState);
    e.Graphics.FillRectangle(new SolidBrush(Color.Blue), 0, 0, 100, 100);
}

http://msdn.microsoft.com/en-us/library/system.drawing.graphics.restore.aspx

Also, depending on the application, you might look at using DrawReversibleFrame. You can change the rectangle location by calling the Offset method.

Instead of calling g.DrawRectangle(pen, 100,100, 100, 200); , maintain the rectangle as a object which will be drawn by the graphics object. Each time you will update this rectangle object with new one and graphics object will draw the new one.

The refresh should clear the old rectangle and graphics will draw the new one.

You can just use VisualBasic PowerPacks, it is included with my version of Visual Studio 2008

Here's a sample code that will draw a rectangle over a TextBox, i.e. I am giving it a custom border

    Dim x = TextBox1.Location.X
    Dim y = TextBox1.Location.Y
    Dim width = TextBox1.Width
    Dim height = TextBox1.Height
    Dim ShapeContainer1 As New Microsoft.VisualBasic.PowerPacks.ShapeContainer
    Me.Controls.Add(ShapeContainer1)
    Dim RectangleShape1 As New Microsoft.VisualBasic.PowerPacks.RectangleShape
    ShapeContainer1.Shapes.AddRange(New Microsoft.VisualBasic.PowerPacks.Shape() {RectangleShape1})
    RectangleShape1.Location = New System.Drawing.Point(x - 1, y - 1)
    RectangleShape1.Size = New System.Drawing.Size(width + 1, height + 1)
    RectangleShape1.BorderColor = Color.MistyRose
    ShapeContainer1.Refresh()

Code is self describing but if you'd have any problem, just leave a message...

I think using DrawReversibleFrame is the right solution. The first call draw the rectangle, the second call undraw it and so on.

Here is a sample code, a clic on the button will make the rectangle appear/disapper.

Rectangle pRect = new Rectangle(10, 10, 20, 20);
private void rect_Click(object sender, EventArgs e)
{
  ControlPaint.DrawReversibleFrame(pRect, this.BackColor, FrameStyle.Thick);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top