在从每个列表框中选择一个选项时尝试进行输出
本文关键字:选项 一个 输出 列表 选择 | 更新日期: 2023-09-27 18:27:26
当我从两个具有相同测量词的不同列表框中进行选择时,我正在尝试执行三个选项中的一个。
用户在文本框中输入一个数字,然后从FromList列表框中的3个单位中选择一个(单位为英寸、英尺和码),然后从ToList列表框的3个单元中选择一一个(其中的单位也为英寸、脚和码)。
单击"计算"按钮时,它会将测量英寸转换为英寸(不计算,只需吐出他们在标签上输入的内容),或者将英寸转换为英尺或英寸转换为码(如果FromList选择了英寸),并将输出显示到标签上。它也可以做英尺到英寸,英尺到英尺(不用计算),英尺到码。最后,它会做码到英寸,码到英尺,码到码(不计算)。
两个列表框都将SelectionMode设置为One,因此用户只能从每个列表框中选择一个项目。我可以用SelectedItem将第一个列表框设置为一个值,但它突出显示的代码中的9个错误是,如果我试图在第二个列表框中的一个项目被选中时设置它。以下是我迄今为止所尝试的:
public frmConverter()
{
InitializeComponent();
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
private void btnClear_Click(object sender, EventArgs e)
{
txtEntered.Text = "";
lblOutput.Text = "";
}
private void btnCalc_Click(object sender, EventArgs e)
{
string Fromlist;
string ToList;
double Entered, Output;
Entered = Convert.ToDouble(txtEntered.Text);
if (FromList == "Inches" && ToList == "Inches")
{
lblOutput.Text = txtEntered.Text + " Inches";
}
else if (FromList == "Inches" && ToList =="Feet")
{
Output = Entered / 12;
lblOutput.Text = Output.ToString("N2") + (" Feet");
}
else if (FromList == "Inches" && ToList == "Yards")
{
Output = Entered / 36;
lblOutput.Text = Output.ToString("N2") + (" Yards");
}
else if (FromList == "Feet" && ToList == "Inches")
{
Output = Entered * 12;
lblOutput.Text = Output.ToString("N2") + (" Inches");
}
else if (FromList == "Feet" && ToList == "Feet")
{
lblOutput.Text = txtEntered.Text + " Feet";
}
else if (FromList == "Feet" && ToList == "Yards")
{
Output = Entered / 3;
lblOutput.Text = Output.ToString("N2") + (" Yards");
}
else if (FromList == "Yards" && ToList == "Inches")
{
Output = Entered * 36;
lblOutput.Text = Output.ToString("N2") + (" Inches");
}
else if (FromList == "Yards" && ToList == "Feet")
{
Output = Entered * 12;
lblOutput.Text = Output.ToString("N2") + (" Feet");
}
else if (FromList == "Yards" && ToList == "Yards")
{
lblOutput.Text = txtEntered + " Yards";
}
}
}
问题之一是在if语句中使用赋值运算符=
,而不是相等运算符==
。
if (FromList.SelectedItem.ToString() == "Inches" && ToList.SelectedItem.ToString() == "Inches")
{
lblOutput.Text = txtEntered.Text + " Inches";
}
此外,CCD_ 3从未被赋予值。
您可能需要考虑的是,使用枚举来简化代码并消除所有if
语句:
enum Conversions
{
Inches = 1,
Feet = 12,
Yards = 36,
}
double DoConversion(string from, string to, double quantity)
{
Conversions convertFrom = Conversions.Inches;
Conversions convertTo = Conversions.Inches;
if (Enum.TryParse<Conversions>(from, out convertFrom) && Enum.TryParse<Conversions>(to, out convertTo))
{
return quantity * ((double)convertFrom / (double)convertTo);
}
else
{
return 0;
}
}
此函数将From/To单位作为字符串,并用一个double表示数量并进行转换。它不是用不同的算法进行乘法或除法运算,而是用2个转换值
问题是您将FromList和ToList作为字符串,并试图从FromList/ToList中推断SelectedItem属性,但在FromList/to List中不存在这样的属性,因为您在代码中将它们作为字符串。您可能需要使用在设计中定义的Listcontrols,而不是在代码中使用字符串对象。
private void btnCalc_Click(object sender, EventArgs e)
{
string Fromlist; // this is string so will not work for you
string ToList; // this is string so will not work for you
string Inches;
string Feet;
string Yards;
double Entered, Output;
//as FromList and ToList are string defined above below logic will not work as there is no property like SelectedItem in string.
if (FromList.SelectedItem = "Inches" && ToList.SelectedItem = "Inches")
{
}
好的,所以我用switch和parse重新编码了它,现在它按预期工作了。感谢大家的帮助!这是工作代码:
public Form1()
{
InitializeComponent();
}
private void btnClear_Click(object sender, EventArgs e)
{
txtEntered.Text = "";
lblOutput.Text = "";
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
private void btnCalc_Click(object sender, EventArgs e)
{
string From;
string To;
double Entered, Output;
Entered = double.Parse(txtEntered.Text);
if (FromList.SelectedIndex != -1)
{
From = FromList.SelectedItem.ToString();
To = ToList.SelectedItem.ToString();
switch (From)
{
case "Inches":
switch (To)
{
case "Inches":
Output = Entered;
lblOutput.Text = Output.ToString("N2") + " Inches";
break;
case "Feet":
Output = Entered / 12;
lblOutput.Text = Output.ToString("N2") + " Feet";
break;
case "Yards":
Output = Entered / 36;
lblOutput.Text = Output.ToString("N2") + " Yards";
break;
}
break;
case "Feet":
switch (To)
{
case "Inches":
Output = Entered * 12;
lblOutput.Text = Output.ToString("N2") + " Inches";
break;
case "Feet":
Output = Entered;
lblOutput.Text = Output.ToString("N2") + " Feet";
break;
case "Yards":
Output = Entered / 3;
lblOutput.Text = Output.ToString("N2") + " Yards";
break;
}
break;
case "Yards":
switch (To)
{
case "Inches":
Output = Entered * 36;
lblOutput.Text = Output.ToString("N2") + " Inches";
break;
case "Feet":
Output = Entered * 3;
lblOutput.Text = Output.ToString("N2") + " Feet";
break;
case "Yards":
Output = Entered;
lblOutput.Text = Output.ToString("N2") + " Yards";
break;
}
break;
}
}
}
}
}