在表单上使用大量按钮的高效代码

时间:2015-03-25 13:14:28

标签: c# forms

我的问题不是关于一个问题而是更多关于知道是否有一种更有效的方式来实现我的程序(货币转换器)我是C#的新手所以我已经用我所知的来制作这个代码但是你可以看看转换器是否必须覆盖更多的东西代码会变得非常漫长,所以我想知道有没有更好/更有效的方法呢? (对不起,没有形式的图片;我没有足够高的代表来添加一个)。

    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 = ("");
        }
    }
}

1 个答案:

答案 0 :(得分:3)

    private void Btn_Click(object sender, EventArgs e)
    {
        convertingFrom = (sender as Button).Text.Substring(0, 3);
        CCDisplay.Text = "Converting to: " + convertingFrom;
    }

您可以将此事件处理程序添加到所有按钮,它将从按钮名称中删除前3个字符,并将其添加到字符串中。

编辑:刚刚注意到你需要一个From和To。对于良好的可读代码,请像Simon建议的那样创建另一个类似的处理程序。