将值存储到列表框项C#

时间:2015-09-04 21:00:24

标签: c#

我想知道如何将VALUE存储到列表框中的项目。我目前正在为我的学习制作工作表,我一直在想如何做到这一点。基本上我需要存储食物的价值(例如西兰花,面包。例如:西兰花的价值为20卡路里)。

用户不能看到该值,只有程序可以存储它。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MCAST_Calorie_Counter
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            lbAvailable.Items.Clear();
            if (comboBox1.Text == "Vegetables")
            {
                lbAvailable.Items.Add("Broccoli");
                lbAvailable.Items.Add("Carrots");
                lbAvailable.Items.Add("Lettuce");
                lbAvailable.Items.Add("Onions");
                lbAvailable.Items.Add("Potatoes");
            }

            if (comboBox1.Text == "Meat")
            {
                lbAvailable.Items.Add("Chicken");
                lbAvailable.Items.Add("Veal");
                lbAvailable.Items.Add("Beef");
                lbAvailable.Items.Add("Fish");
            }

            if (comboBox1.Text == "Legumes")
            {
                lbAvailable.Items.Add("Bread");
                lbAvailable.Items.Add("Peanuts");
                lbAvailable.Items.Add("Green Peas");
                lbAvailable.Items.Add("Lentils");
            }
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            ListBox.SelectedObjectCollection highlightedItems = lbAvailable.SelectedItems;

            foreach (var item in highlightedItems)
            {
                lbChosen.Items.Add(item);
            }
            if (lbAvailable.SelectedItems.Count > 0)
            {
                lbAvailable.Items.Remove(lbAvailable.SelectedItems[0]);
            }
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            ListBox.SelectedObjectCollection highlightedItems = lbChosen.SelectedItems;

            foreach (var item in highlightedItems)
            {
                lbAvailable.Items.Add(item);
            }
            if (lbChosen.SelectedItems.Count > 0)
            {
                lbChosen.Items.Remove(lbChosen.SelectedItems[0]);
            }
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            lbAvailable.Items.Clear();
            lbChosen.Items.Clear();
            if (comboBox1.Text == "Vegetables")
            {
                lbAvailable.Items.Add("Broccoli");
                lbAvailable.Items.Add("Carrots");
                lbAvailable.Items.Add("Lettuce");
                lbAvailable.Items.Add("Onions");
                lbAvailable.Items.Add("Potatoes");
            }

            if (comboBox1.Text == "Meat")
            {
                lbAvailable.Items.Add("Chicken");
                lbAvailable.Items.Add("Veal");
                lbAvailable.Items.Add("Beef");
                lbAvailable.Items.Add("Fish");
            }

            if (comboBox1.Text == "Legumes")
            {
                lbAvailable.Items.Add("Bread");
                lbAvailable.Items.Add("Peanuts");
                lbAvailable.Items.Add("Green Peas");
                lbAvailable.Items.Add("Lentils");
            }
        }
    }
}

非常感谢你的帮助!

1 个答案:

答案 0 :(得分:3)

您始终可以将支持ToString()的自定义对象添加到ListBox.Items集合中:

struct Record
{
    int value;
    string label;

    public override string ToString()
    {
        return label;
    }
}

添加自定义对象:

Record record = new Record();
record.value = 1;
record.label = "This text will appear in the ListBox";
listBox.Items.Add(record);

检索它:

Record selectedRecord = (Record)listBox.SelectedItem;
Console.WriteLine(selectedRecord.value); // => 1