2012-03-12 2 views
0

코카인 기계를 만들려고하는데 문제가되지 않습니다.버튼 클릭 이벤트마다 프로그램의 특정 부분을 다시 실행하려면 어떻게합니까?

버튼 클릭 이벤트가 발생할 때마다 특정 코드 블록을 다시 실행시키고 싶습니다. 어떻게해야합니까?

이 내 전체 코드입니다 :

using System; 
using System.Windows.Forms; 
using System.Drawing; 

namespace QuickSharp 
{ 
public class CokeMachine 
{ 


    public Button Coke = new Button(); 
    public Button Sprite = new Button(); 
    public Button Fanta = new Button(); 
    public Button RedBull = new Button(); 
    public Button Juice = new Button(); 
    public Button Water = new Button(); 

    double CokeCost = 1.00; 
    double SpriteCost = 1.00; 
    double FantaCost = 1.00; 
    double RedBullCost = 2.50; 
    double JuiceCost = 2.00; 
    double WaterCost = 0.50; 

    public Button Coin1 = new Button(); 
    public Button Coin2 = new Button(); 
    public Button Coin3 = new Button(); 
    public Button Coin4 = new Button(); 
    public Button Coin5 = new Button(); 
    public Button Coin6 = new Button(); 

    double CoinAmount1 = 0.05; 
    double CoinAmount2 = 0.10; 
    double CoinAmount3 = 0.20; 
    double CoinAmount4 = 0.50; 
    double CoinAmount5 = 1.00; 
    double CoinAmount6 = 2.00; 

    public double TotalInsertedCoins = 0; 

    //EventHandlers for drink buttons 

    public void DrinkButtonEvents() 
    { 
     Coke.Click += new EventHandler(Coke_ClickHandler); 
     Sprite.Click += new EventHandler(Sprite_ClickHandler); 
     Fanta.Click += new EventHandler(Fanta_ClickHandler); 
     RedBull.Click += new EventHandler(RedBull_ClickHandler); 
     Juice.Click += new EventHandler(Juice_ClickHandler); 
     Water.Click += new EventHandler(Water_ClickHandler); 
    } 

    //Drink buttons - Click event handlers 

    public void Coke_ClickHandler(object sender, EventArgs e) 
    { 
     if(TotalInsertedCoins >= CokeCost) 
     { 
      DispenseDrink("Coca-Cola" , CokeCost); 
     } 
     else 
     { 
      MessageBox.Show("You have not inserted enough money to buy this drink." , "Warning"); 
     } 
    } 

    public void Sprite_ClickHandler(object sender, EventArgs e) 
    { 
     if(TotalInsertedCoins >= SpriteCost) 
     { 
      DispenseDrink("Sprite" , SpriteCost); 
     } 
     else 
     { 
      MessageBox.Show("You have not inserted enough money to buy this drink." , "Warning"); 
     } 
    } 

    public void Fanta_ClickHandler(object sender, EventArgs e) 
    { 
     if(TotalInsertedCoins >= FantaCost) 
     { 
      DispenseDrink("Fanta" , FantaCost); 
     } 
     else 
     { 
      MessageBox.Show("You have not inserted enough money to buy this drink." , "Warning"); 
     } 
    } 

    public void RedBull_ClickHandler(object sender, EventArgs e) 
    { 
     if(TotalInsertedCoins >= RedBullCost) 
     { 
      DispenseDrink("Red Bull" , RedBullCost); 
     } 
     else 
     { 
      MessageBox.Show("You have not inserted enough money to buy this drink." , "Warning"); 
     } 
    } 

    public void Juice_ClickHandler(object sender, EventArgs e) 
    { 
     if(TotalInsertedCoins >= JuiceCost) 
     { 
      DispenseDrink("Orange Juice" , JuiceCost); 
     } 
     else 
     { 
      MessageBox.Show("You have not inserted enough money to buy this drink." , "Warning"); 
     } 
    } 

    public void Water_ClickHandler(object sender, EventArgs e) 
    { 
     if(TotalInsertedCoins >= WaterCost) 
     { 
      DispenseDrink("Water" , WaterCost); 
     } 
     else 
     { 
      MessageBox.Show("You have not inserted enough money to buy this drink." , "Warning"); 
     } 
    } 

    //EventHandlers for money buttons 

    public void MoneyButtonEvents() 
    { 
     Coin1.Click += new EventHandler(Coin1_ClickHandler); 
     Coin2.Click += new EventHandler(Coin2_ClickHandler); 
     Coin3.Click += new EventHandler(Coin3_ClickHandler); 
     Coin4.Click += new EventHandler(Coin4_ClickHandler); 
     Coin5.Click += new EventHandler(Coin5_ClickHandler); 
     Coin6.Click += new EventHandler(Coin6_ClickHandler); 
    } 

