如何在 C# 中的表单加载时使用结构填充列表

How do you populate a list with a structure at form load in C#

我完全迷路了。我需要做的是获取结构,特别是用于填充列表框的 cookie 名称。比当我单击所选项目时它应该更改标签中的数据......还有更多,但这就是我现在所在的位置。是的,这是家庭作业,但我也 30 多岁了,有一份好工作。我只是想学习这些东西,这样我就可以在我的爱好中使用它。所以请只帮忙 no snark about "do your own homework."

        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 Unit_8_Cookie_Scouts
        {
            struct CookieStruct
            {
                public string cookieName;
                public decimal cookiePrice;
                public int inventoryNum;
                public decimal valueOfInventory;
            }

            public partial class CookeScouts : Form
            {
                //create a List as a field
                private List<CookieStruct> cookieList = new List<CookieStruct>();

                List<string> cookieName = new List<string>() { "Peppermint Flatties",   "Chocolate Chippers", "Pecan Paradise", "Sugary Shortcake" };
                List<decimal> cookiePrice = new List<decimal>() { 4.99m, 4.76m, 6.82m, 5.99m };
                List<int> inventoryNum = new List<int>() { 8, 17, 9, 12 };
                List<decimal> valueOfInventory = new List<decimal>() { 39.92m, 80.92m, 61.38m, 71.88m };

                public CookeScouts()
                {
                    InitializeComponent();

                    int Index = lstCookies.SelectedIndex;
                    //update values with index 0
                    tempInfo.cookieName = cookieName(0);
                    //create a temp structure object to add values too
                    CookieStruct tempInfo = new CookieStruct();
                    //add temp structure object to list at form level
                    tempInfo.cookieName = cookieName(Index).Text;
                    //add temp structure to list at form level
                    cookieList.Add(tempInfo);

                    for (int index = 0; index < cookieList.Count; index++)
                    {
                        lstCookies.Items.Add(index);
                    }

                    LoadListBox();
                }

                private void btnUpdate_Click(object sender, EventArgs e)
                {

                }


                ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                ///////////////////////////////////////////////////////// METHODS /////////////////////////////////////////////////////////
                ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                private void LoadListBox()
                {
                    string output;

                    //loop through and add to combo box
                    foreach (CookieStruct tempInfo in cookieList)
                    {
                        //make output line for combo box
                        output = tempInfo.cookieName;

                        //send the output to combo box cboCustomers
                        lstCookies.Items.Add(output);
                    }
                }
            }
        }


 //////////////////////////////CLASS//////////////////////
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace Unit_8_Cookie_Scouts
    {
        class clsCookies
        {
            //delcare variables
            string _ItemType;
            decimal _Price;
            decimal _Inventory;
            decimal _Value;

            //new instance of class
            public clsCookies()
            {
                _ItemType = "";
                _Price = 0;
                _Inventory = 0;
                _Value = 0;
            }

            public clsCookies(string CookieName, decimal CookiePrice, decimal InvAmount, decimal TotalValue)
            {
                _ItemType = CookieName;
                _Price = CookiePrice;
                _Inventory = InvAmount;
                _Value = TotalValue;
            }

            //properties
            public string CookieType
            {
                get { return _ItemType; }
                set { _ItemType = value; }
            }

            public decimal Price
            {
                get { return _Price; }
                set { _Price = value; }
            }

            public decimal Inventory
            {
                get { return _Inventory; }
                set { _Inventory = value; }
            }

            public decimal Value
            {
                get
                {
                    return _Value;
                }
            }

            /////////////////////////////////////////////////////////////////////////////////////
            ///////////////////////////////////  METHODS  ///////////////////////////////////////
            /////////////////////////////////////////////////////////////////////////////////////

            public void UpdateInventory(int InvChanged)
            {
                _Inventory += InvChanged;
            }

            public void UpdateValue(decimal ValueChanged)
            {
                _Value = _Price * _Inventory;
            }
        }
    }

看看答案,应该对你有帮助..

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 Unit_8_Cookie_Scouts
{
    struct CookieStruct
    {
        public string cookieName;
        public decimal cookiePrice;
        public int inventoryNum;
        public decimal valueOfInventory;
    }

    public partial class CookeScouts : Form
    {
        //create a List as a field
        private List<CookieStruct> cookieList = new List<CookieStruct>();


        public CookeScouts()
        {
            InitializeComponent();

            cookieList.Add(new CookieStruct() { cookieName = "Peppermint Flatties", cookiePrice = 4.99m, inventoryNum = 8, valueOfInventory = 39.92m });
            cookieList.Add(new CookieStruct() { cookieName = "Chocolate Chippers", cookiePrice = 4.76m, inventoryNum = 17, valueOfInventory = 80.92m });
            cookieList.Add(new CookieStruct() { cookieName = "Pecan Paradise", cookiePrice = 6.82m, inventoryNum = 9, valueOfInventory = 61.38m });
            cookieList.Add(new CookieStruct() { cookieName = "Sugary Shortcake", cookiePrice = 5.99m, inventoryNum = 12, valueOfInventory = 71.88m });

            LoadListBox();
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            int Index = lstCookies.SelectedIndex;
            //update values with index 0
            tempInfo.cookieName = cookieList[Index].cookieName;
            //create a temp structure object to add values too

        }


        private void LoadListBox()
        {
            string output;

            //loop through and add to combo box
            foreach (CookieStruct tempInfo in cookieList)
            {
                //make output line for combo box
                output = tempInfo.cookieName;

                //send the output to combo box cboCustomers
                lstCookies.Items.Add(output);
            }
        }
    }
}