前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >体检套餐管理系统 -- Dictionary<K,V>双列集合

体检套餐管理系统 -- Dictionary<K,V>双列集合

作者头像
房上的猫
发布2018-03-14 12:41:32
9221
发布2018-03-14 12:41:32
举报
文章被收录于专栏:个人随笔

本文章为 Dictionary<K,V>双列集合开发项目,如需要List<T>单列集合开发的此项目,请到楼主博客园寻找

博客网址:http://www.cnblogs.com/lsy131479/

窗体

一.首先创建项目类

代码语言:javascript
复制
 public class HealthCheckItem
    {
        //项目描述
        private string description;
        //项目名称
        private string name;
        //项目价格
        private int price;

        //无参构造
        public HealthCheckItem()
        {
        }

        //有参构造
        public HealthCheckItem(string description, string name, int price)
        {
            this.description = description;
            this.name = name;
            this.price = price;
        }

        public string Description { get => description; set => description = value; }
        public string Name { get => name; set => name = value; }
        public int Price { get => price; set => price = value; }
    }

二.创建套餐类

代码语言:javascript
复制
/// <summary>
    /// 套餐类
    /// </summary>
   public class HealthCheckSet
    {
        //套餐名
        private string name;
        //套餐总价格
        private int price;
        //存储套餐内的项目
         private Dictionary<string, HealthCheckItem> items = new Dictionary<string, HealthCheckItem>();

        //无参构造
        public HealthCheckSet()
        {
        }
        //有参构造
        public HealthCheckSet(string name)
        {
            this.name = name;
            
        }

        

        public string Name { get => name; set => name = value; }
        public int Price { get => price; set => price = value; }
        public Dictionary<string, HealthCheckItem>{ get => items; set => items = value; }
    }

三.主窗体代码

代码语言:javascript
复制
   /// <summary>
    /// 体检套餐管理系统 -- Dictionary<K,V>双列集合
    /// </summary>
    public partial class FrmMain : Form
    {
        public FrmMain()
        {
            InitializeComponent();
        }

        private void FrmMain_Load(object sender, EventArgs e)
        {
            //删除多余列
            this.dataGridView1.AutoGenerateColumns = false;
            //清楚多余行
            this.dataGridView1.AllowUserToAddRows = false;

            /*
             * 套餐下拉框初始化
             * */
            HealthCheckSet set0 = new HealthCheckSet("请选择");
            HealthCheckSet set1 = new HealthCheckSet("入学体检");
            set.Add(set0.Name, set0);
            set.Add(set1.Name, set1);
            AddExamCbo();
        }

        //刷新项目下拉框
        public void AddExamCbo()
        {
            BindingSource source = new BindingSource();
            source.DataSource = set.Keys;
            this.cboExams.DataSource = source;
        }
        //套餐集合
        Dictionary<string, HealthCheckSet> set = new Dictionary<string, HealthCheckSet>();
        //初始化项目集合
        Dictionary<string, HealthCheckItem> allItems = new Dictionary<string, HealthCheckItem>();
        public void AddPhyCbo()
        {
            /*
             * 初始化(刷新)项目下拉框
             * */
            allItems.Clear();
            HealthCheckItem item1 = new HealthCheckItem("用于检查身高。", "身高", 5);
            HealthCheckItem item2 = new HealthCheckItem("用于检查体重。", "体重", 5);
            HealthCheckItem item3 = new HealthCheckItem("用于检查肝功能。", "肝功能", 50);
            HealthCheckItem item4 = new HealthCheckItem("用于检查视力。", "视力", 5);
            HealthCheckItem item5 = new HealthCheckItem("用于检查听力。", "听力", 5);
            HealthCheckItem item6 = new HealthCheckItem("用于检查B超。", "B超", 80);
            HealthCheckItem item7 = new HealthCheckItem("用于检查心电图。", "心电图", 100);
            allItems.Add(item1.Name, item1);
            allItems.Add(item2.Name, item2);
            allItems.Add(item3.Name, item3);
            allItems.Add(item4.Name, item4);
            allItems.Add(item5.Name, item5);
            allItems.Add(item6.Name, item6);
            allItems.Add(item7.Name, item7);
            this.cboPhy.DisplayMember = "name";
            BindingSource source = new BindingSource();
            source.DataSource = allItems.Keys;
            this.cboPhy.DataSource = source;
        }

        private void cboExams_SelectedIndexChanged(object sender, EventArgs e)
        {
            /*
             * 改变下拉框下标,判断下标。改变状态
             * */
            this.lblNames.Text = this.cboExams.Text;
            RenovateDgv();
            Btn();
            if (this.cboExams.SelectedIndex > 0)
            {
                this.btnAdd.Enabled = true;
                AddPhyCbo();
            }
            else
            {
                this.btnAdd.Enabled = false;
                this.cboPhy.DataSource = null;
            }
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            //非空验证
            if (txtNewName.Text == "" || txtNewName.Text == null)
            {
                MessageBox.Show("请输入套餐名称!");
                return;
            }
            /*
             * 集合添加套餐名
             * 并刷新下拉框
             * */
            HealthCheckSet set3 = new HealthCheckSet(this.txtNewName.Text);
            set.Add(set3.Name, set3);
            AddExamCbo();
            this.cboExams.Text = set3.Name;
            MessageBox.Show("添加成功!");
        }

        public void AddDgv()
        {
            //ToList()将双列集合转为单列集合
            //Contains()在单列集合中寻找是否已存在该项
            if (set[cboExams.Text].Items.Keys.ToList().Contains(this.cboPhy.Text))
            {
                MessageBox.Show("您已添加过此项!");
                return;
            }
            /*
             * 向套餐内添加项目
             * */
            set[cboExams.Text].Items.Add(cboPhy.Text, allItems[cboPhy.Text]);
            Btn();
            MessageBox.Show("添加成功!");

        }
        public void RenovateDgv()
        {
            /*
             * 刷新datagridview
             * */
            BindingSource source = new BindingSource();
            source.DataSource = set[cboExams.Text].Items.Values;
            dataGridView1.DataSource = source;
        }
        private void btnAdd_Click(object sender, EventArgs e)
        {
            /*
             * 添加套餐项目
             * */
            AddDgv();
            RenovateDgv();
        }

        public void Btn()
        {
            /*
             * 由集合内是否有元素,来改变删除按钮的状态
             * */
            if (set[cboExams.Text].Items.Count > 0)
            {
                this.btnDel.Enabled = true;
            }
            else
            {
                this.btnDel.Enabled = false;
            }

            /*
             * 计算套餐总金额
             * 并刷新控件
             * */
            set[cboExams.Text].Price = 0;
            foreach (KeyValuePair<string, HealthCheckItem> it in set[cboExams.Text].Items)
            {
                set[cboExams.Text].Price += it.Value.Price;
            }
            this.lblPrices.Text = set[cboExams.Text].Price.ToString();
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            /*
             * 由键来移除指定的集合元素
             * 并刷新控件显示的值与状态
             * */
            set[cboExams.Text].Items.Remove(this.dataGridView1.SelectedRows[0].Cells[0].Value.ToString().Trim());
            RenovateDgv();
            Btn();
            MessageBox.Show("删除成功!");
        }
    }
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档