2014-03-26 17 views
0

我想在c#中創建一個繪圖程序,點擊鼠標時我在屏幕上繪製了對象。知道我想要做的是記錄用戶點擊的所有位置,以便稍後重新繪製圖形。我知道我可以用這個列表做:如何在c中記錄多個鼠標點擊#

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

這不僅增加了最後的鼠標點擊,我需要知道如何鼠標點擊的無限量添加到列表中,我不知道該怎麼辦這個。

我會喜歡它,如果有人如何做到這一點。

+0

什麼情況下,你在幹什麼呢?我從哪裏來? – mason

+0

向我們展示更多代碼:) –

回答

0

您每次添加點時都會創建一個新列表。

List<Point> pts移動到Form類的頂層,因此您只創建一個列表。

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);  
    } 

    // .. 
} 
+0

好的,我如何獲取列表中的項目並在點擊的位置繪製對象。 – user3349095

+0

嗯,我不會爲你寫程序,但是你說你已經知道如何在用戶點擊的地方繪製一個對象,並且你可以使用'foreach'循環遍歷你保存在你的每個點'pts' list:'foreach(var pt in pts){/ *繪製對象在pt * /}' – Blorgbeard

+0

非常感謝,完美的作品 – user3349095

1

假設你有一個「點擊」事件可用來處理,那麼你可以只移動集合類級別和點擊項目新項目:

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 
} 

}