    //Money buttons - Click event handlers 

    public void Coin1_ClickHandler(object sender, EventArgs e) 
    { 
     TotalInsertedCoins += CoinAmount1; 

    } 

    public void Coin2_ClickHandler(object sender, EventArgs e) 
    { 
     TotalInsertedCoins += CoinAmount2; 
    } 

    public void Coin3_ClickHandler(object sender, EventArgs e) 
    { 
     TotalInsertedCoins += CoinAmount3; 
    } 

    public void Coin4_ClickHandler(object sender, EventArgs e) 
    { 
     TotalInsertedCoins += CoinAmount4; 
    } 

    public void Coin5_ClickHandler(object sender, EventArgs e) 
    { 
     TotalInsertedCoins += CoinAmount5; 
    } 

    public void Coin6_ClickHandler(object sender, EventArgs e) 
    { 
     TotalInsertedCoins += CoinAmount6; 
    } 

    private void DispenseDrink(string drink , double cost) 
    { 

     if(TotalInsertedCoins - cost == 0.0) 
     { 
      MessageBox.Show("Enjoy your " + drink + "!"); 
      TotalInsertedCoins = 0.0; 
     } 
     else 
     { 
      MessageBox.Show("Enjoy your " + drink + "! Here is your change: €" + (TotalInsertedCoins - cost)); 
      TotalInsertedCoins = 0.0; 
     } 


    } 

    public CokeMachine() 
    { 
    MoneyButtonEvents(); 
    DrinkButtonEvents(); 
    } 
} 

public class CokeForm : Form 
{ 

    public CokeForm() 
    { 

     CokeMachine Machine = new CokeMachine(); 


     // General aspect of machine 

     this.Text = "Cola Machine"; 
     this.Size = new Size(450 , 500); 

     Label Header; 
     Header = new Label(); 
     Header.Text = "Coca-Cola Machine"; 
     Header.Font = new Font("Arial" , Header.Font.Size +5); 
     Header.ForeColor = Color.DarkRed; 
     Header.Location = new Point(132, 20); 
     Header.AutoSize = true; 
     this.Controls.Add(Header); 

     TextBox TextBox1 ; 
     TextBox1 = new TextBox(); 
     TextBox1.BackColor = Color.Black; 
     TextBox1.ForeColor = Color.Red; 
     TextBox1.Font = new Font("Arial" , TextBox1.Font.Size +3); 
     TextBox1.ReadOnly = true; 
     TextBox1.Size = new Size(210,300); 
     TextBox1.Location = new Point(112,50); 

     int tester = 0; 
     if(Machine.TotalInsertedCoins == 0.05){tester = 1;} 
     if(Machine.TotalInsertedCoins > 0.05){tester = 2;} 
     else{tester = 0;} 


     switch(tester) 
     { 
      case 1: 
       TextBox1.Text = "Inserted Coins: €" + Machine.TotalInsertedCoins; 
       break; 
      case 2: 
       TextBox1.Text = "Inserted Coins: €" + Machine.TotalInsertedCoins + "0"; 
       break; 
      default: 
       TextBox1.Text = "Buy Your Ice Cold Drinks Here!"; 
       break; 
     } 

     /* 

     Previous Attempt: 
     if(Machine.TotalInsertedCoins == 0.00) 
     { 
      TextBox1.Text = "Buy Your Ice Cold Drinks Here!"; 
     } 
     else 
     { 
      TextBox1.Text = "Inserted Coins: €" + Machine.TotalInsertedCoins; 
     } 
     */ 

     this.Controls.Add(TextBox1); 

     // Money aspect of machine 

     Label Money; 
     Money = new Label(); 
     Money.Text = "Insert Coins Here:"; 
     Money.Location = new Point(20, 100); 
     this.Controls.Add(Money); 

     //Money buttons 

     Machine.Coin1.Text = "Insert €0.05"; 
     Machine.Coin1.Location = new Point(28, 125); 
     this.Controls.Add(Machine.Coin1); 

     Machine.Coin2.Text = "Insert €0.10"; 
     Machine.Coin2.Location = new Point(28, 165); 
     this.Controls.Add(Machine.Coin2); 

     Machine.Coin3.Text = "Insert €0.20"; 
     Machine.Coin3.Location = new Point(28, 205); 
     this.Controls.Add(Machine.Coin3); 

     Machine.Coin4.Text = "Insert €0.50"; 
     Machine.Coin4.Location = new Point(28, 245); 
     this.Controls.Add(Machine.Coin4); 

     Machine.Coin5.Text = "Insert €1.00"; 
     Machine.Coin5.Location = new Point(28, 285); 
     this.Controls.Add(Machine.Coin5); 

     Machine.Coin6.Text = "Insert €2.00"; 
     Machine.Coin6.Location = new Point(28, 325); 
     this.Controls.Add(Machine.Coin6); 


     // Drink aspect of machine 

     Label Drinks; 
     Drinks = new Label(); 
     Drinks.Text = "Choose Your Drink:"; 
     Drinks.Location = new Point(315 , 100); 
     Drinks.AutoSize = true; 
     this.Controls.Add(Drinks); 

     //Drink buttons 

     Machine.Coke.Text = "Coca-Cola (€1.00)"; 
     Machine.Coke.Location = new Point(315, 125); 
     Machine.Coke.AutoSize = true; 
     this.Controls.Add(Machine.Coke); 

     Machine.Sprite.Text = "Sprite (€1.00)"; 
     Machine.Sprite.Location = new Point(315, 165); 
     Machine.Sprite.AutoSize = true; 
     this.Controls.Add(Machine.Sprite); 

     Machine.Fanta.Text = "Fanta (€1.00)"; 
     Machine.Fanta.Location = new Point(315, 205); 
     Machine.Fanta.AutoSize = true; 
     this.Controls.Add(Machine.Fanta); 

     Machine.RedBull.Text = "Red Bull (€2.50)"; 
     Machine.RedBull.Location = new Point(315, 245); 
     Machine.RedBull.AutoSize = true; 
     this.Controls.Add(Machine.RedBull); 

     Machine.Juice.Text = "Fruit Juice (€2.00)"; 
     Machine.Juice.Location = new Point(315, 285); 
     Machine.Juice.AutoSize = true; 
     this.Controls.Add(Machine.Juice); 

     Machine.Water.Text = "Water (€0.50)"; 
     Machine.Water.Location = new Point(315, 325); 
     Machine.Water.AutoSize = true; 
     this.Controls.Add(Machine.Water); 
    } 
} 

public class Test 
{ 

