首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在窗体上使用大量按钮的高效代码

在窗体上使用大量按钮的高效代码
EN

Stack Overflow用户
提问于 2015-03-25 13:14:27
回答 1查看 93关注 0票数 0

我的问题与其说是一个问题,不如说是知道是否有一种更有效的方法来实现我的程序(货币转换器)--我是C#的新手,所以我使用了我所知道的来编写这段代码,但是正如您所看到的,如果转换程序必须涵盖更多的事情,代码会变得更长,所以我想知道是否有更好/更有效的方法来完成这个任务?(对不起,没有这张表格的图片,我没有足够高的代表来添加一张)。

代码语言:javascript
复制
    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 = ("");
        }
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-03-25 13:18:31

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

您可以在所有按钮中添加这个均衡器,它将从按钮名称中删除前3个字符,并将其移到字符串中。

编辑:刚刚注意到您需要一个开始和到。为了获得良好的可读性代码,请按照Simon的建议,创建另一个类似的处理程序。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29256730

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档