简体   繁体   中英

how to record multiple mouse clicks in c#

I am trying to create a paint program in c#, I have got the Objects to draw on the screen when I click the mouse. Know what I am trying to do is record all the places that the user clicks so that I can redraw the graphics later. I know I could do it with a list with this:

Point recordpoint = new Point(i.X, i.Y);
List<Point> pts = new List<Point>();
pts.Add(recordpoint);

This only adds the last mouse click and I need to know how add an infinite amount of mouse clicks to the list and I have no idea how to do this.

I would love it if anyone new how to do this.

Assuming you have an "OnClick" event available to handle then you can just move the collection to the class level and item new items on click:

public class MyClass
{

List<Point> pts = new List<Point>();//This way the member persists

public void OnClick(TypeName i, EventArgs e)//whatever params are..
{
    Point recordpoint = new Point(i.X, i.Y);//create element
    pts.Add(recordpoint);//insert into collection
}

}

You are creating a new list every time you add a point.

Move List<Point> pts into the top level of your Form class, so you are creating one list only.

class PaintForm : Form {

    // declare a list of points as a field
    private List<Point> pts = new List<Point>();

    // ..

    private void PictureBox1_OnMouseDown(..) {  // or whereever this code was
        Point recordpoint = new Point(i.X, i.Y);
        pts.Add(recordpoint);    
    }

    // ..
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM