【问题标题】:How to get a Object element out of the List and add them together如何从 List 中获取 Object 元素并将它们添加到一起
【发布时间】: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=&gt;x.Cost).ToString()
  • 那怎么绑定到窗体呢?感谢您的快速回复
  • 这可能有助于在 winforms 中绑定:stackoverflow.com/questions/9187356/…

标签: c# binding


【解决方案1】:

嗯...让您开始 - 您不想将列表项单独添加到复选框列表中。您想将整个列表添加为数据源:

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



  checkedListBoxIngredients.DataSource = new BindingSource( myIngredient, null );
  checkedListBoxIngredients.DisplayMember = "DisplayName";

}

BindingSource 是关键。你会想查一下。

当您将列表控件绑定到列表数据源时,您可以直接访问 UI 控件的列表项引用的基础数据项。例如,您可以将所选成分从列表中取出 - 例如在标有“订购披萨”的按钮上:

private void orderPizza_Click( object sender, EventArgs e )
{

  var selectedIngredients =
    new List<Ingredient>
    (
      checkedListBoxIngredients
      .CheckedItems
      .Cast<Ingredient>()
    );

  var ingredientPrice = selectedIngredients.Sum( i=> i.Cost );

  //--> complete the order...
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-31
    • 2021-06-05
    • 1970-01-01
    • 2021-07-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多