简体   繁体   中英

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. 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

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

在此处输入图像描述

在此处输入图像描述

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. Check how I read the selected value from the combobox as well. Compare how the button can only be pressed if the value is valid.

You could start using a new C# feature, the switch expression. 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);
        }
    }```

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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