变量中有一部分未读:
decimal convertedDistance = Convert.ToDecimal(tbOutput.Text);
现在,我相信我了解到我已经导致文本框输出是用户插入的数字,而不是应该给出的结果。
我尝试了多种方法来解决此问题,但没有成功,是否有人可以帮助我进行编码?
/*Distance Converter.
* In the English measurement system, 1 yard equals 3 feet and 1 foot equals 12 inches.
* Use this information to create an application that let's the user convert distances to and from inches, feet, and yards.
* The user enters the distance to be converted into a TextBox.
* A Listbox allows the user to select the units being converted from,
* and another ListBox allows the user to select the units being converted to.
* Note: Be sure to handle the situation where the user picks the asme units from both list boxes.
* The converted calue will be the same as the value entered. */
using System;
using System.Windows.Forms;
namespace _26DistanceConverter
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void btExit_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void btConvert_Click(object sender, EventArgs e)
{
//1ft = 12" - ft to inch 1" 1/12ft - inch to ft
//1 yard - 3 feet - yard to ft 1ft 1/3 yards - ft to yard
//1 yard = 3 (ft) x 12" - yard to inch 1" = 1/ (3x12) - inch to yard
const decimal inchToFoot = 1m / 12m;
const decimal inchToYard = 1m / (3m * 12m);
const decimal footToInch = 12m;
const decimal footToYard = 1m/ 3m;
const decimal yardToInch = 3m * 12m;
const decimal yardToFoot = 3m;
//tb Distance Covered equals txtInput
decimal distanceToConvert = Convert.ToDecimal(tbDistanceCovered.Text);
//tb Output = txtOutput
***decimal convertedDistance = Convert.ToDecimal(tbOutput.Text);***
string from = LstFrom.SelectedItem.ToString().ToUpper();
string to = LstTo.SelectedItem.ToString().ToUpper();
tbDistanceCovered.Text = Convert.ToString("n2" + tbOutput.Text);
if (from == "Inches" && to == "Feet")
{
convertedDistance = distanceToConvert * inchToFoot;
}
else if (from == "Inches" && to == "Yards")
{
convertedDistance = distanceToConvert * inchToYard;
}
else if (from == "Feet" && to == "Inches")
{
convertedDistance = distanceToConvert * footToInch;
}
else if (from == "Feet" && to == "Yards")
{
convertedDistance = distanceToConvert * footToYard;
}
else if (from == "Yards" && to == "Inches")
{
convertedDistance = distanceToConvert * yardToInch;
}
else if (from == "Yards" && to == "Feet")
{
convertedDistance = distanceToConvert * yardToFoot;
}
else if (from == "Yards" && to == "Yards")
{
convertedDistance = distanceToConvert;
}
else if (from == "Inches" && to == "Inches")
{
convertedDistance = distanceToConvert;
}
else if (from == "Feet" && to == "Feet")
{
convertedDistance = distanceToConvert; //when using same units
}
else
MessageBox.Show("Please enter a valid number", "Invalid Input");
}
private void btClear_Click(object sender, EventArgs e)
{
LstFrom.ClearSelected();
LstTo.ClearSelected();
tbOutput.Clear();
tbDistanceCovered.Clear();
}
}
}
答案 0 :(得分:0)
尝试一下:
L
还记得删除这些行:
private void btConvert_Click(object sender, EventArgs e)
{
// put this in the begining
decimal convertedDistance = 0;
// rest of your code will provide a value for convertedDistance
// and at the end put the value in output textbox with desired formatting
tbOutput.Text = distanceToConvert.ToString("#.##");
}
和
decimal convertedDistance = Convert.ToDecimal(tbOutput.Text);