C# 用于在窗体上使用大量按钮的高效代码
我的问题不是关于一个问题,而是关于是否有更有效的方法来实现我的程序(货币转换器)我是C#的新手,所以我使用了我所知道的来编写这段代码,但正如你所看到的,如果转换器必须覆盖更多的内容,代码会变得非常长,所以我想知道有没有更好/更有效的方法来完成这项工作?(对不起,没有该表单的图片;我没有足够高的代表添加一个) 您可以将此eventhandler添加到所有按钮中,它将去掉按钮名称的前3个字符,并将其添加到字符串中 编辑:刚刚注意到您需要一个From和To。要获得可读性好的代码,请按照Simon的建议执行,并创建另一个类似的处理程序 您可以将此eventhandler添加到所有按钮中,它将去掉按钮名称的前3个字符,并将其添加到字符串中 编辑:刚刚注意到您需要一个From和To。要获得可读性好的代码,请按照Simon的建议执行,并创建另一个类似的处理程序 您可以将此eventhandler添加到所有按钮中,它将去掉按钮名称的前3个字符,并将其添加到字符串中 编辑:刚刚注意到您需要一个From和To。要获得可读性好的代码,请按照Simon的建议执行,并创建另一个类似的处理程序 您可以将此eventhandler添加到所有按钮中,它将去掉按钮名称的前3个字符,并将其添加到字符串中C# 用于在窗体上使用大量按钮的高效代码,c#,forms,C#,Forms,我的问题不是关于一个问题,而是关于是否有更有效的方法来实现我的程序(货币转换器)我是C#的新手,所以我使用了我所知道的来编写这段代码,但正如你所看到的,如果转换器必须覆盖更多的内容,代码会变得非常长,所以我想知道有没有更好/更有效的方法来完成这项工作?(对不起,没有该表单的图片;我没有足够高的代表添加一个) 您可以将此eventhandler添加到所有按钮中,它将去掉按钮名称的前3个字符,并将其添加到字符串中 编辑:刚刚注意到您需要一个From和To。要获得可读性好的代码,请按照Simon的建议
编辑:刚刚注意到您需要一个From和To。要获得可读性好的代码,请按照Simon的建议执行,并创建另一个类似的处理程序。“名称空间”\u 158.\u 212\u赋值\u 2”-是的,有一种方法可以缩短代码。我建议您研究通用事件处理程序。我建议使用一个类和一个数据结构,允许您使用函数调用获取所有这些信息。字典是快速查找的好工具,你可以不用检查每种类型就计算出数量。这个类应该知道每种货币的转换率每当你有很多非常相似的“东西”要处理时,如果你使用它,你的生活可能会更轻松。如果您需要基于某些唯一属性快速访问一个项目(如分配给
convertingFrom
和convertingTo
的“国家代码”值),您可能需要查看。要将相关信息组合在一起(如国家代码、货币单位代码、货币系数到基本单位等),您可能需要创建自己的小类。@MickyDuncan问题解决了,我只是想寻求任何方法,以便在需要制作更大程序时提高效率。“名称空间_158._212_赋值_2”“-是的,有一种方法可以缩短代码。我建议您研究通用事件处理程序。我建议使用一个类和一个数据结构,允许您使用函数调用获取所有这些信息。字典是快速查找的好工具,你可以不用检查每种类型就计算出数量。这个类应该知道每种货币的转换率每当你有很多非常相似的“东西”要处理时,如果你使用它,你的生活可能会更轻松。如果您需要基于某些唯一属性快速访问一个项目(如分配给convertingFrom
和convertingTo
的“国家代码”值),您可能需要查看。要将相关信息组合在一起(如国家代码、货币单位代码、货币系数到基本单位等),您可能需要创建自己的小类。@MickyDuncan问题解决了,我只是想寻求任何方法,以便在需要制作更大程序时提高效率。“名称空间_158._212_赋值_2”“-是的,有一种方法可以缩短代码。我建议您研究通用事件处理程序。我建议使用一个类和一个数据结构,允许您使用函数调用获取所有这些信息。字典是快速查找的好工具,你可以不用检查每种类型就计算出数量。这个类应该知道每种货币的转换率每当你有很多非常相似的“东西”要处理时,如果你使用它,你的生活可能会更轻松。如果您需要基于某些唯一属性快速访问一个项目(如分配给convertingFrom
和convertingTo
的“国家代码”值),您可能需要查看。要将相关信息组合在一起(如国家代码、货币单位代码、货币系数到基本单位等),您可能需要创建自己的小类。@MickyDuncan问题解决了,我只是想寻求任何方法,以便在需要制作更大程序时提高效率。“名称空间_158._212_赋值_2”“-是的,有一种方法可以缩短代码。我建议您研究通用事件处理程序。我建议使用一个类和一个数据结构,允许您使用函数调用获取所有这些信息。字典是快速查找的好工具,你可以不用检查每种类型就计算出数量。这个类应该知道每种货币的转换率每当你有很多非常相似的“东西”要处理时,如果你使用它,你的生活可能会更轻松。如果您需要基于某些唯一属性快速访问一个项目(如分配给convertingFrom
和convertingTo
的“国家代码”值),您可能需要查看。并将相关信息分组(如国家代码、货币单位代码、货币系数到基本单位等),你可能想创建你自己的小类。@MickyDuncan问题已经解决了,我只是想寻求一些方法,以便在我需要创建一个更大的程序时使它更有效。还可能创建另一个类似于“from”的类.你是指CCDisplay.Text
而不是CCDisplay.Name
?@horHAY有什么方法让我使用吗
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace _158._212_assignment_2
{
public partial class Form1 : Form
{
private double amountToConvert = 0;
private string convertingTo = "";
private string convertingFrom = "";
public Form1()
{
InitializeComponent();
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void NZDOC_Click(object sender, EventArgs e)
{
OCDisplay.Text = "Converting from: NZD";
convertingFrom = "NZ";
}
private void AUDOC_Click(object sender, EventArgs e)
{
OCDisplay.Text = "Converting from: AUD";
convertingFrom = "AU";
}
private void EUROC_Click(object sender, EventArgs e)
{
OCDisplay.Text = "Converting from: EUD";
convertingFrom = "EU";
}
private void GBPOC_Click(object sender, EventArgs e)
{
OCDisplay.Text = "Converting from: GBP";
convertingFrom = "GB";
}
private void CADOC_Click(object sender, EventArgs e)
{
OCDisplay.Text = "Converting from: CAD";
convertingFrom = "CA";
}
private void USDOC_Click(object sender, EventArgs e)
{
OCDisplay.Text = "Converting from: USD";
convertingFrom = "US";
}
//Buttons for the currency you are converting to
private void NZDCC_Click(object sender, EventArgs e)
{
CCDisplay.Text = "Converting to: NZD";
convertingTo = "NZD";
}
private void AUDCC_Click(object sender, EventArgs e)
{
CCDisplay.Text = "Converting to: AUD";
convertingTo = "AUD";
}
private void EURCC_Click(object sender, EventArgs e)
{
CCDisplay.Text = "Converting to: EUR";
convertingTo = "EUR";
}
private void GBPCC_Click(object sender, EventArgs e)
{
CCDisplay.Text = "Converting to: GBP";
convertingTo = "GBP";
}
private void CADCC_Click(object sender, EventArgs e)
{
CCDisplay.Text = "Converting to: CAD";
convertingTo = "CAD";
}
private void USDCC_Click(object sender, EventArgs e)
{
CCDisplay.Text = "Converting to: USD";
convertingTo = "USD";
}
private void Convert_Click(object sender, EventArgs e)
{
double check;
string Amount = currencyInput.Text;
bool result = double.TryParse(Amount, out check);//checks if user input is a integer
if (result)//if input is a integer the code proceeds
{
inputWarning.Text = ("");//removes previous error message if it was triggered
if (convertingFrom == "NZ")
{
amountToConvert = double.Parse(Amount);
amountToConvert = amountToConvert / 1.36;
}
else if (convertingFrom == "AU")
{
amountToConvert = double.Parse(Amount);
amountToConvert = amountToConvert / 1.31;
}
else if (convertingFrom == "GB")
{
amountToConvert = double.Parse(Amount);
amountToConvert = amountToConvert / 0.68;
}
else if (convertingFrom == "EU")
{
amountToConvert = double.Parse(Amount);
amountToConvert = amountToConvert / 0.95;
}
else if (convertingFrom == "CA")
{
amountToConvert = double.Parse(Amount);
amountToConvert = amountToConvert / 1.28;
}
else if (convertingFrom == "US")
{
amountToConvert = double.Parse(Amount);
}
else
{
convertFromWarning.Text = "Please select the currency you are converting from";
}
if (convertingTo == "USD")
{
output.Text = amountToConvert.ToString("F2");
}
else if (convertingTo == "CAD")
{
amountToConvert = amountToConvert * 1.28;
output.Text = amountToConvert.ToString("F2");
}
else if (convertingTo == "NZD")
{
amountToConvert = amountToConvert * 1.36;
output.Text = amountToConvert.ToString("F2");
}
else if (convertingTo == "AUD")
{
amountToConvert = amountToConvert * 1.31;
output.Text = amountToConvert.ToString("F2");
}
else if (convertingTo == "GBP")
{
amountToConvert = amountToConvert * 0.68;
output.Text = amountToConvert.ToString("F2");
}
else if (convertingTo == "EUR")
{
amountToConvert = amountToConvert * 0.95;
output.Text = amountToConvert.ToString("F2");
}
else
{
convertToWarning.Text = "Please select the currency to convert to";
}
}
else
{
inputWarning.Text = " Please enter a valid amount";
}
}
private void Reset_Click(object sender, EventArgs e)
{
amountToConvert = 0;
convertingTo = "";
convertingFrom = "";
CCDisplay.Text = "Converting to:";
OCDisplay.Text = "Converting from:";
output.Text = ("");
currencyInput.Text = ("");
inputWarning.Text = ("");
convertToWarning.Text = ("");
convertFromWarning.Text = ("");
}
}
}
private void Btn_Click(object sender, EventArgs e)
{
convertingFrom = (sender as Button).Text.Substring(0, 3);
CCDisplay.Text = "Converting to: " + convertingFrom;
}