C# 如何对所选列表框项设置条件?

C# 如何对所选列表框项设置条件?,c#,listbox,C#,Listbox,我真不敢相信我被这个小问题弄得精疲力竭,因为我试图找到解决办法,玩智能感知。刚启动c#GUI就没什么运气了。只是需要一个快速的回答 if (listBox1.SelectedValue== "Chicken") { total += 15; ord += " Chicken"; label4.Text = "chicken selected"; } 这到底是怎么回事。 我想在用户从listbox1中选择项目“chicken”时执行这些语句 public partial

我真不敢相信我被这个小问题弄得精疲力竭,因为我试图找到解决办法,玩智能感知。刚启动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();
    }
}
公共部分类表单1:表单
{
双倍合计=0;
int x=0;
字符串ord=“”;
私有void placeToolStripMenuItem\u单击(对象发送者,事件参数e)
{
checkBox1.Checked=false;
radioButton1.选中=假;
radioButton2.选中=错误;
radioButton3.选中=错误;
radioButton4.选中=错误;
listBox1.SelectedItems.Clear();
如果(复选框1.选中)
{
总数+=1;
ord+=“水”;
}
如果(复选框1.Text==“额外肉类”)
{
总数+=1;
ord+=ord+“额外肉类”;
}
如果(comboBox1.Text==“额外大米”)
{
总数+=1;
ord+=“额外大米”;
}
如果(comboBox1.Text==“额外蔬菜”)
{
总数+=1;
ord+=“额外的蔬菜”;
}
如果(listBox1.SelectedValue==“Chicken”)
{ 
总数+=15;
ord+=“鸡肉”;
label4.Text=“选中的鸡”;
}
如果(listBox1.Text==“比萨饼$8”)/<我用Intelissense解决这个问题的可悲尝试
{ 
总数+=8;
ord+=“披萨”;
label4.Text=“qwe”;
}
如果(listBox1.SelectedItem==“意大利面$12”)/<我可怜的尝试用IntelliSense解决它
{ 
总数+=12;
ord+=“意大利面”;
}
如果(listBox1.SelectedItem==“薯条$8”)
{ 
总数+=8;
ord+=“薯条”;
}
如果(listBox1.SelectedItem==“汉堡$10”)
{ 
总数+=10;
ord+=“汉堡”;
}
//单选按钮
如果(radioButton1.选中)
{ 
总数+=5;
ord+=“菠萝汁”;
}
如果(radioButton2.选中)
{
总数+=6;
ord+=“芒果汁”;
}
如果(收音机按钮3.选中)
{
总数+=7;
ord+=“苹果汁”;
}
如果(收音机按钮4.选中)
{
总数+=8;
ord+=“橙汁”;
}
MessageBox.Show(“订单完成”);
}
私有void clearToolStripMenuItem\u单击(对象发送方,事件参数e)
{
ord=“”;
总数=0;
}
私有void displayToolStripMenuItem\u单击(对象发送方,事件参数e)
{
显示(“订单:+ord+”\n总计:+total”);
}
private void exitToolStripMenuItem\u单击(对象发送者,事件参数e)
{
这个。关闭();
}
}

您需要使用SelectedItem

if (listBox1.SelectedItem == "Chicken")

第一种方法:

if (listBox1.SelectedIndex != -1)    //first check if any item is selected
    if (listBox1.SelectedItem.ToString() == "Chicken")
if (listBox1.Text == "Chicken")
第二种方法:

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
状态。

您的代码无法工作,因为您在处理订单之前清除了所有选择。将此代码移动到
placeToolStripMenuItem\u单击方法的最末端:

// 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
-语句。而是创建一个
产品

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";
}
还将
总计
声明为
十进制
。双倍运算有利于科学计算,但很容易产生类似
7.4999999
的结果,而不是
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;
    }
}
foreach(Controls.OfType()中的var radioButton){
如果(单选按钮。选中){
var p=(产品)radioButton.Tag;
总+=p.价格;
ord+=“”+p.名称;
}
}

我的程序正在向我抛出Nullreferenceexception。
在这一行: 如果(listBox1.SelectedItem.ToString()=“鸡肉$15”)
我想我必须初始化它,但我不知道如何初始化列表框。

出现了什么问题?显示一些上下文。这段代码什么时候运行?错误是什么?total和ord在哪里声明?答案取决于您添加到列表框中的内容。串?一些东西?谢谢。我刚刚开始c#所以我意识到我的方法是有问题的,我怎样才能使这项工作成功,兄弟?它不适用于meDid您是否删除了该行?但现在,当单击按钮时,如何取消选择该项目?当显示消息时,
Order done
。您可以将该行放在消息框显示之后。