gpt4 book ai didi

c# - 如何从Button Click事件获取返回值?

转载 作者:行者123 更新时间:2023-11-30 13:49:24 25 4
gpt4 key购买 nike

我刚刚开始学习C#。我看到一个古老的问题,有人试图制造可口可乐机器,这似乎是一个很好的练习。

但是我被钱按钮卡住了。我无法弄清楚如何将按钮所代表的金额存储在ColaMachine方法可以访问的变量中。

我有以下代码:

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

namespace QuickSharp
{
public class ColaMachine : Form
{
public ColaMachine()
{
this.Text = "Cola Machine";
this.Size = new Size(450 , 500);

//Money & Money Buttons

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

Button MoneyButton1;
MoneyButton1 = new Button();
MoneyButton1.Text = "€0,05";
MoneyButton1.Location = new Point(28,125);
MoneyButton1.Click += new System.EventHandler(this.MoneyButton1_Click);
this.Controls.Add(MoneyButton1);

Button MoneyButton2;
MoneyButton2 = new Button();
MoneyButton2.Text = "€0,10";
MoneyButton2.Location = new Point(28,165);
MoneyButton2.Click += new System.EventHandler(this.MoneyButton2_Click);
this.Controls.Add(MoneyButton2);

Button MoneyButton3;
MoneyButton3 = new Button();
MoneyButton3.Text = "€0,20";
MoneyButton3.Location = new Point(28,205);
MoneyButton3.Click += new System.EventHandler(this.MoneyButton3_Click);
this.Controls.Add(MoneyButton3);

Button MoneyButton4;
MoneyButton4 = new Button();
MoneyButton4.Text = "€0,50";
MoneyButton4.Location = new Point(28,245);
MoneyButton4.Click += new System.EventHandler(this.MoneyButton4_Click);
this.Controls.Add(MoneyButton4);

Button MoneyButton5;
MoneyButton5 = new Button();
MoneyButton5.Text = "€1,00";
MoneyButton5.Location = new Point(28,285);
MoneyButton5.Click += new System.EventHandler(this.MoneyButton5_Click);
this.Controls.Add(MoneyButton5);

Button MoneyButton6;
MoneyButton6 = new Button();
MoneyButton6.Text = "€2,00";
MoneyButton6.Location = new Point(28,325);
MoneyButton6.Click += new System.EventHandler(this.MoneyButton6_Click);
this.Controls.Add(MoneyButton6);

// Drinks & Drink Buttons

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

Button DrinkButton1;
DrinkButton1 = new Button();
DrinkButton1.Text = "Coca-Cola";
DrinkButton1.Location = new Point(328,125);
this.Controls.Add(DrinkButton1);

Button DrinkButton2;
DrinkButton2 = new Button();
DrinkButton2.Text = "Coca-Cola Light";
DrinkButton2.Location = new Point(328,165);
this.Controls.Add(DrinkButton2);

Button DrinkButton3;
DrinkButton3 = new Button();
DrinkButton3.Text = "Fanta";
DrinkButton3.Location = new Point(328,205);
this.Controls.Add(DrinkButton3);

Button DrinkButton4;
DrinkButton4 = new Button();
DrinkButton4.Text = "Sprite";
DrinkButton4.Location = new Point(328,245);
this.Controls.Add(DrinkButton4);

Button DrinkButton5;
DrinkButton5 = new Button();
DrinkButton5.Text = "Spa Blauw";
DrinkButton5.Location = new Point(328,285);
this.Controls.Add(DrinkButton5);

Button DrinkButton6;
DrinkButton6 = new Button();
DrinkButton6.Text = "Red Bull";
DrinkButton6.Location = new Point(328,325);
this.Controls.Add(DrinkButton6);

//Header & Machine Display

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

if(InsertedCoins == 0.00)
TextBox1.Text = "Buy Your Ice Cold Drinks Here!";
else
TextBox1.Text = "Inserted Coins: €" + InsertedCoins;

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

// I tried to get the text scrolling here... :)
TextBox1.SelectionStart = TextBox1.Text.Length;
TextBox1.ScrollToCaret();
TextBox1.Refresh();

this.Controls.Add(TextBox1);
}


public double InsertedCoins;

// Money Button Click Events

private void MoneyButton1_Click(object sender, EventArgs e)
{
InsertedCoins = InsertedCoins + 0.05;
}

private void MoneyButton2_Click(object sender, EventArgs e)
{
InsertedCoins = InsertedCoins + 0.10;
}

private void MoneyButton3_Click(object sender, EventArgs e)
{
InsertedCoins = InsertedCoins + 0.20;
}

private void MoneyButton4_Click(object sender, EventArgs e)
{
InsertedCoins = InsertedCoins + 0.50;
}

private void MoneyButton5_Click(object sender, EventArgs e)
{
InsertedCoins = InsertedCoins + 1.00;
}

private void MoneyButton6_Click(object sender, EventArgs e)
{
InsertedCoins = InsertedCoins + 2.00;
}

private static void Main()
{

ColaMachine Scherm;
Scherm = new ColaMachine();
Application.Run(Scherm);
}
}
}


