第五章.体检套餐管理系统.刁汉生.20170408
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了第五章.体检套餐管理系统.刁汉生.20170408相关的知识,希望对你有一定的参考价值。
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 Lesson5 12 { 13 public partial class frmMain : Form 14 { 15 public frmMain() 16 { 17 InitializeComponent(); 18 } 19 20 Dictionary<string, HealthCheckSet> dic = new Dictionary<string, HealthCheckSet>(); 21 List<HealthCheckItem> checkItem = new List<HealthCheckItem>(); 22 /// <summary> 23 /// 初始检查项目 24 /// </summary> 25 public void IniCheckItem() 26 { 27 HealthCheckItem h1 = new HealthCheckItem(); 28 h1.Name = "身高"; 29 h1.Description = "用于检查身高"; 30 h1.Price = 5; 31 HealthCheckItem h2 = new HealthCheckItem(); 32 h2.Name = "体重"; 33 h2.Description = "用于检查体重"; 34 h2.Price = 5; 35 HealthCheckItem h3 = new HealthCheckItem(); 36 h3.Name = "听力"; 37 h3.Description = "用于检查听力"; 38 h3.Price = 10; 39 HealthCheckItem h4 = new HealthCheckItem(); 40 h4.Name = "肝功能"; 41 h4.Description = "用于检查肝功能"; 42 h4.Price = 30; 43 HealthCheckItem h5 = new HealthCheckItem(); 44 h5.Name = "B超"; 45 h5.Description = "用于检查身体"; 46 h5.Price = 66; 47 HealthCheckItem h6 = new HealthCheckItem(); 48 h6.Name = "心电图"; 49 h6.Description = "用于检查心"; 50 h6.Price = 111; 51 checkItem.Add(h1); 52 checkItem.Add(h2); 53 checkItem.Add(h3); 54 checkItem.Add(h4); 55 checkItem.Add(h5); 56 checkItem.Add(h6); 57 58 } 59 /// <summary> 60 /// 绑定检查项目 61 /// </summary> 62 public void BindCheckItem() 63 { 64 cmbCheckItems.DataSource = new List<HealthCheckItem>(checkItem); 65 cmbCheckItems.DisplayMember = "Name"; 66 } 67 /// <summary> 68 /// 初始体检套餐 69 /// </summary> 70 public void IniCheckSet() 71 { 72 HealthCheckSet check1 = new HealthCheckSet(); 73 check1.Name = "请选择"; 74 HealthCheckSet check2 = new HealthCheckSet(); 75 check2.Name = "入学体验"; 76 HealthCheckItem h1 = new HealthCheckItem(); 77 h1.Name = "身高"; 78 h1.Description = "用于检查身高"; 79 h1.Price = 5; 80 HealthCheckItem h2 = new HealthCheckItem(); 81 h2.Name = "体重"; 82 h2.Description = "用于检查体重"; 83 h2.Price = 5; 84 HealthCheckItem h3 = new HealthCheckItem(); 85 h3.Name = "听力"; 86 h3.Description = "用于检查听力"; 87 h3.Price = 10; 88 check2.Lis.Add(h1); 89 check2.Lis.Add(h2); 90 check2.Lis.Add(h3); 91 dic.Add(check1.Name, null); 92 dic.Add(check2.Name, check2); 93 Bindtc(); 94 } 95 /// <summary> 96 /// 绑定套餐 97 /// </summary> 98 public void Bindtc() 99 { 100 BindingSource bs = new BindingSource(); 101 bs.DataSource = dic.Keys; 102 cmbTCList.DataSource = bs; 103 cmbTCList.DisplayMember = "Name"; 104 } 105 /// <summary> 106 /// 窗体加载事件 107 /// </summary> 108 /// <param name="sender"></param> 109 /// <param name="e"></param> 110 private void frmMain_Load(object sender, EventArgs e) 111 { 112 dgvList.AutoGenerateColumns = false; 113 IniCheckItem(); 114 IniCheckSet(); 115 } 116 /// <summary> 117 /// 求套餐总价格 118 /// </summary> 119 public void ChangePrice() 120 { 121 int price = 0; 122 foreach (var item in dic) 123 { 124 if (item.Key.Equals(cmbTCList.Text.Trim())) 125 { 126 item.Value.CalcPrice(); 127 price = item.Value.TotalPrice; 128 break; 129 } 130 } 131 lblSPrice.Text = price.ToString(); 132 } 133 /// <summary> 134 /// 文本改变事件 135 /// </summary> 136 /// <param name="sender"></param> 137 /// <param name="e"></param> 138 private void cmbTCList_TextChanged(object sender, EventArgs e) 139 { 140 if (cmbTCList.Text.Equals("请选择")) 141 { 142 cmbCheckItems.DataSource = null; 143 this.dgvList.DataSource = null; 144 lblTCName.Text = ""; 145 lblSPrice.Text = ""; 146 } 147 else 148 { 149 lblTCName.Text = cmbTCList.Text; 150 ChangePrice(); 151 BindCheckItem(); 152 BingDgvList(); 153 //foreach (var item in dic)s 154 //{ 155 // if (cmbTCList.Text.Equals(item.Key)) 156 // { 157 // BindingSource bs = new BindingSource(); 158 // bs.DataSource = item.Value; 159 // this.dgvList.DataSource = bs; 160 // return; 161 // } 162 //} 163 } 164 } 165 /// <summary> 166 /// 绑定dgv 167 /// </summary> 168 public void BingDgvList() 169 { 170 BindingSource bs = new BindingSource(); 171 bs.DataSource = dic[cmbTCList.Text].Lis; 172 this.dgvList.DataSource = bs; 173 } 174 /// <summary> 175 /// 文本改变事件 176 /// </summary> 177 /// <param name="sender"></param> 178 /// <param name="e"></param> 179 private void cmbCheckItems_TextChanged(object sender, EventArgs e) 180 { 181 if (string.IsNullOrEmpty(cmbCheckItems.Text.Trim())) 182 { 183 btnAddC.Enabled = false; 184 btnDelete.Enabled = false; 185 } 186 else 187 { 188 btnAddC.Enabled = true; 189 btnDelete.Enabled = true; 190 } 191 } 192 /// <summary> 193 /// 循环找到需要添加的套餐检查项 194 /// </summary> 195 public HealthCheckItem AddI() 196 { 197 foreach (var item in checkItem) 198 { 199 if (cmbCheckItems.Text.Trim().Equals(item.Name)) 200 { 201 return item; 202 } 203 } 204 return null; 205 } 206 /// <summary> 207 /// 为套餐添加项 208 /// </summary> 209 /// <param name="sender"></param> 210 /// <param name="e"></param> 211 private void btnAddC_Click(object sender, EventArgs e) 212 { 213 if (RpOrNo() == true) 214 { 215 var v = dic[cmbTCList.Text.Trim()]; 216 v.Lis.Add(AddI()); 217 BingDgvList(); 218 ChangePrice(); 219 } 220 else 221 { 222 MessageBox.Show("添加失败!不能添加重复项"); 223 } 224 } 225 /// <summary> 226 /// 判断是套餐是否添加了重复项 227 /// </summary> 228 /// <returns></returns> 229 public bool RpOrNo() 230 { 231 var v = dic[cmbTCList.Text.Trim()]; 232 foreach (var item in v.Lis) 233 { 234 if (item.Name.Equals(cmbCheckItems.Text.Trim())) 235 { 236 return false; 237 } 238 } 239 return true; 240 } 241 /// <summary> 242 /// 删除指定套餐检查项 243 /// </summary> 244 /// <param name="sender"></param> 245 /// <param name="e"></param> 246 private void btnDelete_Click(object sender, EventArgs e) 247 { 248 string name = dgvList.SelectedRows[0].Cells[0].Value.ToString(); 249 if (dgvList.SelectedRows.Count > 0) 250 { 251 for (int i = 0; i < dic[cmbTCList.Text.Trim()].Lis.Count; i++) 252 { 253 if (name.Equals(dic[cmbTCList.Text.Trim()].Lis[i].Name)) 254 { 255 DialogResult result = MessageBox.Show("是否删除" + name + "项","提示",MessageBoxButtons.YesNo,MessageBoxIcon.Question); 256 if (result == DialogResult.Yes) 257 { 258 //移除指定项 259 dic[cmbTCList.Text.Trim()].Lis.RemoveAt(i); 260 BingDgvList(); 261 ChangePrice(); 262 MessageBox.Show("删除" + name + "项成功"); 263 return; 264 } 265 } 266 } 267 } 268 else 269 { 270 MessageBox.Show("请选中要删除的套餐项"); 271 } 272 } 273 /// <summary> 274 /// 添加套餐 275 /// </summary> 276 /// <param name="sender"></param> 277 /// <param name="e"></param> 278 private void btnAddTC_Click(object sender, EventArgs e) 279 { 280 if (isTCX() == true) 281 { 282 dic.Add(txtTC.Text.Trim(),new HealthCheckSet()); 283 Bindtc(); 284 txtTC.Text = ""; 285 } 286 else 287 { 288 MessageBox.Show("不能添加重复套餐"); 289 } 290 } 291 /// <summary> 292 /// 判断是否有重复套餐 293 /// </summary> 294 /// <returns></returns> 295 public bool isTCX() 296 { 297 foreach (var item in dic.Keys) 298 { 299 if (txtTC.Text.Trim().Equals(item)) 300 { 301 return false; 302 } 303 } 304 return true; 305 } 306 } 307 }
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace Lesson5 8 { 9 /// <summary> 10 /// 检查项目类 11 /// </summary> 12 public class HealthCheckItem 13 { 14 //项目描述 15 public string Description { get; set; } 16 //项目名称 17 public string Name { get; set; } 18 //项目价格 19 public int Price { get; set; } 20 } 21 }
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace Lesson5 8 { 9 /// <summary> 10 /// 体检套餐类 11 /// </summary> 12 public class HealthCheckSet 13 { 14 //检查项目类 15 public List<HealthCheckItem> Lis{get;set;} 16 //套餐名称 17 public string Name { get; set; } 18 //套餐总价格 19 public int TotalPrice{get;set;} 20 /// <summary> 21 /// 套餐价格计算方法 22 /// </summary> 23 public void CalcPrice() 24 { 25 int totalPrice = 0; 26 foreach (var item in Lis) 27 { 28 totalPrice += item.Price; 29 } 30 this.TotalPrice = totalPrice; 31 } 32 /// <summary> 33 /// 每次 new 出新空间 34 /// </summary> 35 public HealthCheckSet() 36 { 37 Lis = new List<HealthCheckItem>(); 38 } 39 } 40 }
以上是关于第五章.体检套餐管理系统.刁汉生.20170408的主要内容,如果未能解决你的问题,请参考以下文章