使用C#转换距离

时间:2018-03-24 22:36:56

标签: c# string if-statement build

程序问题:我试图在用户输入数据后从一种距离转换为下一种距离。用户必须从一个列表中选择以转换为距离中的另一个列表。例如,在一个列表中选择英寸以转换为另一个列表中的码。

我的代码:

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 Distance_Converter
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void ConvertButton_click(object sender, EventArgs e)
        {
            //int Inches = 1;
            //int Feet = 12;
            //int Yards = 36;
            int distance_to_convert;
            string lengthOption1;
            string lengthOption2;
            int inches_feet;
            int inches_yard;

            lengthOption1 = FromListBox.SelectedItem.ToString();
            lengthOption2 = ToListBox.SelectedItem.ToString();
            distance_to_convert = int.Parse(distancetoconvertTextBox.Text);

            if ((FromListBox.SelectedIndex) != -1 && (ToListBox.SelectedIndex) != -1)   
            {
                switch (lengthOption1)
                {
                    case "Inches":
                        if (lengthOption2 == "Inches")
                        {
                            //object distancetoconvert = null;
                            ConvertedDistanceTextBox = distance_to_convert.ToString();
                        }
                        else if (lengthOption2 == "Feet")
                        {
                            inches_feet = distance_to_convert / 12;
                            ConvertedDistanceTextBox = inches_feet.ToString();
                        }
                        else if (lengthOption2 == "Yards")
                        {
                            inches_yard = distance_to_convert / 36;
                            ConvertedDistanceTextBox = inches_yard.ToString();
                        }
                        break;
                    case "Feet":
                        if (lengthOption2 == "Inches")
                        {
                            int feet_inches = distance_to_convert * 12;
                            ConvertedDistanceTextBox = feet_inches.ToString();
                        }
                        else if (lengthOption2 == "Feet")
                        {
                            ConvertedDistanceTextBox = distance_to_convert.ToString(); ;
                        }
                        else if (lengthOption2 == "Yards")
                        {
                            int feet_yard = distance_to_convert / 3;
                            ConvertedDistanceTextBox = feet_yard.ToString();
                        }
                        break;

                    case "Yards":
                        if (lengthOption2 == "Inches")
                        {
                            int Yards_inches = distance_to_convert * 36;
                            ConvertedDistanceTextBox = Yards_inches.ToString();
                        }
                        else if (lengthOption2 == "Feet")
                        {
                            int Yards_feet = distance_to_convert * 3;
                            ConvertedDistanceTextBox = Yards_feet.ToString();
                        }
                        else if (lengthOption2 == "Yards")
                        {
                            ConvertedDistanceTextBox = distance_to_convert.ToString(); ;
                        }
                        break;
                }
            }

        }
        private void Exitbutton_click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}

我的困境:从各方面来看,代码看起来都是正确的。但是,当我尝试多次将int从int转换为string时,IDE会给我一个红线。代码不会编译并产生构建错误。我想我将不得不创建一个单独的类来从int转换为字符串。

错误说明:"错误CS0029无法隐式转换类型'字符串' to' System.Windows.Forms.TextBox' "

它显示在第42,47,52行或任何以ConvertedDistanceTextBox =开头的行。

道歉,我是编码的新手,我正在努力学习。而且我对stackoverflow相对较新。

1 个答案:

答案 0 :(得分:2)

您应该将字符串值设置为文本框的文本属性

ConvertedDistanceTextBox.Text = inches_yard.ToString();