2 回答
TA贡献1784条经验 获得超2个赞
我cbConversion.SelectedIndex = 0;在 form1_load 中添加以选择组合框的第一个索引,然后在calculateConversions()我在 textoutput.text 中输出的每个条件下使用函数。
然后剩下的你可以做 else if 并研究其他转换公式。
我使用cbConversion.SelectedItem.ToString()了这样您就可以从组合框中获取项目文本以进行比较。
你可以试试这个
string conversions;
decimal meter, feet, centimeter, inches, miles, kilometers;
private void btnCalculate_Click(object sender, EventArgs e)
{
calculateConversions();
}
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
cbConversion.SelectedText = "Miles to Kilometers";
string[] Conversion = new string[] { "Miles to Kilometers",
"Kilometers to Miles",
"Feet to Meters",
"Meters to Feet",
"Inches to Centimeters",
"Centermeters to Inches" };
{ }
for (int i = 0; i < Conversion.Length; i++)
{
cbConversion.Items.Add(Convert.ToString(Conversion[i]));
}
miles = 0m;
kilometers = 0m;
feet = 0m;
meter = 0m;
inches = 0m;
centimeter = 0m;
cbConversion.SelectedIndex = 0;
}
private void cbConversion_SelectedIndexChanged(object sender, EventArgs e)
{
conversions = Convert.ToString(cbConversion.SelectedItem);
IList<string> lstString = new List<string>();
lstString.Add("Miles:");
lstString.Add("Kilometers:");
lstString.Add("Feet:");
lstString.Add("Meters:");
lstString.Add("Inches:");
lstString.Add("Centimeters:");
label2.Text = lstString[cbConversion.SelectedIndex];
IList<string> lstStringTwo = new List<string>();
lstStringTwo.Add("Kilometers:");
lstStringTwo.Add("Miles:");
lstStringTwo.Add("Meters:");
lstStringTwo.Add("Feet:");
lstStringTwo.Add("Centimeters:");
lstStringTwo.Add("Inches:");
label3.Text = lstStringTwo[cbConversion.SelectedIndex];
}
private void calculateConversions()
{
decimal input = Convert.ToDecimal(txtInput.Text);
decimal mileToKM = Convert.ToDecimal(1.609344);
if (cbConversion.SelectedItem.ToString() == "Miles to Kilometers")
{
decimal miles = (input * mileToKM);
txtOutput.Text = miles.ToString();
}
}
输出
TA贡献1853条经验 获得超6个赞
这是我现在使用的代码
private void Form1_Load(object sender, EventArgs e)
{
cbConversion.SelectedText = "Miles to Kilometers";
string[] Conversion = new string[] { "Miles to Kilometers",
"Kilometers to Miles",
"Feet to Meters",
"Meters to Feet",
"Inches to Centimeters",
"Centermeters to Inches" };
{ }
for (int i = 0; i < Conversion.Length; i++)
{
cbConversion.Items.Add(Convert.ToString(Conversion[i]));
}
miles = 0m;
kilometers = 0m;
feet = 0m;
meter = 0m;
inches = 0m;
centimeter = 0m;
cbConversion.SelectedIndex = 0;
}
private void cbConversion_SelectedIndexChanged(object sender, EventArgs e)
{
conversions = Convert.ToString(cbConversion.SelectedItem);
IList<string> lstString = new List<string>();
lstString.Add("Miles:");
lstString.Add("Kilometers:");
lstString.Add("Feet:");
lstString.Add("Meters:");
lstString.Add("Inches:");
lstString.Add("Centimeters:");
label2.Text = lstString[cbConversion.SelectedIndex];
IList<string> lstStringTwo = new List<string>();
lstStringTwo.Add("Kilometers:");
lstStringTwo.Add("Miles:");
lstStringTwo.Add("Meters:");
lstStringTwo.Add("Feet:");
lstStringTwo.Add("Centimeters:");
lstStringTwo.Add("Inches:");
label3.Text = lstStringTwo[cbConversion.SelectedIndex];
}
private void calculateConversions()
{
decimal input = Convert.ToDecimal(txtInput.Text);
decimal mileToKM = Convert.ToDecimal(1.6093);
decimal kMToMile = Convert.ToDecimal(0.6214);
decimal ftToM = Convert.ToDecimal(0.3048);
decimal mToFt = Convert.ToDecimal(3.2808);
decimal inToCm = Convert.ToDecimal(2.54);
decimal cmToIn = Convert.ToDecimal(0.3937);
if (cbConversion.SelectedText.ToString() == "Miles to Kilometers")
{
decimal miles = (input * mileToKM);
txtOutput.Text = miles.ToString();
}
- 2 回答
- 0 关注
- 94 浏览
添加回答
举报