如何从所选列表框项中创建条件?

时间:2014-09-11 17:54:30

标签: c# listbox

我无法相信我已经被这个小问题所困扰,试图找到解决智能感知的解决方案。没有运气刚开始使用c#GUI。请快速回答。

if (listBox1.SelectedValue== "Chicken")
{
    total += 15;
    ord += " Chicken";
    label4.Text = "chicken selected";
}

这到底是怎么回事。
我想在用户从listbox1中选择项目“chicken”时执行语句。

public partial class Form1 : Form
{
    double total = 0;
    int x = 0;
    string ord = "";

    private void placeToolStripMenuItem_Click(object sender, EventArgs e)
    {
        checkBox1.Checked = false;
        radioButton1.Checked = false;
        radioButton2.Checked = false;
        radioButton3.Checked = false;
        radioButton4.Checked = false;
        listBox1.SelectedItems.Clear();
        if (checkBox1.Checked)
        {
            total += 1;
            ord += " Water";
        }
        if (checkBox1.Text == "Extra Meat")
        {
            total += 1;
            ord += ord+" Extra Meat ";
        }
        if (comboBox1.Text == "Extra Rice")
        {
            total += 1;
            ord += " Extra Rice";

        }
        if (comboBox1.Text == "Extra Veggies")
        {
            total += 1;
            ord +=" Extra Veggies";
        }

        if (listBox1.SelectedValue== "Chicken")
        { 
            total+=15;
            ord+=" Chicken";
            label4.Text = "chicken selected";
        }
        if (listBox1.Text == "Pizza    $8")  //< my pathetic attempt to figure it out with intelisense
        { 
            total+=8;
            ord+="Pizza ";
            label4.Text = "qwe";
        }
        if (listBox1.SelectedItem == "Spaghetti     $12")//< my pathetic attempt to figure it out with intelisense
        { 
            total+=12;
            ord+=" Spaghetti";
        }
        if (listBox1.SelectedItem == "Fries              $8")
        { 
            total+=8;
            ord+=" Fries";
        }
        if (listBox1.SelectedItem == "Burger          $10")
        { 
            total+=10;
            ord+=" Burger";
        }

        //radiobutton
        if (radioButton1.Checked)
        { 
            total+=5;
            ord += " Pineapple Juice";
        }
        if (radioButton2.Checked)
        {
            total+=6;
            ord += " Mango Juice";
        }
        if (radioButton3.Checked)
        {
            total+=7;
            ord += " Apple Juice";
        }
        if (radioButton4.Checked)
        {
            total+=8;
            ord += " Orange Juice";
        }

        MessageBox.Show("Order Done");
    }

    private void clearToolStripMenuItem_Click(object sender, EventArgs e)
    {
        ord = "";
        total = 0;
    }

    private void displayToolStripMenuItem_Click(object sender, EventArgs e)
    {
        MessageBox.Show("Order: " + ord+"\nTotal: "+total);
    }

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

4 个答案:

答案 0 :(得分:1)

您的代码无法使用,因为您在处理订单之前清除了所有选择。将此代码移至placeToolStripMenuItem_Click方法的最后:

// Process the order here ...
MessageBox.Show("Order Done");

checkBox1.Checked = false;
radioButton1.Checked = false;
radioButton2.Checked = false;
radioButton3.Checked = false;
radioButton4.Checked = false;
listBox1.SelectedItems.Clear();

我认为你的做法是错误的。你不应该做出所有这些if - 陈述。而是创建一个Product

public class Product
{
    public string Name { get; set; }
    public decimal Price { get; set; }

    public override ToString()
    {
        return String.Format("{0}    ${1}", Name, Price);
    }
}

然后将产品添加到列表框中:

listbox1.Items.Add(new Product{ Name = "Chicken", Price = 15 });
listbox1.Items.Add(new Product{ Name = "Pizza", Price = 8 });
...

然后您可以使用这样的选定项目:

var product = listBox1.SelectedItem as Product;
if (product != null) {
    total += product.Price;
    ord += " " + product.Name;
    label4.Text = product.Name + " selected";
}

同时将total声明为decimal。双打有利于科学计算,但很容易产生7.49999999而不是7.5的结果。特别是在货币计算方面引入了小数。它们不是很快,但它们不会在内部将十进制数转换为二进制数,而是保留十进制数。二进制数的问题是,例如1/10无法准确表示,就像1/3无法在十进制系统中准确表示一样。


您甚至可以将产品添加到单选按钮

radioButton3.Tag = new Product{ Name = "Apple Juice", Price = 7 };

更高级的方法是使用Product属性创建自己的单选按钮控件,但此解决方案现在可以执行此操作。

然后你可以像这样循环遍历所有的单选按钮

foreach (var radioButton in Controls.OfType<RadioButton>()) {
    if (radioButton.Checked) {
        var p = (Product)radioButton.Tag;
        total += p.Price;
        ord += " " + p.Name;
    }
}

答案 1 :(得分:0)

您需要使用SelectedItem。

if (listBox1.SelectedItem == "Chicken")

答案 2 :(得分:0)

我的程序正在向我抛出一个Nullreferenceexception 在这条线上:             if(listBox1.SelectedItem.ToString()==“Chicken $ 15”) 我必须初始化它,但我不知道如何初始化listBox。

答案 3 :(得分:-2)

第一种方法:

if (listBox1.SelectedIndex != -1)    //first check if any item is selected
    if (listBox1.SelectedItem.ToString() == "Chicken")

第二种方法:

if (listBox1.Text == "Chicken")

只需删除该行:

private void placeToolStripMenuItem_Click(object sender, EventArgs e)
    {
        listBox1.SelectedItems.Clear();    //remove this line

它会清除您选择的项目,这就是您没有进入任何if条件的原因。

相关问题