2014-07-04 14 views
0

大家好,這是我的第一個問題。 我有一些問題讓我的自定義創建的單元類正常工作的單擊事件。到現在爲止,它通知單元格被點擊的時間,但它只顯示最後一個單元格被點擊,即使你點擊了第一個單元格。這是我的代碼。對象(類Cell)上的自定義點擊事件只要點擊了最後一個單元格就會觸發

class Cell 
{ 
    public const int Width = 220; 
    public const int Height = Width; 
    public Point Position { get; set; } 
    public string Id { get; set; } 

    public delegate void ClickEvent(object o); 
    public event ClickEvent Click; 


    public Cell()  
    {  

    } 

    public void OnClick(Point p) 
    { 
     if (Click != null) 
     { 

      if (p.X - 220 < Position.X && p.X > 200 && p.Y - 220 < Position.Y && p.Y > 10) 
      { 
       Click(this); 
      } 

     } 
    } 

    public override string ToString() { return "Cell(" + row + "," + col + ")"; } 
    } 


public partial class Form1 : Form 
{ 
    Grid myGrid; 
    Cell cell; 
    private Point userPoint; 
    Graphics g; 
    int x = 200; 
    int y = 20; 

    public Form1() 
    { 
     InitializeComponent(); 
     myGrid = new Grid();   
    } 
    private void Form1_MouseDown(object sender, MouseEventArgs e) 
    { 
     userPoint = e.Location; 
     cell.OnClick(userPoint); 
     label1.Text = userPoint.ToString(); 
    } 

    private void Form1_Paint(object sender, PaintEventArgs e) 
    { 
     g = e.Graphics; 
     DrawCrossingCells(); 
     myGrid.DrawAllCrossings(g,imageList1); 
    } 

    private void DrawCrossingCells() 
    { 

     for (int row = 0; row < 3; row++) 
     { 
      for (int col = 0; col < 4; col++) 
      { 
       cell = new Cell(row,col);     
       g.DrawRectangle(new Pen(Color.Blue),x,y,Cell.Width,Cell.Height); 
       cell.Position = new Point(x, y); 
       cell.Id = (col.ToString() + row.ToString()).ToString(); 
       x += 220; 
       cell.Click += cell_Click;    
      }    
      x = 200; 
      y += 220; 
     } 
    } 

    private void cell_Click(object sender) 
    { 
     Cell cc = (Cell)sender; 
     Crossing cross = new Crossing(cc.Id); 
     cross.Position = cc.Position; 
     myGrid.AddCrossing(cross); 
     MessageBox.Show(cc.Id); 
    } 
} 
+0

的「在細胞內的點?」不知怎的,部分感覺錯了。嘗試使用矩形?看起來你正在對Point進行一些數學運算,最終把它放在別的地方,而不是你實際得到它的地方。 – TJennings

+0

你的意思是在Cell類的Onclick方法中的邏輯?因爲我沒有得到你。 – Vla

+0

我認爲'Onclick'中的邏輯應該是'if(p.X> Position.X && p.X Position.Y && p.Y petelids

回答

0

你只具有在DrawCrossingCells在每次循環覆蓋一個Cell對象。這意味着cell字段只會指向循環內創建的最後一個Cell,因此當您執行cell.OnClick(userPoint);時,您始終會呼叫最後創建的CellOnClick

當您在Form1中創建對象時,您需要存儲每個Cell對象。對於行中的每個cellList對於每行List s的List有意義。這意味着行Cell cell;將變爲List<List<Cell>> cells = new List<List<Cell>>();。在你的循環內,你可以用你創建的每個Cell填充cells對象。

Form1_MouseDown您需要確定哪個Cell已被點擊並且請撥打OnClick正確。這可以通過將e.Location.X除以220(單元寬度)並將其用作內部List的索引並將e.Location.Y除以220(單元高度)並使用它索引外部List來完成。

兩個改變方法的代碼將被:

private void DrawCrossingCells() 
{ 
    for (int row = 0; row < 3; row++) 
    { 
     List<Cell> rowCells = new List<Cell>(); 
     for (int col = 0; col < 4; col++) 
     { 
      Cell cell = new Cell(row, col); 
      g.DrawRectangle(new Pen(Color.Blue), x, y, Cell.Width, Cell.Height); 
      cell.Position = new Point(x, y); 
      cell.Id = (col.ToString() + row.ToString()).ToString(); 
      x += 220; 
      cell.Click += cell_Click; 
      rowCells.Add(cell); 
     } 
     cells.Add(rowCells); 
     x = 0; 
     y += 220; 
     } 
    } 

private void Form1_MouseDown(object sender, MouseEventArgs e) 
{ 
    int row = e.Location.Y/220; //divide by the Y size 
    int column = e.Location.X/220; //divide by the X size 

    cells[row][column].OnClick(e.Location); 
    label1.Text = e.Location.ToString(); 
} 
+0

真的很有幫助,非常感謝,現在我有另一個問題:我有一個最多12個十字路口,他們每個人都有12條車道。 ,你能否給我提供一條連接適當通道作爲通道的最佳方式。正確的我的意思是通過id連接車道。id有三個字母 - 前兩個字母顯示它們所屬的路口,第三個是車道的實際數量。圖形是二維陣列(Matrix-12交叉點和144 lane) – Vla

+0

@ user3804541 - 沒問題。我認爲可能值得爲你的其他問題開一個新的問題。我會高興地看看它。如果這個答案已經解決了您的第一個問題,那麼可能值得將其標記爲接受的答案。 – petelids

+0

我不知道,我可以標記對不起:) – Vla