另外,如果您对我的常规编程有任何建议(例如,使其他尝试阅读我的代码的人更容易理解),请告诉我!

最佳答案

当我想到可乐机时,我会看到该机中每种饮料的按钮,但看不到用于支付不同金额的按钮。也许您的意思是说可乐要花50美分,所以按可乐按钮我要收50美分。

按钮和事件处理程序

当您按下屏幕上的按钮时,它将生成一个单击事件。您需要编写一种方法来响应该点击。我们用于响应事件的任何方法(通常来说)都称为事件处理程序。您必须告诉程序哪些按钮与事件处理程序一起使用。我们称此为注册事件处理程序

按照约定,如果您的按钮名为“ CokeButton”,则与该特定按钮关联的事件处理程序将命名为“ CokeButton_ClickHandler”。或类似的东西。

一般建议

考虑一下您要建模的事物,并在代码中定义事物以反映现实世界。模型中的事物通常最终会以类,类属性和类字段结尾。这些事情通常会在适当的类中作为方法结束。然后,您考虑这些事物如何相互作用。

在开始编写代码之前,您不需要了解有关炼焦机的所有内容。而且您应该一次写一点点,进行测试,然后在测试的基础上进行构建。不要编写复杂的交互代码,然后进行测试。您最终将转圈追逐您的尾巴。写一点,测试一下,重复一次。现在听我说,以后再相信我。写一点,测试一下,重复一次。永远留意这个建议。

因此,这就是我可能会想到的可乐机。首先是一台可乐机。

public class CokeMachine {}


一台可乐机有一个钱槽,一个返回槽和饮料按钮。我真的不能把钱放在老虎机中,所以我会说我要在文本框中键入内容。然后,我将单击一个按钮,焦炭将被分配。我觉得我已经定义了足够的模型以开始使用。关于可乐机还有很多其他事情,但我现在不担心它们。

但是我需要知道每种饮料要花多少钱。

好吧那么必须有“ CokeCost”,“ 7UpCost”等字段。因此,定义它们!我们将逐步弄清如何以及在何处使用它们。

   public class CokeMachine {
Button Coke;
Button 7Up;
Button RootBeer;
TextBox MoneySlot;

double CokeCost = .75;
double 7UpCost = .65;
}


