1

I have a list of lines in a txt file, the C# program need to collect the data from the txt file and draw every line from the txt file. I need to make them somehow clickable, so if I click on a line the program recognise what specific line object is that and I have the option to delete it and redraw the image without that line (and repeat this until I only have the lines I need)

I'm just not sure how can I make this happen, what technology should I use for the clickable drawing part.

I trying to use winform because it seemed suitable for the job with the canvas and display,draw features etc.. I made some classes to store the data from the txt with a small hierarchy system. On top there is an image class, it has a Shape list (the program works with only 1 image, but the image can contain more shapes, some are inside in another), Shape class has a Line list, Line class has int variables: fromX,fromY,toX,toY I was able to get all the data from the txt and load into the program, I write out some data to the console, they stored properly, I can reach from the Image object every line's coordinates. Now I need to draw every line object one by one and make them clickable (maybe an onclick event which return or store the data of the clicked line) but the part where I'm stuck is I don't have an idea how can I calculate if the click is on a line, if it is on a line then which line object is that to get back the right data.

/*
Example of input file (two triangle and one pentagon on the pic, lines with ? are shape dividers)

X:Y>X:Y
810:-448>935:-532
935:-532>806:-534
806:-534>804:-449
?:?>?:?
597:-529>673:-411
673:-411>747:-531
747:-531>597:-529
?:?>?:?
475:-275>582:-355
582:-355>541:-487
541:-487>411:-487
411:-487>370:-355
370:-355>475:-275
?:?>?:?
*/

//As I mentioned I stored the data in line objects
class Line
    {
        public int startX;
        public int startY;
        public int endX;
        public int endY;

        public Line(int startX, int startY, int endX, int endY)
        {
            this.startX = startX;
            this.startY = startY;
            this.endX = endX;
            this.endY = endY;
        }

    }

//Every shape is an object with a list of the Line objects
 class Shape
    {
        public List<Line> lines = new List<Line>();

        public void AddLine(Line a)
        {
            this.lines.Add(a);
        }

        public void RemoveLine(Line a)
        {
            this.lines.Remove(a);
        }

        public void ResetShape()
        {
            this.lines.Clear();
        }

        public Line GetLine(int index)
        {
            return this.lines[index];
        }
    }

//Image class looks similar, it has a List of Shapes, the main difference is the constructor.
//The constructor get the file path as parameter, opens it, read and store data
//Split every line, get the coordinates, make line objects and add them to a temp Shape object's list
//When the read reach a divider it adds the temp Shape object to the Image's shape list
//Then reset the temp Shape object and continue to collect new lines untile the file reach the last line (which is a divider) 
reFi
  • 21
  • 1
  • basically, you should refer to GDI+ for the rendering and expand your ```Shape``` and ```Line``` classes to implement functions receiving mouse click coordinates and make clicking test, and you will have a whole new winform realm to explore. – Ge Rong Aug 13 '19 at 17:38
  • There are a lot of example around, for instance [How to drag and move shapes in C#](https://stackoverflow.com/q/38747027/3110834) or [this one](https://stackoverflow.com/q/38345828/3110834). – Reza Aghaei Aug 13 '19 at 17:52
  • Thank you, the links you provided Reza Aghaei are really hepful, thank you very much for both of you! I think I will be able to make this task now :) – reFi Aug 13 '19 at 18:20

1 Answers1

0

Try to use inheritance. The type Control (System.Windows.Forms.Control) has the Click method (and some more mouse and keyboard events)

public class Shape : Control
{

}

Then, when you want to add a click event to it just use:

private void SomeMethod()
{
    Shape shape = new Shape();
    shape.Click += Shape_Click;
}

private void Shape_Click(object sender, System.EventArgs e)
{
    // Do Something
}

If you want to make it shorter, you can use this:

Shape shape = new Shape();
shape.Click += (object sender, System.EventArgs e) =>
{
    // Do Something
};

Which does the same thing.

Good luck!

rom_totach
  • 106
  • 8