简体   繁体   English

C#:我对文本框做错了什么?

[英]C#: What am I doing wrong with the textbox?

This is what the app looks like so far.这是该应用程序到目前为止的样子。 The program is suppose to allow the user to enter a distance, then it'll have a "from" box and a "to" box that will convert the distance to something like "from inches to feet" for example.该程序假设允许用户输入距离,然后它将有一个“从”框和一个“到”框,将距离转换为例如“从英寸到英尺”之类的东西。

When i run the program though the to and from boxes are empty.当我运行程序时,虽然 to 和 from 框是空的。 In the boxes there should be inches, feet, and yards listed.在方框中应列出英寸、英尺和码。 What is wrong in my code here?我的代码有什么问题?

namespace DistanceConverter
{
    public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void convertButton_Click(object sender, EventArgs e)
    {

        int fromDistance;
        int toDistance = 0;

        fromDistance = int.Parse(textBox1.Text);
        string measureInput = fromBox.Items[fromBox.SelectedIndex].ToString();
        string measureOutput = toBox.Items[toBox.SelectedIndex].ToString();

        switch (measureInput)
        {
            case "Yards":
                switch (measureOutput)
                {
                    case "Inches":
                        toDistance = fromDistance;
                        break;
                    case "Yards":
                        toDistance = fromDistance * 3;
                        break;
                    case "Feet":
                        toDistance = fromDistance * 3 * 12;
                        break;
                }
                break;
            case "Feet":
                switch (measureOutput)
                {
                    case "Inches":
                        toDistance = fromDistance;
                        break;
                    case "Yards":
                        toDistance = fromDistance / 3;
                        break;
                    case "Feet":
                        toDistance = fromDistance * 12;
                        break;
                }
                break;
            case "Inches":
                switch (measureOutput)
                {
                    case "Inches":
                        toDistance = fromDistance;
                        break;
                    case "Feet":
                        toDistance = fromDistance / 12;
                        break;
                    case "Yards":
                        toDistance = fromDistance / (3 * 12);
                        break;
                }
                break;
        }

        distanceConverted.Text = toDistance.ToString();
    }
}

} }

Your question is somewhat misleading, looking at the comments I presume that you want to populate the values of you combo boxes,你的问题有点误导,看看我认为你想填充组合框的值的评论,

You can do programmatically from the constructor by adding the items您可以通过添加项目从构造函数以编程方式执行

public Form1()
{
    InitializeComponent();

    // Populate fromBox
    fromBox.Items.Add("Inches");
    fromBox.Items.Add("Yards");
    fromBox.Items.Add("Feet");

    // Populate toBox
    toBox.Items.Add("Inches");
    toBox.Items.Add("Yards");
    toBox.Items.Add("Feet");
}

or setting a DataSource或设置DataSource

public Form1()
{
    InitializeComponent();

    var items = new List<string>
    {
        "Inches",
        "Yards",
        "Feet"
    };

    comboBox1.DataSource = items;
    comboBox2.DataSource = items;
}

or you can set the values of the Items property in the properties panel或者您可以在属性面板中设置Items属性的值

在此处输入图像描述

在此处输入图像描述

This is no answer, just some hints in the right direction, cannot post as comment!这不是答案,只是一些正确方向的提示,不能作为评论发表!

First of all you are doing a lot of unnecessary string comparison.首先,您正在做很多不必要的字符串比较。 This is bad practice.这是不好的做法。 Check how I use enum to populate your comboboxes.检查我如何使用enum来填充您的组合框。 Check how I read the selected value from the combobox as well.检查我如何从 combobox 中读取选定的值。 Compare how the button can only be pressed if the value is valid.比较只有在值有效时才能按下按钮的方式。

You could start using a new C# feature, the switch expression.您可以开始使用新的 C# 功能,即 switch 表达式。 My few lines are not complete but should give you a hint in the right direction.我的几行不完整,但应该给你一个正确方向的提示。

    {
        double fromValue;

        enum Units
        {
            Inches,
            Yards,
            Feet
        }

        public Form1()
        {
            InitializeComponent();

            button1.Enabled = false;
            fromBox.DataSource = Enum.GetValues(typeof(Units));
            toBox.DataSource = Enum.GetValues(typeof(Units));
        }


        private double convert(Units from, Units to, double value)
        {
            return (from, to) switch
            {
                (Units.Yards, Units.Inches) => value * 36.0,
                (Units.Yards, Units.Yards) => value,
                //..
            };

        }

        private void button1_Click(object sender, EventArgs e)
        {
            var fromUnit = (Units)fromBox.SelectedItem;
            var toUnit = (Units)toBox.SelectedItem;

            label1.Text = convert(fromUnit, toUnit, fromValue).ToString();
        }



        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            button1.Enabled = Double.TryParse(textBox1.Text, out fromValue);
        }
    }```

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM