2012-10-02 54 views
1

我想要點擊一張圖片來啓動一個按鈕點擊事件。 但是如何? ButtonPlus通過每次點擊+1獲得收益,如果達到5/10 ...則激活其他圖像。C#在事件開始時啓動另一個事件

通過點擊加號按鈕來增加浪費時間,我想單擊圖片,例如第一個(獲取值5)並執行button_click代碼。

我曾經想過,將ButtonPlus的值設置爲4,然後轉發到button_click事件,得到值5,並進行切換。

private void button1_Click(object sender, EventArgs e) 
    { 
     if (MaxPunkte > 0) 
     { 

      if (ButtonPlus1 < 30) 
      { 
       ButtonPlus1 += 1; 
       MaxPunkte += -1; 
       LabelAnzeige1 = ButtonPlus1; 

       label1.Text = LabelAnzeige1.ToString(); 
       label5.Text = MaxPunkte.ToString(); 
       label7.Text = (LabelAnzeige1 * 10).ToString(); 
       label8.Text = label7.Text; 
       #region Switch 
       switch (ButtonPlus1) 
       { 
        case 5: 

         pictureBox1.Image = TraitCalc.Resources.HexagonSmall_5_active; 
         break; 

        case 10: 
         pictureBox2.Image = TraitCalc.Resources.HexagonBig_10_active_empty; 
         break; 

        case 15: 

         pictureBox3.Image = TraitCalc.Resources.HexagonSmall_15_active; 
         break; 

        case 20: 
         pictureBox4.Image = TraitCalc.Resources.HexagonBig_20_active_empty; 
         break; 

        case 25: 

         pictureBox5.Image = TraitCalc.Resources.HexagonSmall_25_active; 
         break; 

        case 30: 
         pictureBox6.Image = TraitCalc.Resources.HexagonBig_30_active_empty; 
         break; 


       } 
       #endregion 

      } 
     } 

    } 

private void pictureBox1_Click(object sender, EventArgs e) 
    { 
     ButtonPlus1 = 4; 
     //and start button click event (adds +1 = 5 -> go to switch case 1) 


    } 

回答

2

爲什麼不直接調用按鈕處理程序?

private void pictureBox1_Click(object sender, EventArgs e) 
{ 
    ButtonPlus1 = 4; 
    //and start button click event (adds +1 = 5 -> go to switch case 1) 
    button1_Click(null, null); // or whatever arguments you need, 
           // but you were not using them. 
} 
2
  • 移動的按鈕點擊代碼放到一個單獨的方法
  • 調用從按鈕單擊事件
  • 另外從圖片上單擊事件

調用它的方法(或者你可以與其他方法一樣,字面上呼叫button1_Click)。