本文章為 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("刪除成功!"); } }