体检套餐管理项目
1.體檢套餐管理項目頁面
2:用戶自定義類:HealthCheckItem和HealthCheckSet
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace FrmPhysical 8 { 9 public class HealthCheckItem //檢查項目類 10 { 11 public string Description { get; set; }//項目描述 12 public string Name { get; set; }//項目名稱 13 public int Price { get; set; }//項目價格 14 15 //無參構造函數 16 public HealthCheckItem() 17 { 18 } 19 20 //帶參構造函數 21 public HealthCheckItem(string name, string description, int price) 22 { 23 this.Name = name; 24 this.Description = description; 25 this.Price = price; 26 27 } 28 29 } 30 } namespace 體檢套餐管理 {public class HealthCheckItem{public HealthCheckItem(string name, int price, string description)//定義帶參構造為三個屬性賦初值 {this.Name = name;this.Description = description;this.Price = price;}private string name;public string Name//項目名 {get { return name; }set { name = value; }}private string description;public string Description//項目描述 {get { return description; }set { description = value; }}private int price;public int Price//項目價格 {get { return price; }set { price = value; }}} }?
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace FrmPhysical 8 { 9 public class HealthCheckSet //體檢套餐類 10 { 11 //HealthCheckItem的集合 12 public List<HealthCheckItem> Item { get; set; } 13 14 //套餐價格 15 public int Price { get; set; } 16 17 //套餐名稱 18 public string Name { get; set; } 19 20 //無參構造 21 public HealthCheckSet() { } 22 23 //帶參構造函數 24 public HealthCheckSet(string name,List<HealthCheckItem> item) 25 { 26 this.Name = name; 27 this.Item = item; 28 } 29 30 //計算總價格 31 public void CalcPrice() 32 { 33 int totaPrice = 0; 34 foreach (HealthCheckItem item in this.Item ) 35 { 36 totaPrice += item.Price; 37 } 38 this.Price = totaPrice; 39 } 40 41 42 } 43 } 1 namespace 體檢套餐管理 2 { 3 public 4 class HealthCheckSet 5 { 6 public HealthCheckSet()//初始化HealthCheckItems項的集合 7 { 8 items = new List<HealthCheckItem>(); 9 } 10 public HealthCheckSet(string name, List<HealthCheckItem> items)//有項目時給集合賦值的構造函數 11 { 12 this.Name = name; 13 this.items = items; 14 } 15 private string name; 16 public string Name 17 { 18 get { return name; } 19 set { name = value; } 20 } 21 private List<HealthCheckItem> items; 22 public List<HealthCheckItem> Items//表示HealthCheckItems中的項的集合 23 { 24 get { return items; } 25 set { items = value; } 26 } 27 private int price; 28 public int Price//套餐價格 29 { 30 get { return price; } 31 } 32 public void CalcPrice()//計算套餐價格的方法 33 { 34 int totalPrice = 0; 35 foreach (HealthCheckItem item in items) 36 { 37 totalPrice += item.Price; 38 } 39 this.price = totalPrice; 40 } 41 } 42 }?
?
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Data; 5 using System.Drawing; 6 using System.Linq; 7 using System.Text; 8 using System.Threading.Tasks; 9 using System.Windows.Forms; 10 11 namespace FrmPhysical 12 { 13 public partial class FrmPhysical : Form 14 { 15 public FrmPhysical() 16 { 17 InitializeComponent(); 18 } 19 20 private void FrmPhysical_Load(object sender, EventArgs e) 21 { 22 #region 主窗體調用方法 23 main();//初始化檢查項目的方法 24 yuan();//生成默認套餐數據 25 combox();//套餐列表 下拉框的加載方法 26 combox2(); //檢查項目 下拉框的加載方法 27 28 29 #endregion 30 } 31 #region 初始化檢查項目的方法 32 //初始化檢查項目的方法 33 34 //建立所有 檢查項目 集合 35 Dictionary<string, HealthCheckItem> AllList = new Dictionary<string, HealthCheckItem>(); 36 37 //初始化檢查項目 38 HealthCheckItem item, item2, item3, item4, item5, item6, item7; 39 public void main() 40 {//調用檢查項目類的帶參構造函數 41 item = new HealthCheckItem("身高","用于檢查身高",5); 42 item2 = new HealthCheckItem("體重", "用于檢查體重",5); 43 item3 = new HealthCheckItem("視力", "用于檢查視力",15); 44 item4 = new HealthCheckItem("聽力", "用于檢查聽力",20); 45 item5 = new HealthCheckItem("肝功能", "用于檢查肝功能",50); 46 item6 = new HealthCheckItem("B超", "用于檢查B超",30); 47 item7 = new HealthCheckItem("心電圖", "用于檢查心電圖",100); 48 49 //所有檢查項目 集合 50 AllList.Add(item.Name,item); 51 AllList.Add(item2.Name, item2); 52 AllList.Add(item3.Name, item3); 53 AllList.Add(item4.Name, item4); 54 AllList.Add(item5.Name, item5); 55 AllList.Add(item6.Name, item6); 56 AllList.Add(item7.Name, item7); 57 } 58 #endregion 59 60 #region 生成默認套餐數據 61 //生成默認套餐數據 62 63 //建立 套餐中的 檢查項目 集合 64 List<HealthCheckItem> List = new List<HealthCheckItem>(); 65 66 //使用字典保存套餐集合 67 Dictionary<string, HealthCheckSet> dictionary = new Dictionary<string, HealthCheckSet>(); 68 69 //定義一個默認套餐 70 HealthCheckSet moren; 71 public void yuan() 72 { 73 //套餐中的 檢查項目 集合 74 List.Add(item); 75 List.Add(item2); 76 List.Add(item3); 77 //定義一個默認套餐 78 moren = new HealthCheckSet("入學體檢",List); 79 //調用方法 定義一個默認套餐.計算價格 80 moren.CalcPrice(); 81 //使用字典保存套餐集合 82 this.dictionary.Add("入學體檢", moren); 83 84 85 } 86 87 #endregion 88 89 #region 套餐列表 下拉框的加載方法 90 public void combox() 91 { 92 cboList.Items.Clear(); 93 cboList.Items.Add("--請選擇--"); 94 foreach (string item in dictionary .Keys ) 95 { 96 cboList.Items.Add(item); 97 } 98 cboList.SelectedIndex = 0;//默認第一項為選擇 99 } 100 #endregion 101 102 #region 檢查列表 下拉框的加載方法 103 public void combox2() 104 { 105 cboProject.Items.Clear(); 106 cboProject.Items.Add("--請選擇--"); 107 foreach (string item in AllList.Keys ) 108 { 109 cboProject.Items.Add(item); 110 } 111 cboProject.SelectedIndex = 0;//默認第一項為選中 112 } 113 #endregion 114 115 #region 填充套餐的DataGridView 116 public void Updateset(HealthCheckSet set) 117 { 118 if (set.Item == null) 119 { 120 //給DataGridView的數據源賦空值 121 dataGridView1.DataSource = new BindingList<HealthCheckItem>(); 122 return; 123 } 124 else 125 { 126 dataGridView1.DataSource = new BindingList<HealthCheckItem>(set.Item); 127 } 128 } 129 #endregion 130 public const string CAPTION = "操作提示"; 131 private void btnAdd_Click(object sender, EventArgs e) 132 { 133 #region 添加套餐 134 if (txtName.Text != "") 135 { 136 if (dictionary.Keys.Contains(txtName.Text)) 137 { 138 MessageBox.Show("已經有該套餐了", CAPTION, MessageBoxButtons.OKCancel, MessageBoxIcon.Question); 139 return; 140 } 141 else 142 { 143 //給health實例化 144 List<HealthCheckItem> A = new List<HealthCheckItem>(); 145 HealthCheckSet health = new HealthCheckSet(); 146 health.Item = A; 147 health.Name = ""; 148 health.Price = 0; 149 this.dictionary.Add(txtName.Text, health); 150 combox(); 151 cboList.Text = txtName.Text; 152 txtName.Text = ""; 153 154 } 155 } 156 else 157 { 158 MessageBox.Show("添加的不能為空!",CAPTION,MessageBoxButtons.OKCancel,MessageBoxIcon.Question); 159 160 } 161 162 #endregion 163 } 164 165 private void btnDelete_Click(object sender, EventArgs e) 166 { 167 #region 刪除 168 string text = this.cboList.Text; 169 string key = dataGridView1.SelectedRows[0].Cells["名稱"].Value.ToString(); 170 this.dictionary[cboList.Text].Item.Remove(AllList[key]); 171 dictionary[text].CalcPrice(); 172 label2.Text = dictionary[text].Price.ToString(); 173 dataGridView1.DataSource = new BindingList<HealthCheckItem>(dictionary[cboList.Text].Item); 174 btnDelete.Enabled = false;//刪除按鈕的禁用 175 #endregion 176 } 177 private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e) 178 { 179 180 } 181 182 private void btnAdder_Click(object sender, EventArgs e) 183 { 184 #region 下面 添加項目 185 string name = cboProject.Text; 186 if (!dictionary[cboList.Text].Item.Contains(AllList[name])) 187 { 188 dictionary[cboList.Text].Item.Add(AllList[name]); 189 MessageBox.Show("添加成功!", CAPTION, MessageBoxButtons.OKCancel, MessageBoxIcon.Question); 190 dataGridView1.DataSource = new BindingList<HealthCheckItem>(dictionary[cboList.Text].Item); 191 dictionary[cboList.Text].CalcPrice(); 192 //根據套餐名給其中的檢查項求總價格 193 label2.Text = dictionary[cboList.Text].Price.ToString(); 194 } 195 else 196 { 197 MessageBox.Show("已經有該項目的存在了",CAPTION,MessageBoxButtons.OKCancel,MessageBoxIcon.Question); 198 } 199 #endregion 200 } 201 private void cboProject_SelectedIndexChanged(object sender, EventArgs e) 202 { 203 #region 添加按鈕的 是否可用 204 if (cboProject.Text == "--請選擇--" || cboList.Text == "--請選擇--") 205 { 206 btnAdder.Enabled = false; 207 } 208 else 209 { 210 btnAdder.Enabled = true; 211 } 212 #endregion 213 } 214 public string name; 215 private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) 216 { 217 #region 選中 218 if (dataGridView1.SelectedRows.Count != 1 || dataGridView1.SelectedRows[0].Cells[1].Value == null) 219 { 220 MessageBox.Show("請你正確的選擇一行!", CAPTION, MessageBoxButtons.OKCancel, MessageBoxIcon.Question); 221 return; 222 } 223 else 224 { 225 name = dataGridView1.SelectedRows[0].Cells[1].Value.ToString(); 226 btnDelete.Enabled = true;//刪除按鈕的可用 227 228 } 229 #endregion 230 } 231 232 private void cboList_SelectedIndexChanged(object sender, EventArgs e) 233 { 234 #region 選擇套餐 235 string setName = cboList.Text; 236 if (cboList.Text == "--請選擇--") 237 { 238 // 239 dataGridView1.DataSource = new BindingList<HealthCheckItem>(); 240 label1.Text = ""; 241 cboProject.Text = ""; 242 label2.Text = ""; 243 btnAdder.Enabled = false; 244 return; 245 } 246 else 247 { 248 label1.Text = setName; 249 if (dictionary[setName] != null) 250 { 251 //根據套餐名給DataGridView綁定數據 252 Updateset(dictionary[setName]); 253 } 254 else 255 { 256 //給DataGridView的數據源賦空值 257 dataGridView1.DataSource = new BindingList<HealthCheckItem>(); 258 259 } 260 //根據套餐名給其中的檢查項求總價格 261 label2.Text = dictionary[setName].Price.ToString(); 262 } 263 #endregion 264 } 265 266 267 } 268 }?
3:定義兩個List<T>單列集合,一個Dictionary<K,V>雙列集合并且默認初始化一個體檢套餐。
1 HealthCheckItem height, weight, eyes, ears, gan, Bchao, heart; 2 List<HealthCheckItem> allitems = new List<HealthCheckItem>();//保存所有的體檢項目 3 List<HealthCheckItem> items = new List<HealthCheckItem>();//保存套餐中的體檢項目 4 public Dictionary<string, HealthCheckSet> item = new Dictionary<string, HealthCheckSet>();//保存所有套餐 5 public HealthCheckSet numA;4:初始化保存在allitems中的所有的體檢項目以及默認套餐中的所有的體檢項目。
1 public void Show()//保存所有體檢項目2 {3 height = new HealthCheckItem("身高", 5, "測量身高");4 weight = new HealthCheckItem("體重", 5, "檢測體重");5 eyes = new HealthCheckItem("視力", 10, "測視力");6 ears = new HealthCheckItem("聽力", 10, "測聽力");7 gan = new HealthCheckItem("肝功能", 20, "測試肝功能");8 Bchao = new HealthCheckItem("B超", 20, "檢測身體內部");9 heart = new HealthCheckItem("心電圖", 50, "檢查心臟"); 10 allitems.Add(height); 11 allitems.Add(weight); 12 allitems.Add(eyes); 13 allitems.Add(ears); 14 allitems.Add(gan); 15 allitems.Add(Bchao); 16 allitems.Add(heart); 17 } 18 public void Num1()//默認的體檢套餐 19 { 20 items = new List<HealthCheckItem>(); 21 items.Add(height); 22 items.Add(weight); 23 items.Add(eyes); 24 numA = new HealthCheckSet("入學體檢", items); 25 numA.CalcPrice(); 26 this.item.Add("入學體檢", numA); 27 28 }5:向下拉框中添加數據,由于此控件是套餐下拉框,其儲存的數據是雙列集合dictionary<K,V>中的key值,即代碼如下
1 public void Chushi()2 {3 this.comboBox1.Items.Clear();4 this.comboBox1.Items.Add("請選擇");5 6 foreach (string key in this.item.Keys)7 {8 this.comboBox1.Items.Add(key);9 } 10 this.comboBox1.SelectedIndex = 0; 11 }6:在下拉框的SelectedIndexChanged事件中編寫如下代碼,目的是點擊下拉框選項后將數據加載到datagridview控件中,并且實現顯示套餐名稱和價格的功能。
1 string text = this.comboBox1.Text; 2 if (text == "請選擇") 3 { 4 this.label3.Text = ""; 5 this.label5.Text = ""; 6 this.dataGridView1.DataSource = new BindingList<HealthCheckItem>(); 7 return; 8 } 9 label3.Text = this.item[text].Name; 10 label5.Text = this.item[text].Price.ToString(); 11 load(item[text]); 12 this.button2.Enabled = true;備注:load()作用是將所有的體檢項目的信息加載到datagridview中
1 public void load(HealthCheckSet set)//將所有的體檢項目的信息加載到datagridview中 2 { 3 this.dataGridView1.DataSource = new BindingList<HealthCheckItem>(set.Items); 4 }7:實現刪除套餐中的項目的功能,setName是套餐下拉框中所有項的文本的集合。item[setName].Item是指HealthCheckItems中所有項的集合,datagridview控件中加載的也是HealthCheckItems項的集合,所以可以根據datagridview中被選中行的下表來刪除集合中的數據。并且重新加載標簽中的價格
1 string setName = this.comboBox1.Text; 2 3 if (this.dataGridView1.SelectedRows.Count == 0) 4 { 5 MessageBox.Show("沒有選擇刪除項。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error); 6 return; 7 } 8 9 int index = this.dataGridView1.SelectedRows[0].Index; 10 11 this.item[setName].Items.RemoveAt(index); 12 13 this.item[setName].CalcPrice(); 14 15 load(item[setName]); 16 17 this.label3.Text =numA.Name; 18 this.label5.Text = numA.Price.ToString(); 19 MessageBox.Show("刪除成功。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);8:實現添加項目的操作,首先判斷集合中是否已經存在要添加的項目了,如果要添加的項目不在集合中,則執行添加操作。其次,從allitems所有體檢項目的集合中提取數據,使用foreach遍歷,如果被選擇的項目等于allitems中的項目,將此項目添加到HealthCheckItem項的集合中去,并且改變價格,刷新datagridview控件
1 if (this.comboBox2.SelectedIndex == 0) 2 { 3 MessageBox.Show("請選擇一個項目。"); 4 return; 5 } 6 7 string text = this.comboBox1.Text; 8 if (text == "請選擇") 9 { 10 MessageBox.Show("請選擇套餐!"); 11 return; 12 } 13 14 15 foreach (HealthCheckItem cboitem in item[text].Items) 16 { 17 if (cboitem.Name == comboBox2.SelectedItem.ToString()) 18 { 19 MessageBox.Show("該項目存在", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error); 20 return; 21 } 22 } 23 foreach (HealthCheckItem addItem in allitems) 24 { 25 if (addItem.Name == comboBox2.SelectedItem.ToString()) 26 { 27 item[text].Items.Add(addItem); 28 } 29 this.item[text].CalcPrice(); 30 load(this.item[text]); 31 this.label3.Text = this.item[text].Name; 32 this.label5.Text = this.item[text].Price.ToString(); 33 34 35 }9:實現添加套餐操作,通過添加dictionary<K,V>中的Key值來添加套餐,并且刷新下拉列表。并在套餐下拉框中自動定位到剛被添加的套餐。
1 HealthCheckSet he = new HealthCheckSet(); 2 this.item.Add(textBox1.Text, he); 3 this.Chushi(); 4 this.comboBox1.SelectedIndex = item.Count; 5 MessageBox.Show("添加成功!");?
轉載于:https://www.cnblogs.com/WuXuanKun/p/5373374.html
總結
- 上一篇: OC中runtime的使用
- 下一篇: The operation couldn