-1

i have drawn a simple line using DrawLine and PaintEvent. I want to select the line and delete it from the world !! I want guideline and direction that how will i select and delete the drawn line?

Edited: I dont need code. i need some direction and guideline thats it. so stop killing my reputation :(

Anum Qazi
  • 41
  • 1
  • 5

2 Answers2

1

I am not a C# expert but I can suggest something here, first of all you need to keep track of all the lines that you draw by using some collections like array. Now on mouse event you need to check whether the tap is closer to any of the lines that you have drawn, based on that you can pick the line from your collection and re draw/ move or erase. I have done the same way in iOS.

Please see some more info on this in the below links

Graphic - DrawLine - draw line and move it --> Very close to what you are asking for.

How to draw a selectable line?

How to draw and move shapes using mouse in C#

hope this helps

-anoop

Community
  • 1
  • 1
anoop4real
  • 7,598
  • 4
  • 53
  • 56
0

You can't select it. It's just pixels. You must redraw again whole area where that line been drawn, but now just without drawing this line in Paint event handler. Calculate rect that must be redrawn and call Invalidate() method of your Control to redraw this area.

Simple example:

using System;
using System.Drawing;
using System.Windows.Forms;

namespace TestPaintApp
{
    public class TestPaint : Form
    {
        private bool drawLine = false;
        private Point lineStart;
        private Point lineEnd;

        public TestPaint()
        {
            var drawLineButton = new Button();
            drawLineButton.Text = "Draw line";
            drawLineButton.Location = new Point(5, 5);
            drawLineButton.Click += DrawLineButton_Click;

            var dontDrawLineButton = new Button();
            dontDrawLineButton.Text = "Don't draw";
            dontDrawLineButton.Location = new Point(5, 30);
            dontDrawLineButton.Click += DontDrawLineButton_Click;

            GetLineRect();

            this.Controls.Add(drawLineButton);
            this.Controls.Add(dontDrawLineButton);

            this.MinimumSize = new Size(200, 200);

            this.Paint += Form_Paint;
            this.Resize += Control_Resize;
        }

        private Rectangle GetLineRect()
        {
            this.lineStart = new Point(75, 75);
            this.lineEnd = new Point(this.ClientSize.Width - 75, this.ClientSize.Height - 75);

            return new Rectangle(
                Math.Min(lineStart.X, lineEnd.X),
                Math.Min(lineStart.Y, lineEnd.Y),
                Math.Max(lineStart.X, lineEnd.X),
                Math.Max(lineStart.Y, lineEnd.Y)
                );
        }

        private void Form_Paint(object sender, PaintEventArgs e)
        {
            if (drawLine)
            {
                e.Graphics.DrawLine(Pens.Red, lineStart, lineEnd);
            }
        }

        private void Control_Resize(object sender, EventArgs e)
        {
            this.Invalidate(GetLineRect());
        }

        private void DrawLineButton_Click(object sender, EventArgs e)
        {
            drawLine = true;
            this.Invalidate(GetLineRect());
        }

        private void DontDrawLineButton_Click(object sender, EventArgs e)
        {
            drawLine = false;
            this.Invalidate(GetLineRect());
        }
    }
}
rufanov
  • 3,266
  • 1
  • 23
  • 41