尝试在选择每个列表框时进行输出

时间:2014-10-22 03:03:06

标签: c# listboxitem

从2个具有相同测量单词的不同列表框中进行选择时,我尝试从3个选项中选择1个。

用户在文本框中输入一个数字,然后在FromList列表框中选择3个单位中的一个(单位为英寸,英尺和码),然后在“列表”列表框中选择3个单位中的一个(单位为还有英寸,英尺和码。)

当点击计算按钮时,它会将测量英寸转换为英寸(不做数学,只是吐出他们输入到标签的内容)或英寸到英尺或英寸到码,如果FromList选择了英寸,并显示输出到标签。它也可以做到英尺,英尺到英尺(没有数学),脚到码。最后,它会做到英寸,码距离,码距离(不做数学)。

两个列表框都将SelectionMode设置为One,因此用户只能从每个列表框中选择1个项目。我可以将第一个列表框设置为具有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";
        }
    }
} 

3 个答案:

答案 0 :(得分:1)

您的一个问题是您在if语句中使用赋值运算符=而不是相等运算符==

if (FromList.SelectedItem.ToString() == "Inches" && ToList.SelectedItem.ToString() == "Inches")
{
    lblOutput.Text = txtEntered.Text + " Inches";
}

Entered永远不会给出值。

您可能想要考虑的事情是使用枚举来简化代码并删除所有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个转换值的除法

答案 1 :(得分:0)

问题是您已将FromList和ToList作为字符串,并且您正尝试从FromList / ToList中选择SelectedItem属性,但是在FromList / ToList中不存在这样的属性,因为您已将它们作为代码中的字符串。可能是您需要使用在设计中定义的Listcontrol而不是在代码中使用字符串对象。

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")
        {
}

答案 2 :(得分:0)

好的,所以我使用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;
            }
        }

        }
    }
}