我说过按钮需要处理程序,因此我们至少可以编写一些代码外壳。我希望它们都能以相同的方式工作,所以我现在将重点介绍一个。请注意,在编写代码时,我意识到必须处理的其他事情。我将添加注释,对尚不存在的方法的调用等。

   public class CokeMachine {
Button Coke;
Button 7Up;
Button RootBeer;
TextBox MoneySlot;

double CokeCost = .75;
double 7UpCost = .65;

// "wiring up" the coke button click event to it's handler.
// We do this in C# by declaring an new EventHandler object (a .NET framework supplied class)
// and we pass in the name of our method as a parameter.
// This new EventHandler is *added* to the button's click event.
// An event can have multiple handlers, that's why we do "+="
// instead of just "=". Otherwise we would have accidentally "unhooked" any
// previously registered handlers.
Coke.Click += new EventHandler(Coke_ClickHandler);

// this is the .NET event handler method signature.
Public void Coke_ClickHandler (object sender, EventArgs args){
if (MoneySlot.Value >= CokeCost) {
DispenseDrink();
// How do I handle returning change? Maybe DispenseDrink() can do that.
}else {
// tell customer to put in more money
}
}

private void DispenseDrink() {
// An empty method is enough to get it to compile so for now that's fine.
// I need to test the Coke_EventHandler logic that I've written so far.
}

}


现在,我需要测试到目前为止的内容。之后,我需要决定接下来要关注什么。但是要意识到,当您编写依赖于已编写的代码的新代码时,如果该现有代码尚未经过测试-现在您看到错误,那么您自己就会变得更加困难。您可能已经测试了更简单的代码。现在,它变得越来越复杂,并且将更加难以调试和修复。

建议,第二部分

冒着把事情弄乱的风险,我将以下内容提供给我的原始答案:

您会看到每个饮料按钮都执行相同的操作,并且鉴于以上代码,我们将为每个按钮一遍又一遍地编写相同的逻辑。如果需要任何更改,我们必须在任何地方进行更改。

更多一般建议

一种面向对象的编程启发式方法是封装保持不变的方法。您应该始终在寻找可能成为通用代码候选对象的地方。

我想强调,这种常见的按钮行为对我而言并不是立即显而易见的。直到我写完上面的代码后,我才开始想到我所有的饮料按钮处理程序都将看起来相同,并且我意识到在真正的饮料机上,它们实际上的行为方式相同。我的编码间谍意识告诉我们,当代码反映出您真实事物的可识别行为时,这绝对是一件好事(双关语!)。

重构

实际上,这是一个技术术语,意味着对现有代码进行改编以使其更灵活,可重复使用,可读性强。

重构应该一直存在于您的思维过程中。但是请确保您有进行任何更改的正当理由。重塑代码是软件开发的常规组成部分。

让我们通过提取方法进行重构

     Public void Coke_ClickHandler (object sender, EventArgs args){
PurchaseDrink("Coke", CokeCost);
}

// now we have a method that stands out and says THIS is how it works
// and a single point of change, rather than ump-teen button handlers.
private PurchaseDrink (string whatKind, double cost) {

// all I did so far is move the code and change "Cokecost" to "cost"
// Now I'm beginning to think I may need to pass "whatKind" to
// DispenseDrink() - but first I need to test the changes I've
// made at this level.
// ***** and since I already tested the code when I 1st wrote it,
// this refactoring will be easier & quicker to test.. GET IT??!! ******

if (MoneySlot.Value >= cost) {
DispenseDrink();
// How do I handle returning change? Maybe DispenseDrink() can do that.
}else {
// tell customer to put in more money
}
}

private void DispenseDrink() {
// An empty method is enough to get it to compile so for now that's fine.
// I need to test the Coke_EventHandler logic that I've written so far.
}


枚举

我讨厌像上面使用“可乐”那样使用字符串。错字和大小写(即上/下)可能会导致Visual Studio无法捕获的问题。当我的物品清单很有限时-各种饮料-我真的很喜欢使用枚举。它们显示在智能感知中,我可以在switch语句中使用它们(并研究“类型安全”的概念)。我真正喜欢的是,它们绝对在一处定义了我们程序所知道的所有饮料类型。就像文档!

关于c# - 如何从Button Click事件获取返回值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9651484/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com