1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace HealthSys.entity 8 { 9 /// <summary> 10 /// 检查项目类 11 /// </summary> 12 public class HealthCheckItem 13 { 14 /// <summary> 15 /// 描述 16 /// </summary> 17 public string Description { get; set; } 18 19 /// <summary> 20 /// 名称 21 /// </summary> 22 public string Name { get; set; } 23 24 /// <summary> 25 /// 价格 26 /// </summary> 27 public int Price { get; set; } 28 29 } 30 }
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 7 namespace HealthSys.entity 8 { 9 /// <summary> 10 /// 套餐类 11 /// </summary> 12 public class HealthCheckSet 13 { 14 /// <summary> 15 /// 套餐,代表项目的集合 16 /// </summary> 17 public List<HealthCheckItem> Items { get; set; } 18 19 /// <summary> 20 /// 套餐名称 21 /// </summary> 22 public string Name { get; set; } 23 24 /// <summary> 25 /// 套餐价格(总和) 26 /// </summary> 27 public int Price { get; set; } 28 29 /// <summary> 30 /// 无参构造函数 31 /// </summary> 32 public HealthCheckSet() 33 { 34 Items = new List<HealthCheckItem>(); 35 } 36 37 /// <summary> 38 /// 带参构造函数 39 /// </summary> 40 /// <param name="items"></param> 41 /// <param name="name"></param> 42 public HealthCheckSet(List<HealthCheckItem> items, string name) 43 { 44 this.Items = items; 45 this.Name = name; 46 } 47 48 /// <summary> 49 /// 计算总价 50 /// </summary> 51 public void CalcPrice() 52 { 53 int totalPrice = 0; 54 //遍历item项中的pirce价格 55 foreach (HealthCheckItem item in this.Items) 56 { 57 totalPrice += item.Price; 58 } 59 this.Price = totalPrice; 60 } 61 } 62 }
1 using HealthSys.entity; 2 using System; 3 using System.Collections.Generic; 4 using System.ComponentModel; 5 using System.Data; 6 using System.Drawing; 7 using System.Linq; 8 using System.Text; 9 using System.Threading.Tasks; 10 using System.Windows.Forms; 11 12 namespace HealthSys 13 { 14 public partial class FrmMain : Form 15 { 16 public FrmMain() 17 { 18 InitializeComponent(); 19 } 20 21 /// <summary> 22 /// 所有体检项目 23 /// </summary> 24 public List<HealthCheckItem> AllItems = new List<HealthCheckItem>(); 25 26 /// <summary> 27 /// 套餐中的体检项目 28 /// </summary> 29 public List<HealthCheckItem> Items = new List<HealthCheckItem>(); 30 31 /// <summary> 32 /// 套餐列表集合 33 /// </summary> 34 public Dictionary<string, HealthCheckSet> HealthSet = new Dictionary<string, HealthCheckSet>(); 35 36 /// <summary> 37 /// 设置为入学体检 38 /// </summary> 39 HealthCheckSet healthCheckSet; 40 41 /// <summary> 42 /// 定义一些检查项 43 /// </summary> 44 HealthCheckItem height, weight, Bchao, Gan, sight, litsen, heart,choose; 45 46 47 /// <summary> 48 /// 套餐初始值 49 /// </summary> 50 public void Inital() 51 { 52 choose = new HealthCheckItem(); 53 choose.Name = "请选择"; 54 height = new HealthCheckItem(); 55 height.Name = "身高"; 56 height.Price = 5; 57 height.Description = "检测身高"; 58 weight = new HealthCheckItem(); 59 weight.Name = "体重"; 60 weight.Price = 10; 61 weight.Description = "检测体重"; 62 Bchao = new HealthCheckItem(); 63 Bchao.Name = "B超"; 64 Bchao.Price = 30; 65 Bchao.Description = "检测B超"; 66 Gan = new HealthCheckItem(); 67 Gan.Name = "肝功能"; 68 Gan.Price = 50; 69 Gan.Description = "检测肝功能"; 70 sight = new HealthCheckItem(); 71 sight.Name = "视力"; 72 sight.Price = 5; 73 sight.Description = "检测视力"; 74 litsen = new HealthCheckItem(); 75 litsen.Name = "听觉"; 76 litsen.Price = 5; 77 litsen.Description = "检测听觉"; 78 heart = new HealthCheckItem(); 79 heart.Name = "心电图"; 80 heart.Price = 100; 81 heart.Description = "检测心电图"; 82 AllItems.Add(choose); 83 AllItems.Add(height); 84 AllItems.Add(weight); 85 AllItems.Add(Bchao); 86 AllItems.Add(Gan); 87 AllItems.Add(sight); 88 AllItems.Add(litsen); 89 AllItems.Add(heart); 90 } 91 92 /// <summary> 93 /// 设置默认套餐 94 /// </summary> 95 public void initalSet() 96 { 97 //默认套餐添加数据 98 Items = new List<HealthCheckItem>(); 99 Items.Add(height); 100 Items.Add(weight); 101 Items.Add(sight); 102 Items.Add(heart); 103 104 healthCheckSet = new HealthCheckSet(Items,"入学体检"); 105 //调用计算价格 106 healthCheckSet.CalcPrice(); 107 HealthSet.Add("入学体检", healthCheckSet); 108 109 } 110 111 /// <summary> 112 /// 绑定检查项目数据 113 /// </summary> 114 public void BindList() 115 { 116 //绑定检查项目 117 cmbCheckItem.DataSource = new List<HealthCheckItem>(AllItems); 118 cmbCheckItem.DisplayMember = "Name"; 119 cmbCheckItem.ValueMember = ""; 120 } 121 122 /// <summary> 123 /// 绑定套餐列表 124 /// </summary> 125 public void BindSet() 126 { 127 //清空选项,为了避免刷新产生重复值 128 cmbItem.Items.Clear(); 129 //添加选择 130 cmbItem.Items.Add("请选择"); 131 foreach (var key in HealthSet.Keys) 132 { 133 cmbItem.Items.Add(key); 134 } 135 //默认为0 136 cmbItem.SelectedIndex = 0; 137 } 138 139 /// <summary> 140 /// 窗体加载 141 /// </summary> 142 /// <param name="sender"></param> 143 /// <param name="e"></param> 144 private void FrmMain_Load(object sender, EventArgs e) 145 { 146 //设置套餐名、价格为空,添加删除键不能使用 147 lblName.Text = ""; 148 lblPrice.Text = ""; 149 btnAdd.Enabled = false; 150 btnDelete.Enabled = false; 151 152 Inital(); 153 initalSet(); 154 BindSet(); 155 156 BindList(); 157 158 159 } 160 161 /// <summary> 162 /// 绑定dgv的数据 163 /// </summary> 164 /// <param name="set"></param> 165 public void BindDgv(HealthCheckSet set) 166 { 167 dgvShow.AutoGenerateColumns = false; 168 dgvShow.DataSource = new BindingList<HealthCheckItem>(set.Items); 169 } 170 171 /// <summary> 172 /// 套餐列表选择 173 /// </summary> 174 /// <param name="sender"></param> 175 /// <param name="e"></param> 176 private void cmbItem_SelectedIndexChanged(object sender, EventArgs e) 177 { 178 string listName = cmbItem.Text.Trim(); 179 if (listName == "请选择") 180 { 181 //dgv的数据绑定为空 182 dgvShow.DataSource = new BindingList<HealthCheckItem>(); 183 lblName.Text = ""; 184 lblPrice.Text = ""; 185 return; 186 } 187 //改变套餐名 188 lblName.Text = HealthSet[listName].Name; 189 //改变价格 190 lblPrice.Text = HealthSet[listName].Price.ToString(); 191 //根据选择的套餐,绑定新的数据 192 BindDgv(HealthSet[listName]); 193 194 btnDelete.Enabled = true; 195 } 196 197 /// <summary> 198 /// 删除套餐中某个项目 199 /// </summary> 200 public void Delete() 201 { 202 string listName = cmbItem.Text; 203 if (dgvShow.SelectedRows.Count == 0) 204 { 205 MessageBox.Show("请选择删除项!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error); 206 return; 207 } 208 //获取选中项的下标 209 int index = dgvShow.SelectedRows[0].Index; 210 //根据下标删除选中项 211 HealthSet[listName].Items.RemoveAt(index); 212 //重新算价格 213 HealthSet[listName].CalcPrice(); 214 //刷新dgv 215 BindDgv(HealthSet[listName]); 216 lblName.Text = healthCheckSet.Name; 217 lblPrice.Text = healthCheckSet.Price.ToString(); 218 MessageBox.Show("删除成功。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); 219 } 220 221 /// <summary> 222 /// 点击删除按钮 223 /// </summary> 224 /// <param name="sender"></param> 225 /// <param name="e"></param> 226 private void btnDelete_Click(object sender, EventArgs e) 227 { 228 Delete(); 229 } 230 231 /// <summary> 232 /// 添加新的检查项目至套餐中 233 /// </summary> 234 public void AddItme() 235 { 236 //先判断是否有选择套餐与项目 237 if (cmbItem.Text == "请选择") 238 { 239 MessageBox.Show("请选择套餐。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); 240 return; 241 } 242 if (cmbCheckItem.Text == "请选择") 243 { 244 MessageBox.Show("请选择一个项目。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); 245 return; 246 } 247 248 int index = cmbCheckItem.SelectedIndex - 1; 249 if (!HealthSet[cmbItem.Text].Items.Contains(AllItems[index])) 250 { 251 //添加选中的检查项 252 HealthSet[cmbItem.Text].Items.Add(AllItems[index]); 253 //计算总价 254 HealthSet[cmbItem.Text].CalcPrice(); 255 //刷新dgv 256 BindDgv(HealthSet[cmbItem.Text]); 257 lblName.Text = this.HealthSet[cmbItem.Text].Name; 258 lblPrice.Text = this.HealthSet[cmbItem.Text].Price.ToString(); 259 MessageBox.Show("添加成功。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); 260 } 261 else 262 { 263 MessageBox.Show("该项目已存在,请勿重复选择。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error); 264 } 265 } 266 267 /// <summary> 268 /// 添加套餐 269 /// </summary> 270 public void AddSet() 271 { 272 if (string.IsNullOrEmpty(txtItemName.Text.Trim())) 273 { 274 MessageBox.Show("请输入套餐名称!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); 275 return; 276 } 277 HealthCheckSet set = new HealthCheckSet(); 278 HealthSet.Add(txtItemName.Text.Trim(), set); 279 280 //刷新套餐 281 BindSet(); 282 283 cmbItem.SelectedIndex = HealthSet.Count; 284 285 MessageBox.Show("添加成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); 286 } 287 288 /// <summary> 289 /// 点击添加(套餐名称) 290 /// </summary> 291 /// <param name="sender"></param> 292 /// <param name="e"></param> 293 private void btnAddItemName_Click(object sender, EventArgs e) 294 { 295 AddSet(); 296 } 297 298 /// <summary> 299 /// 判断体检项目文本是否“请选择”,改变添加按钮是否可使用 300 /// </summary> 301 /// <param name="sender"></param> 302 /// <param name="e"></param> 303 private void cmbCheckItem_SelectedIndexChanged(object sender, EventArgs e) 304 { 305 if (cmbCheckItem.Text != "请选择") 306 { 307 btnAdd.Enabled = true; 308 } 309 else 310 { 311 btnAdd.Enabled = false; 312 } 313 } 314 315 /// <summary> 316 /// 添加项目的按钮 317 /// </summary> 318 /// <param name="sender"></param> 319 /// <param name="e"></param> 320 private void btnAdd_Click(object sender, EventArgs e) 321 { 322 AddItme(); 323 } 324 325 326 } 327 }
(注: HealthCheckSet 类与 HealthCheckItem 类都是存放在 entity 文件夹当中,所以在 FrmMain 中要添加引用命名空间 using HealthSys.entity;)
效果以视频展示:
请移步去 --> http://www.bilibili.com/video/av9738053/