完成效果图
代码展示
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.Runtime.Remoting.Messaging;
8 using System.Text;
9 using System.Threading.Tasks;
10 using System.Windows.Forms;
11
12 namespace 体检套餐管理系统_01
13 {
14 public partial class Form1 : Form
15 {
16 public Form1()
17 {
18 InitializeComponent();
19 }
20 //定义检查项目
21 private HealthCheckItem height, weight, sight, hearing, liverFun, ekg, bWaves, bloodPressure, bloodTest;
22
23 //默认检查套餐“入学体检”
24 private HealthCheckSet setA;
25
26 //保存所有的体检项目
27 List<HealthCheckItem> AllItems = new List<HealthCheckItem>();
28
29 //保存套餐中的体检项目,和套餐相关的项目
30 List<HealthCheckItem> item = new List<HealthCheckItem>();
31
32 //使用字典保存套餐集合
33 public Dictionary<string,HealthCheckSet> HealthSet=new Dictionary<string, HealthCheckSet>();
34
35
36
37 private void btnOK_Click(object sender, EventArgs e)
38 {
39 if (string.IsNullOrEmpty(txtHealthName.Text))
40 {
41 MessageBox.Show("请输入套餐名称", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
42 return;
43 }
44 else
45 {
46 //声明一数组
47 HealthCheckSet Hch = new HealthCheckSet();
48
49 this.HealthSet.Add(this.txtHealthName.Text,Hch);
50 this.InitHealthSetList();
51 //向下拉框显示添加的内容
52 this.cboSets.SelectedIndex = this.HealthSet.Count;
53 lblSetName.Text = cboItems.Text;
54 Hch.Name = cboSets.Text;
55
56 MessageBox.Show("添加成功!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
57 }
58 }
59
60 public void InitItems()
61 {
62 height = new HealthCheckItem("身高", 500, "用于检查身高");
63 weight = new HealthCheckItem("体重", 500, "用于检查体重.");
64 sight = new HealthCheckItem("视力", 1000, "用于检查视力.");
65 hearing = new HealthCheckItem("听力", 100, "用于检查听力.");
66 liverFun = new HealthCheckItem("肝功能", 50000, "用于检查肝功能.");
67 bWaves = new HealthCheckItem("B超", 30000, "用于检查B超.");
68 ekg = new HealthCheckItem("心电图", 500000, "用于检查心电图.");
69 bloodPressure = new HealthCheckItem("血压", 2000000, "用于检查血压.");
70 bloodTest = new HealthCheckItem("血常规", 9999999, "用于检查血常规.");
71
72 AllItems.Add(height);
73 AllItems.Add(weight);
74 AllItems.Add(sight);
75 AllItems.Add(hearing);
76 AllItems.Add(liverFun);
77 AllItems.Add(bWaves);
78 AllItems.Add(ekg);
79 AllItems.Add(bloodPressure);
80 AllItems.Add(bloodTest);
81 }
82 //默认套餐数据
83 private void InitSets()
84 {
85 item = new List<HealthCheckItem>();
86 item.Add(height);
87 item.Add(weight);
88 item.Add(liverFun);
89
90 setA = new HealthCheckSet("入学体检",item);
91
92 setA.CalcPrice();
93 this.HealthSet.Add("入学体检",setA);
94 }
95 //加载体检套餐下拉列表
96 private void InitHealthSetList()
97 {
98 //清空下拉列表
99 this.cboSets.Items.Clear();
100 this.cboSets.Items.Add("请选择");
101
102 foreach (string key in this.HealthSet.Keys)
103 {
104 this.cboSets.Items.Add(key);
105 }
106 this.cboSets.SelectedIndex = 0;
107
108 }
109
110 private void UpdateSet(HealthCheckSet set)
111 {
112 dgvHealthList.DataSource = new BindingList<HealthCheckItem>(set.Items);
113 }
114 //实现添加项目的操作,首先判断集合中是否已经存在要添加的项目了,如果要添加的项目不在集合中,则执行添加操作。其次,从allitems所有体检项目的集合中提取数据,使用foreach遍历,如果被选择的项目等于allitems中的项目,将此项目添加到HealItem项的集合中去,并且改变价格,刷新datagridview控件
115 private void btnAdd_Click(object sender, EventArgs e)
116 {
117 if (this.cboItems.SelectedIndex == 0)
118 {
119 MessageBox.Show("请选择一个项目");
120 return;
121 }
122 string cboSetText = this.cboSets.Text;
123 if (cboSetText == "请选择")
124 {
125 MessageBox.Show("请选择套餐!");
126 return;
127 }
128 int index = this.cboItems.SelectedIndex - 1;
129 if (!this.HealthSet[cboSetText].Items.Contains(AllItems[index]))
130 {
131 this.HealthSet[cboSetText].Items.Add(AllItems[index]);
132 this.HealthSet[cboSetText].CalcPrice();
133 UpdateSet(this.HealthSet[cboSetText]);
134 this.lblSetName.Text = this.HealthSet[cboSetText].Name; //刷新窗体集合A名称
135 this.lblSetPrice.Text = this.HealthSet[cboSetText].Price.ToString(); //刷新集合A价格
136 MessageBox.Show("添加成功。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
137 }
138 else
139 {
140 MessageBox.Show("该项目存在", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
141 }
142
143 }
144 \ 实现删除套餐中的项目的功能,setName是套餐下拉框中所有项的文本的集合。item[setName].Item是指HealItems中所有项的集合,datagridview控件中加载的也是HealItems项的集合,所以可以根据datagridview中被选中行的下表来删除集合中的数据。并且重新加载标签中的价格
145 private void btnDel_Click(object sender, EventArgs e)
146 {
147 string setgName = this.cboSets.Text;
148 if (this.dgvHealthList.SelectedRows.Count == 0)
149 {
150 MessageBox.Show("没有选择删除项","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
151 return;
152 }
153 //获取选中的项目的索引
154 int index = this.dgvHealthList.SelectedRows[0].Index;
155 //删除检查项
156 this.HealthSet[setgName].Items.RemoveAt(index);
157 //重新计算价格
158 this.HealthSet[setgName].CalcPrice();
159 //更新DateGridView显示
160 UpdateSet(HealthSet[setgName]);
161 //重设标签显示
162 lblSetName.Text = setA.Name;
163 string cboSetText = this.cboSets.Text;
164 lblSetPrice.Text = this.HealthSet[cboSetText].Price.ToString();
165 MessageBox.Show("删除成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
166
167
168 }
169
170 private void cboItems_SelectedIndexChanged(object sender, EventArgs e)
171 {
172 if (this.cboSets.Text != "请选择")
173 {
174 this.btnAdd.Enabled = true;
175 }
176 else
177 {
178 this.btnAdd.Enabled = false;
179 }
180 }
181 //
182 private void cboSets_SelectedIndexChanged(object sender, EventArgs e)
183 {
184 string setName = this.cboSets.Text;
185 if (setName == "请选择")
186 {
187 this.dgvHealthList.DataSource = null;
188 lblSetName.Text = "";
189 lblSetPrice.Text = "";
190 return;
191 }
192 //设置套餐名称
193 lblSetName.Text = this.HealthSet[setName].Name;
194 //设置套餐总价
195 lblSetPrice.Text = this.HealthSet[setName].Price.ToString();
196 //更新套餐检查项目
197 UpdateSet(HealthSet[setName]);
198 //删除按钮为可用状态
199 btnDel.Enabled = true;
200 }
201
202 private void Form1_Load(object sender, EventArgs e)
203 {
204 this.lblSetName.Text = "";
205 //设置套餐总价的值为空
206 this.lblSetPrice.Text = "";
207 btnAdd.Enabled = false;
208 btnDel.Enabled = false;
209 //默认初始化所有检查项目
210 InitItems();
211 //默认初始化套餐
212 InitSets();
213 //加载下拉列表
214 InitHealthSetList();
215 dgvHealthList.AutoGenerateColumns = false;
216 }
217 }
218 }
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace 体检套餐管理系统_01 7 { 8 public class HealthCheckSet 9 { 10 public HealthCheckSet() 11 { 12 items = new List<HealthCheckItem>(); 13 } 14 public HealthCheckSet(string name, List<HealthCheckItem> items) 15 { 16 this.Name = name; 17 this.items = items; 18 } 19 20 //套餐价格 21 private int price; 22 //检查项目 23 private List<HealthCheckItem> items; 24 private string name; 25 26 public string Name 27 { 28 get { return name; } 29 set { name = value; } 30 } 31 32 public List<HealthCheckItem> Items 33 { 34 get { return items; } 35 set { items = value; } 36 } 37 38 public int Price 39 { 40 get { return price; } 41 set { price = value; } 42 } 43 //套餐计算方法 44 public void CalcPrice() 45 { 46 int totalPrice = 0; 47 foreach (HealthCheckItem item in items) 48 { 49 totalPrice += item.Price; 50 } 51 this.price = totalPrice; 52 } 53 } 54 }
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace 体检套餐管理系统_01 { public class HealthCheckItem { private int price; private string descripion; private string description; private string name; public string Name { get { return name; } set { name = value; } } public int Price { get { return price; } set { price = value; } } public string Description { get { return description; } set { description = value; } } public HealthCheckItem(string name, int price, string description) { //this this.Name = name; this.Description = description; this.Price = price; } } }