Как вы заполняете список со структурой при загрузке формы в C # - PullRequest
0 голосов
/ 11 ноября 2018

Я просто потерян. Что мне нужно сделать, это получить структуру, в частности имя cookie, чтобы заполнить список. Чем когда я нажимаю на выбранный элемент, это должно изменить данные в метках ... это еще не все, но это то, где я сейчас нахожусь. Да, это домашнее задание, но мне уже за тридцать с хорошей работой. Я просто пытаюсь изучить этот материал, чтобы я мог использовать его в своих хобби. Так что, пожалуйста, помогите только не выдумывать о «делать домашнее задание».

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

1 Ответ

0 голосов
/ 11 ноября 2018

Посмотрите на ответ, должен помочь вам ..

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);
            }
        }
    }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...