Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/319.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从另一个类调用单击事件?_C#_Winforms_Events - Fatal编程技术网

C# 如何从另一个类调用单击事件?

C# 如何从另一个类调用单击事件?,c#,winforms,events,C#,Winforms,Events,我将这两种方法分为1类(CokeMachine类): & 在另一个类(类CokeForm:Form)中,我正在为应用程序构建表单。 其中一部分是: CokeMachine Machine = new CokeMachine(); Machine.Coin1.Text = "Test"; this.Controls.Add(Machine.Coin1); 但是当我点击这个按钮时,它什么也没显示。有人能帮我解决这个问题吗? 提前谢谢 注:完整代码: using System; using Sys

我将这两种方法分为1类(CokeMachine类):

&

在另一个类(类CokeForm:Form)中,我正在为应用程序构建表单。 其中一部分是:

CokeMachine Machine = new CokeMachine();

Machine.Coin1.Text = "Test";
this.Controls.Add(Machine.Coin1);
但是当我点击这个按钮时,它什么也没显示。有人能帮我解决这个问题吗? 提前谢谢

注:完整代码:

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 DrinkButtonsEvents()
    {
        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;
        MessageBox.Show("Does this click work?");
    }

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

        //TextBox1.SelectionStart = TextBox1.Text.Length;
        //TextBox1.ScrollToCaret();
        //TextBox1.Refresh();


        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 will be here

        Machine.Coin1.Text = "Test";
        this.Controls.Add(Machine.Coin1);


        // 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 will be here
   }
}

public class Test
{

    public static void Main()
    {                  

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

MoneyButtonEvents
从未被调用。

我没有看到您的
炼焦机类的初始值设定项

尝试将其添加到您的
炼焦机
类:

public  CokeMachine()
{
    MoneyButtonEvents();
    DrinkButtonsEvents();
}
这将导致在创建类时添加处理程序

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 DrinkButtonsEvents()
    {
        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;
        MessageBox.Show("Does this click work?");
    }

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

        //TextBox1.SelectionStart = TextBox1.Text.Length;
        //TextBox1.ScrollToCaret();
        //TextBox1.Refresh();


        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 will be here

        Machine.Coin1.Text = "Test";
        this.Controls.Add(Machine.Coin1);


        // 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 will be here
   }
}

public class Test
{

    public static void Main()
    {                  

        CokeForm ColaForm;
        ColaForm = new CokeForm();
        Application.Run(ColaForm);
    }
  }
}
public  CokeMachine()
{
    MoneyButtonEvents();
    DrinkButtonsEvents();
}