體檢套餐管理系統 -- Dictionary<K,V>雙列集合

来源:https://www.cnblogs.com/lsy131479/archive/2018/01/09/8251444.html
-Advertisement-
Play Games

本文章為 Dictionary<K,V>雙列集合開發項目,如需要List<T>單列集合開發的此項目,請到樓主博客園尋找 博客網址:http://www.cnblogs.com/lsy131479/ 窗體 一.首先創建項目類 二.創建套餐類 三.主窗體代碼 ...


本文章為 Dictionary<K,V>雙列集合開發項目,如需要List<T>單列集合開發的此項目,請到樓主博客園尋找

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

 

窗體

      

 

 

一.首先創建項目類

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

二.創建套餐類

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

 

三.主窗體代碼

   /// <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("刪除成功!");
        }
    }

 


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...