C#自己写的第一个小程序,庆祝一下

Packages.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Examination
{
    public class Packages
    {
        public string Name
        {
            get;
            set;
        }

        public Dictionary<string, Module> Items
        {
            get;
            set;
        }

        public double Price
        {
            get
            {
                return Get_Price();          
            }
        }

        public Packages()
        {
            Items = new Dictionary<string, Module>();
        }

        public Packages(string n,Dictionary<string,Module> it)
        {
            Name = n;
            Items = it;
        }

        public double Get_Price()
        {
            double result = 0;
            if (Items.Count > 0)
            {
                foreach(Module mo in Items.Values)
                {
                    result += mo.Price;
                }
                
            }
            return result;
        }


    }
}

 

Module.cs

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Examination
{
    public class Module
    {

        public string Name
        {
            get;
            set;
        }

        public double Price
        {
            get;
            set;
        }

        public string Remark
        {
            get;
            set;
        }

        public Module()
        {

        }

        public Module(string n, double p, string r)
        {
            Name = n;
            Price = p;
            Remark = r;
        }
    }
}

 

Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Examination
{
    public partial class Form1 : Form
    {
        Module m1, m2, m3, m4, m5, m6, m7, m8;
        Packages p1, p2, p3;
        Dictionary<string, Module> AllModule;
        Dictionary<string, Packages> AllPackages;

        public Form1()
        {
            InitializeComponent();
        }

        private void InitDisplay()
        {
            m1 = new Module("Arduino UNO R3",40,"核心板");
            m2 = new Module("Arduino面包板",10,"做试验用");
            m3 = new Module("Arduino面包板线",10,"做试验用");

            m4 = new Module("16*16点阵模块", 39, "能显示单个汉字的点阵模块");
            m5 = new Module("LCD12864模块", 39, "大液晶");

            m6 = new Module("GPS模块", 100, "采用ublox模块");
            m7 = new Module("SIM900A模块", 100, "采用SIM900A模块");

            AllModule = new Dictionary<string, Module>();
            AllModule.Add(m1.Name, m1);
            AllModule.Add(m2.Name, m2);
            AllModule.Add(m3.Name, m3);
            AllModule.Add(m4.Name, m4);
            AllModule.Add(m5.Name, m5);
            AllModule.Add(m6.Name, m6);
            AllModule.Add(m7.Name, m7);

            Dictionary<string, Module> P1_Module = new Dictionary<string, Module>();
            P1_Module.Add(m1.Name, m1);
            P1_Module.Add(m2.Name, m2);
            P1_Module.Add(m3.Name, m3);
            p1 = new Packages("基础版",P1_Module);

            Dictionary<string, Module> P2_Module = new Dictionary<string, Module>();
            P2_Module.Add(m1.Name, m1);
            P2_Module.Add(m2.Name, m2);
            P2_Module.Add(m3.Name, m3);
            P2_Module.Add(m4.Name, m4);
            P2_Module.Add(m5.Name, m5);
            p2 = new Packages("提高版", P2_Module);

            Dictionary<string, Module> P3_Module = new Dictionary<string, Module>();
            P3_Module.Add(m1.Name, m1);
            P3_Module.Add(m2.Name, m2);
            P3_Module.Add(m3.Name, m3);
            P3_Module.Add(m4.Name, m4);
            P3_Module.Add(m5.Name, m5);
            P3_Module.Add(m6.Name, m6);
            P3_Module.Add(m7.Name, m7);
            p3 = new Packages("至尊版", P3_Module);

            AllPackages = new Dictionary<string, Packages>();
            AllPackages.Add(p1.Name, p1);
            AllPackages.Add(p2.Name, p2);
            AllPackages.Add(p3.Name, p3);           
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            InitDisplay();
            cb_taocan.Text = "请选择套餐";
            foreach(Packages pk in AllPackages.Values)
            {
                cb_taocan.Items.Add(pk.Name);
            }

            ListViewItem lvi;
            foreach (Module md in AllModule.Values)
            {
                clb1.Items.Add(md.Name);
            }

        }

        private void cb_taocan_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cb_taocan.Text == p1.Name)
            {
                DisplayPackage(p1);
            }
            else if (cb_taocan.Text == p2.Name)
            {
                DisplayPackage(p2);
            }
            else if (cb_taocan.Text == p3.Name)
            {
                DisplayPackage(p3);
            }
        }

        private void DisplayPackage(Packages p)
        {
            lv1.Items.Clear();
            ListViewItem lvi;
            foreach(Module m in p.Items.Values)
            {
                lvi = new ListViewItem(m.Name);
                lvi.SubItems.Add(m.Price.ToString());
                lvi.SubItems.Add(m.Remark);

                lv1.Items.Add(lvi);
            }
            lbl_name.Text = p.Name;
            lbl_price.Text = p.Price.ToString(); 
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (cb_taocan.Text != "请选择套餐" && clb1.CheckedItems.Count > 0)
            {
                Packages pa;
                for (int i = 0; i < clb1.CheckedItems.Count; i++)
                {
                    pa = AllPackages[cb_taocan.Text];
                    Module md = AllModule[clb1.CheckedItems[i].ToString()];

                    if (!pa.Items.ContainsValue(md))
                    {
                        pa.Items.Add(md.Name, md);
                        DisplayPackage(pa);
                    }
                    else
                    {
                        MessageBox.Show(md.Name + "已经存在了哦!");
                    }

                }
            }
            else
            {
                MessageBox.Show("别想糊弄我!你就没选择套餐/模块 --!");
            }
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if(checkBox1.Checked)
            {
                for (int i = 0; i < clb1.Items.Count; i++)
                {
                    clb1.SetItemChecked(i, true);
                }
            }
            else
            {
                for (int i = 0; i < clb1.Items.Count; i++)
                {
                    clb1.SetItemChecked(i, false);
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {

            if (lv1.SelectedItems.Count > 0)
            {
                Packages pa = AllPackages[cb_taocan.Text];

                foreach (ListViewItem lvi in lv1.SelectedItems)
                {
                    Module md = pa.Items[lvi.Text];

                    if (pa.Items.ContainsValue(md))
                    {
                        pa.Items.Remove(md.Name);
                    }
                }
                DisplayPackage(pa);
            }
            else
            {
                MessageBox.Show("啥都没选,移除毛线啊!");
            }

            
        }
    }
}

显示效果

1

posted on 2013-11-08 17:36  神秘藏宝室  阅读(674)  评论(0编辑  收藏  举报

 >>>转载请注明出处<<<