2011-02-04 76 views
0

我編寫代碼在上使用C#與Windows窗體應用程序處理的MouseClick事件上繪製切換按鈕。點擊事件觸發,但操作未執行。誰能告訴我我做錯了什麼?PictureBox上的Click事件正在觸發,但沒有執行任何操作

public partial class Form1 : Form 
{ 
    bool flagarrow = false; 

    public Form1() 
    { 
     InitializeComponent(); 

     pictureBox1.Paint += new PaintEventHandler(pictureBox1_Paint); 

    } 

    void pictureBox1_Paint(object sender, PaintEventArgs e) 
    { 
     Point[] arrPoints = new Point[3]; 

     //Identify rectangle area filled by label. 
     Rectangle lblBackground = (sender as Control).ClientRectangle; 

     if (false == flagarrow) 
     { 
      //(x0,y0) for Triangle. 
      arrPoints[0].X = lblBackground.Left + 5; 

      arrPoints[0].Y = lblBackground.Top + 7; 

      //(x1,y1) for Triangle. 
      arrPoints[1].X = lblBackground.Left + 5; 

      arrPoints[1].Y = lblBackground.Top + 17; 

      //(x2,y2) for Triangle. 
      arrPoints[2].X = lblBackground.Left + 14; 

      arrPoints[2].Y = lblBackground.Top + 12; 
     } 
     else 
     { 

      //(x0,y0) for Triangle. 
      arrPoints[0].X = lblBackground.Left + 5; 

      arrPoints[0].Y = lblBackground.Top + 7; 

      //(x1,y1) for Triangle. 
      arrPoints[1].X = lblBackground.Left + 15; 

      arrPoints[1].Y = lblBackground.Top + 7; 

      //(x2,y2) for Triangle. 
      arrPoints[2].X = lblBackground.Left + 10; 

      arrPoints[2].Y = lblBackground.Top + 16; 

     } 

     //Fill the Triangle with Black Color. 
     e.Graphics.FillPolygon(Brushes.Black, arrPoints); 
    } 

    private void pictureBox1_Click(object sender, EventArgs e) 
    { 

     if (flagarrow == false) 
     { 
      flagarrow = true; 
     } 
     else 
     { 
      flagarrow = false; 
     } 
    } 
} 
+0

你能做到從臨時按鈕的動作? – Igal 2011-02-04 13:11:36

回答

2

首先確保你掛鉤的Click事件。我看到這是一個部分類,所以它可能在設計器代碼背後。第二次嘗試使圖片框在點擊後無效以強制刷新。

private void pictureBox1_Click(object sender, EventArgs e) 
{ 

    if (flagarrow == false) 
    { 
     flagarrow = true; 
    } 
    else 
    { 
     flagarrow = false; 
    } 

    pictureBox1.Invalidate(); 
} 
2

PictureBox.Click事件確實正在提高,而且我懷疑,在你的事件處理程序的代碼恰好正在按預期運行。

問題是,您在該事件處理程序方法中所做的所有操作都設置了變量的值(flagarrow)。 你沒有做任何事情會導致PictureBox控件重新繪製自己。Paint事件從未觸發,因此它的外觀保持不變。

修復很簡單:撥打電話Invalidate method這將迫使PictureBox控件重繪本身。儘管我們處於這個狀態,但您也可以稍微清理一下您的代碼。

修改代碼在你Click事件處理程序如下:

private void pictureBox1_Click(object sender, EventArgs e) 
{ 
    flagarrow = !flagarrow; 
    pictureBox1.Invalidate(); 
} 
+0

@Joe:不,你說得對。它確實工作。我清楚地試圖在這裏一次做太多事情。我批准了您的修改,但需要兩個人。而您的待處理編輯會阻止我自己進行更改。所以希望它會很快得到第二個讚許。但謝謝你指出。 – 2011-02-04 14:09:44

4

Winforms沒有理由做任何特殊的事情,只是因爲你改變了你的代碼中的私人領域。您必須告訴它您在Paint事件處理程序中使用的條件已更改,並且需要新的繪圖。讓你的Click事件處理程序是這樣的:

flagarrow = !flagarrow; 
pictureBox1.Invalidate(); 
0

您只是需要修改PictureBox的單擊事件如下:

private void pictureBox1_Click(object sender, EventArgs e) 
{ 
      if (flagarrow == false) 
      { 
       flagarrow = true; 
      } 
      else 
      { 
       flagarrow = false; 
      } 
      //Add this following line to repaint the picture box. 
      pictureBox1.Refresh(); 

}