    public static void Main() 
    {     

     CokeForm ColaForm; 
     ColaForm = new CokeForm(); 
     Application.Run(ColaForm); 
    } 
} 
} 

이제 매번 코인 버튼을 누르면, 내가 원하는이 부분에 수 다시 실행 :

int tester = 0; 
     if(Machine.TotalInsertedCoins == 0.05){tester = 1;} 
     if(Machine.TotalInsertedCoins > 0.05){tester = 2;} 
     else{tester = 0;} 


     switch(tester) 
     { 
      case 1: 
       TextBox1.Text = "Inserted Coins: €" + Machine.TotalInsertedCoins; 
       break; 
      case 2: 
       TextBox1.Text = "Inserted Coins: €" + Machine.TotalInsertedCoins + "0"; 
       break; 
      default: 
       TextBox1.Text = "Buy Your Ice Cold Drinks Here!"; 
       break; 
     } 

이 방법 콜라의 표시 기계는 당신이 삽입 한 돈의 금액을 말할 것입니다.

미리 감사드립니다.

답변

1

리팩토링 밖으로 당신이 필요로하는 부분은 cokeform 생성자 내부 cokeform 생성자

public partial class CokeForm : Form 
{ 
    CokeMachine Machine = new CokeMachine(); 
    TextBox TextBox1; 

    public CokeForm() 
    { 
     ... 
    } 

에서 공공 방법

public void CoinInserted(object sender, EventArgs e) 
{ 
    int tester = 0; 
    if (Machine.TotalInsertedCoins == 0.05) { tester = 1; } 
    if (Machine.TotalInsertedCoins > 0.05) { tester = 2; } 

    switch (tester) 
    { 
     case 1: 
      TextBox1.Text = "Inserted Coins: €" + Machine.TotalInsertedCoins; 
      break; 
     case 2: 
      TextBox1.Text = "Inserted Coins: €" + Machine.TotalInsertedCoins + "0"; 
      break; 
     default: 
      TextBox1.Text = "Buy Your Ice Cold Drinks Here!"; 
      break; 
    } 
} 

이동 cokemachine 및 TextBox1에로 다시 실행, 다음 코드를 추가

Machine.Coin1.Click += new EventHandler(CoinInserted); 
Machine.Coin2.Click += new EventHandler(CoinInserted); 
Machine.Coin3.Click += new EventHandler(CoinInserted); 
Machine.Coin4.Click += new EventHandler(CoinInserted); 
Machine.Coin5.Click += new EventHandler(CoinInserted); 
Machine.Coin6.Click += new EventHandler(CoinInserted); 

CoinInserted(this, null); 
+0

고마워요! 마침내 내 프로그램을 마쳤습니다! –

+0

걱정하지 마시고, 행복하게 코딩하십시오 – uowzd01

+0

왜 메소드를 'public'으로해야합니까? – svick

3

그런 다음 코드 조각을 별도의 메서드에 넣고 필요한 위치로 호출해야합니다.

관련 문제