0
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
  isDrawing = true;

  currentPoint = e.Location;
}

private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            Graphics bedLine = this.CreateGraphics();
            Pen bedp = new Pen(Color.Blue, 2);
            if (isDrawing)
            {
                bedLine.DrawLine(bedp, currentPoint, e.Location);
                currentPoint = e.Location;
            }
            bedp.Dispose();
        }

dont know how to delete a line, Drawn when mouse moves

Hari Prasad
  • 16,716
  • 4
  • 21
  • 35
Shushan
  • 373
  • 1
  • 3
  • 5
  • 2
    Without getting into the more general issues with this code, looks like you're missing a `Form1_MouseUp` somewhere in there. – Rotem Mar 20 '16 at 15:12
  • private void Form1_MouseUp(object sender, MouseEventArgs e){isDrawing = false; } – Shushan Mar 20 '16 at 15:14
  • 2
    _Graphics bedLine = this.CreateGraphics();_ As this is badly wrong, the rest simply is moot. Do [read up](http://stackoverflow.com/questions/32919918/how-to-draw-line-and-select-it-in-panel/32920894?s=7|0.0000#32920894) on how to draw in Winforms! Never cache or store a Grahics object. In fact you shouldn't event use CreateGraphics in the first place, as the Graphics object will never stay in scope and the graphics it produces will not persist (i.e. survive a Minimize-maximize sequence).. – TaW Mar 20 '16 at 15:14
  • there is no way just to delete Graphic object?? just to delete bedLine? – Shushan Mar 20 '16 at 16:13

1 Answers1

2

It is a bad idea to draw on your form in any other method than OnPaint. You can not delete a line, it can only be drawn over.

So you should do all your drawing in an overriden OnPaint method. I suggest the following:

public partial class Form1
{
    private bool isDrawing;
    private Point startPoint;
    private Point currentPoint;

    private void Form1_MouseDown(object sender, MouseEventArgs e)
    {
        isDrawing = true;
        startPoint = e.Location; // remember the starting point
    }

    // subscribe this to the MouseUp event of Form1
    private void Form1_MouseUp(object sender, EventArgs e)
    {
        isDrawing = false;
        Invalidate(); // tell Form1 to redraw!
    }
    private void Form1_MouseMove(object sender, MouseEventArgs e)
    {
        currentPoint = e.Location; // save current point
        Invalidate(); // tell Form1 to redraw!
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e); // call base implementation
        if (!isDrawing) return; // no line drawing needed

        // draw line from startPoint to currentPoint
        using (Pen bedp = new Pen(Color.Blue, 2))
            e.Graphics.DrawLine(bedp, startPoint, currentPoint);
    }
}

This is what happens:

  • when the mouse is pressed, we save that position in startPoint and set isDrawing to true
  • when the mouse is moved, we save the new point in currentPoint and call Invalidate to tell Form1 that it should be redrawn
  • when the mouse is released, we reset isDrawing to false and again call Invalidate to redraw Form1 without a line
  • OnPaint draws Form1 (by calling the base implementation) and (if isDrawing is true) a line from startPoint to currentPoint

Instead of overriding OnPaint you can also subscribe to the Paint event of Form1 just as you did with the mouse event handlers

René Vogt
  • 43,056
  • 14
  • 77
  • 99
  • there is no way just to delete Graphic object?? just to delete bedLine? – Shushan Mar 20 '16 at 16:13
  • no, `Graphics` is just used to draw. Your line itself is not a graphics object or something like that. So once drawn, the pixels are there until something else is drawn there. – René Vogt Mar 20 '16 at 16:14