【发布时间】:2015-10-02 13:38:44
【问题描述】:
我需要将价格从List<Ingredient> myIngredient 中取出并放入 Windows 窗体上的标签中。代码是我到目前为止所拥有的,我需要添加什么来实现这一点,我已经这样做了好几天,但似乎无法实现。在处理控制台应用程序时我很好,但窗口窗体有点困难..
这是 Windows 窗体页面
public partial class Form1 : Form
{
List<Ingredient> myIngredient = new List<Ingredient>();
public Form1()
{
InitializeComponent();
myIngredient.Add(new Ingredient("Cheese", "grams", 3, "grab cheese and sprinkle on top of pizza"));
myIngredient.Add(new Ingredient("Olives", "grams", 2, "cut up olives and sprickle on pizza"));
myIngredient.Add(new Ingredient("Ham", "grams", 1, "spread cut up ham over pizza"));
myIngredient.Add(new Ingredient("Pineapple", "grams", 1, "spread cut up chunks over pizza"));
myIngredient.Add(new Ingredient("Pepperoni", "pieces", 1, "place slices on pepperoni on pizza"));
myIngredient.Add(new Ingredient("Onion", "handfuls", 1, "sprinkle cut up onion on pizza, try not to cry"));
myIngredient.Add(new Ingredient("Peppers", "grams", 1, "sprinkle cut up peppers on pizza"));
myIngredient.Add(new Ingredient("Anchovy", "grams", 1, "place on top of pizza"));
myIngredient.Add(new Ingredient("Mushrooms", "grams", 1, "put gently on top of pizza"));
foreach (Ingredient pizza in myIngredient)
if (pizza != null)
{
checkedListBoxIngredients.Items.Add(myIngredient);
}
checkedListBoxIngredients.DataSource = myIngredient;
checkedListBoxIngredients.DisplayMember = "DisplayName";
}
private void checkedListBoxIngredients_ItemCheck(object sender, ItemCheckEventArgs e)
{
string item = checkedListBoxIngredients.SelectedItem.ToString();
if (e.NewValue == CheckState.Checked)
{
yourPizza.Items.Add(item);
}
else
yourPizza.Items.Remove(item);
}
}
这是类成分
class Ingredient
{
public string ToppingName { get; set; }
public string UnitName { get; set; }
public int Cost { get; set; }
public string DescriptionName { get; set; }
int totalCost;
public Ingredient(string pizzaToppingName, string pizzaToppingUnit, int pizzaCost, string pizzaDescription)
{
ToppingName = pizzaToppingName;
UnitName = pizzaToppingUnit;
Cost = pizzaCost;
DescriptionName = pizzaDescription;
}
public string DisplayName
{
get
{
return ToppingName + " " + UnitName;
}
}
public override string ToString()
{
return string.Format("{0} - {1} ${2:F}", ToppingName, UnitName, Cost);
}
}
【问题讨论】:
-
label1.Text = myIngredient.Sum(x=>x.Cost).ToString() -
那怎么绑定到窗体呢?感谢您的快速回复
-
这可能有助于在 winforms 中绑定:stackoverflow.com/questions/9187356/…