using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace HealthCheck { //檢查項目 class HealthCheckItem { private string name; public string Name { get { return name; } set { name = value; } } private string bewrite; public string Bewrite { get { return bewrite; } set { bewrite = value; } } private int price; public int Price { get { return price; } set { price = value; } } public HealthCheckItem() {} public HealthCheckItem(string name,int price,string bewrite) { this.name = name; this.price = price; this.bewrite = bewrite; } public static Dictionary<string, HealthCheckItem> ItemDic = new Dictionary<string, HealthCheckItem>() { {"身高",new HealthCheckItem("身高",5,"用於測量身高")}, {"體重",new HealthCheckItem("體重",5,"用於測量體重")}, {"視力",new HealthCheckItem("視力",5,"用於檢查視力")}, {"聽力",new HealthCheckItem("聽力",5,"用於檢查聽力")}, {"肝功能",new HealthCheckItem("肝功能",200,"用於檢測肝功能")}, {"B超",new HealthCheckItem("B超",60,"用於B超檢查")}, {"心電圖",new HealthCheckItem("心電圖",100,"用於檢查心電圖")}, {"肺活量",new HealthCheckItem("肺活量",8,"用於測量肺活量")} }; } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace HealthCheck { //套餐 class HealthCheckSet { private string name; public string Name { get { return name; } set { name = value; } } public List<HealthCheckItem> ItemList=new List<HealthCheckItem>(); public static Dictionary<string, List<HealthCheckItem>> SetDic = new Dictionary<string, List<HealthCheckItem>>(); public HealthCheckSet() { this.ItemList = new List<HealthCheckItem>(); ItemList.Add(HealthCheckItem.ItemDic["身高"]); ItemList.Add(HealthCheckItem.ItemDic["體重"]); ItemList.Add(HealthCheckItem.ItemDic["視力"]); ItemList.Add(HealthCheckItem.ItemDic["聽力"]); ItemList.Add(HealthCheckItem.ItemDic["肺活量"]); SetDic.Add("入學體檢",this.ItemList); } public HealthCheckSet(string name) { SetDic.Add(name,this.ItemList); } /// <summary> /// Dic_Values 添加 /// </summary> /// <param name="str"></param> public static void DicAdd(List<HealthCheckItem> list,string str) { list.Add(HealthCheckItem.ItemDic[str]); } } }
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 HealthCheck { public partial class FrmMain : Form { public FrmMain() { InitializeComponent(); } private void FrmMain_Load(object sender, EventArgs e) { cboItem.Items.Add("請選擇"); cboItem.SelectedIndex = 0; RenovateItem(); HealthCheckSet set = new HealthCheckSet(); RenovateList(); cboList.SelectedIndex = 0; cboItem.Enabled = false; btnAddItem.Enabled = false; btnDeleteItem.Enabled = false; } //更新檢查項目列表 private void RenovateItem() { foreach (KeyValuePair<string, HealthCheckItem> item in HealthCheckItem.ItemDic) { cboItem.Items.Add(item.Key); } } //更新套餐列表 private void RenovateList() { cboList.Items.Clear(); cboList.Items.Add("請選擇"); foreach (KeyValuePair<string, List<HealthCheckItem>> item in HealthCheckSet.SetDic) { cboList.Items.Add(item.Key); } if (cboList.Items.Count>2) { cboList.SelectedIndex = cboList.Items.Count - 1; } } //套餐列表 private void cboList_SelectedIndexChanged(object sender, EventArgs e) { if (cboList.SelectedIndex>0) { lblChooseName.Text = cboList.Text; cboItem.Enabled = true; RenovateDGV(); } else { cboItem.Enabled = false; } } /// <summary> /// 刷新DGV列表 /// </summary> private void RenovateDGV() { dgvHealthCheckInfo.DataSource = new BindingList<HealthCheckItem>(HealthCheckSet.SetDic[cboList.Text]); lblChoosePrice.Text = PriceSum(cboList.Text).ToString(); } /// <summary> /// 檢查總金額 /// </summary> /// <param name="key"></param> /// <returns></returns> private int PriceSum(string key) { int sum = 0; foreach (HealthCheckItem item in HealthCheckSet.SetDic[key]) { sum += item.Price; } return sum; } //項目列表 private void cboItem_SelectedIndexChanged(object sender, EventArgs e) { if (cboItem.SelectedIndex>0) { btnAddItem.Enabled = true; } else { btnAddItem.Enabled = false; btnDeleteItem.Enabled = false; } } //DGV單擊事件 private void dgvHealthCheckInfo_CellClick(object sender, DataGridViewCellEventArgs e) { if (!btnDeleteItem.Enabled) { btnDeleteItem.Enabled = true; } } //刪除檢查項目 private void btnDeleteItem_Click(object sender, EventArgs e) { DialogResult result = MessageBox.Show("您確定要刪除嗎?","警告!",MessageBoxButtons.OKCancel,MessageBoxIcon.Information); if (result == DialogResult.OK) { if (dgvHealthCheckInfo.SelectedRows[0] != null && dgvHealthCheckInfo.SelectedRows[0].Cells[0] != null && dgvHealthCheckInfo.SelectedRows[0].Cells[0].Value != null) { //刪除 套餐中所選的與內置檢查項目相匹配的項 HealthCheckSet.SetDic[cboList.Text].Remove(HealthCheckItem.ItemDic[dgvHealthCheckInfo.SelectedRows[0].Cells["clmItemName"].Value.ToString()]); RenovateDGV(); } } } //添加檢查項目 private void btnAddItem_Click(object sender, EventArgs e) { if (HealthCheckSet.SetDic[cboList.Text].Contains(HealthCheckItem.ItemDic[cboItem.Text])) { MessageBox.Show(cboList.Text+"套餐中已存在該檢查項目!","錯誤",MessageBoxButtons.OK,MessageBoxIcon.Error); } else { HealthCheckSet.DicAdd(HealthCheckSet.SetDic[cboList.Text],cboItem.Text); RenovateDGV(); } } //套餐添加 private void btnAdd_Click(object sender, EventArgs e) { if (txtName.Text!="") { foreach (string item in HealthCheckSet.SetDic.Keys) { if (txtName.Text.Equals(item)) { MessageBox.Show("已經存在"+txtName.Text+"套餐!"); return; } } HealthCheckSet dic = new HealthCheckSet(txtName.Text); RenovateList(); } } } }