2015-09-04 56 views
1

我想知道如何將VALUE存儲到列表框中的項目。我目前正在爲我的研究制定一個工作表,我一直在想如何做到這一點。基本上我需要儲存食物的價值(例如西蘭花,麪包,例如:西蘭花的價值爲20卡路里)。將值存儲到列表框項目C#

用戶不能看到值,只有程序可以存儲它。

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

非常感謝您的幫助!

+0

你可以保持字典哪裏食物是關鍵。 –

回答

2

您可以隨時添加支持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 
相關問題