体检套餐管理系统 -- List单列集合
程序员文章站
2024-01-09 12:53:59
本文章为List单列集合开发项目,如需要 Dictionary双列集合开发的此项目,请到楼主博客园寻找 博客网址:http://www.cnblogs.com/lsy131479/ 窗体 一.首先定义项目类 二.定义套餐类 三.主窗体代码 ......
本文章为List<T>单列集合开发项目,如需要 Dictionary<K,V>双列集合开发的此项目,请到楼主博客园寻找
博客网址:http://www.cnblogs.com/lsy131479/
窗体
一.首先定义项目类
/// <summary>
/// 项目类
/// </summary>
public class HealthCheckItem { //项目描述 private string description; //项目名称 private string name; //项目价格 private int price; //无参构造 public HealthCheckItem() { } //有参构造 public HealthCheckItem(string description, string name, int price) { this.description = description; this.name = name; this.price = price; } public string Description { get => description; set => description = value; } public string Name { get => name; set => name = value; } public int Price { get => price; set => price = value; } }
二.定义套餐类
/// <summary> /// 套餐类 /// </summary> public class HealthCheckSet { //套餐名 private string name; //套餐总价格 private int price; //存储套餐内的项目 private List<HealthCheckItem> items = new List<HealthCheckItem>(); //无参构造 public HealthCheckSet() { } //有参构造 public HealthCheckSet(string name) { this.name = name; } public string Name { get => name; set => name = value; } public int Price { get => price; set => price = value; } public List<HealthCheckItem> Items { get => items; set => items = value; } }
三.主窗体代码
/// <summary> /// 体检套餐管理系统 -- List<T>单列集合 /// </summary> public FrmMain() { InitializeComponent(); } //存储套餐类的集合 List<HealthCheckSet> Set = new List<HealthCheckSet>(); private void FrmMain_Load(object sender, EventArgs e) { //删除多余的列 this.dgvHealth.AutoGenerateColumns = false; //删除多余的行 this.dgvHealth.AllowUserToAddRows = false; /* * 初始化套餐类集合 * 调用刷新datagridview的方法 * */ HealthCheckSet set0 = new HealthCheckSet("请选择"); HealthCheckSet set1 = new HealthCheckSet("入学体检"); Set.Add(set0); Set.Add(set1); AddExamCbo(); } //存储所有套餐内项目的集合 public List<HealthCheckItem> allItems = new List<HealthCheckItem>(); public void AddPhyCbo() { //清空集合 allItems.Clear(); //集合初始化 HealthCheckItem item1 = new HealthCheckItem("用于检查身高。", "身高", 5); HealthCheckItem item2 = new HealthCheckItem("用于检查体重。", "体重", 5); HealthCheckItem item3 = new HealthCheckItem("用于检查肝功能。", "肝功能", 50); HealthCheckItem item4 = new HealthCheckItem("用于检查视力。", "视力", 5); HealthCheckItem item5 = new HealthCheckItem("用于检查听力。", "听力", 5); HealthCheckItem item6 = new HealthCheckItem("用于检查B超。", "B超", 80); HealthCheckItem item7 = new HealthCheckItem("用于检查心电图。", "心电图", 100); allItems.AddRange(new HealthCheckItem[] { item1, item2, item3, item4, item5, item6, item7 }); /* * 绑定刷新项目下拉框 * */ this.cboPhy.DisplayMember = "name"; this.cboPhy.DataSource = new BindingList<HealthCheckItem>(allItems); } public void AddExamCbo() { /* * 绑定刷新套餐下拉框 * */ this.cboExams.DataSource = new BindingList<HealthCheckSet>(Set); ; this.cboExams.DisplayMember = "name"; } private void cboExams_SelectedIndexChanged(object sender, EventArgs e) { //调用绑定刷新datagridview方法 AddDgv(); //调用刷新金额的方法 CalcPrice(); //调用删除按钮状态方法 Btn(); /* * 如果不是请选择也就是0下标还原初始状态(按钮为禁用,项目下拉框无项目) * 反之,按钮解除禁用状态,项目下拉框从新填充数据 * */ if (this.cboExams.SelectedIndex > 0) { AddPhyCbo(); this.btnAdd.Enabled = true; this.lblNames.Text = cboExams.Text; } else { this.btnAdd.Enabled = false; this.btnDel.Enabled = false; this.lblNames.Text = cboExams.Text; this.cboPhy.DataSource = null; } } public void AddDgv() { /* * 将选中套餐的项绑定刷新datagridview * */ foreach (HealthCheckSet item in Set) { if (item.Name.Equals(cboExams.Text)) { this.dgvHealth.DataSource = new BindingList<HealthCheckItem>(item.Items); return; } } } public void item() { foreach (HealthCheckSet item in Set) { if (item.Name.Equals(cboExams.Text)) { foreach (HealthCheckItem it in item.Items) { if (it.Name.Equals(this.cboPhy.Text)) { /* * 判定是否已存在要添加的项 * */ MessageBox.Show("您已添加过此项!"); return; } } foreach (HealthCheckItem it in allItems) { if (it.Name.Equals(this.cboPhy.Text)) { /* * 如果通过验证,可以添加,则将该项添加到集合中,并刷新datagridview与套餐价格 * */ item.Items.Add(it); AddDgv(); CalcPrice(); MessageBox.Show("添加成功!"); return; } } return; } } } public void CalcPrice() { foreach (HealthCheckSet item in Set) { if (item.Name.Equals(cboExams.Text)) { /* * 计算套餐金额 * */ item.Price = 0; foreach (HealthCheckItem it in item.Items) { item.Price += it.Price; } //将套餐金额赋值给控件,并显示 this.lblPrices.Text = item.Price.ToString(); return; } } } private void btnAdd_Click(object sender, EventArgs e) { //添加套餐项目 item(); Btn(); } private void btnNew_Click(object sender, EventArgs e) { //非空验证 if (txtNewName.Text == "" || txtNewName.Text == null) { MessageBox.Show("请输入套餐名称!"); return; } /* * 将通过验证的套餐名称添加到集合并刷新套餐下拉框 * */ HealthCheckSet set = new HealthCheckSet(this.txtNewName.Text); Set.Add(set); AddExamCbo(); this.cboExams.Text = this.txtNewName.Text; MessageBox.Show("添加成功!"); } private void btnDel_Click(object sender, EventArgs e) { foreach (HealthCheckSet item in Set) { if (item.Name.Equals(cboExams.Text)) { for (int i = 0; i < item.Items.Count; i++) { if (item.Items[i].Name.Equals(this.dgvHealth.SelectedRows[0].Cells[0].Value.ToString())) { /* * 删除要删除的项并刷新相关控件 * */ item.Items.RemoveAt(i); AddDgv(); CalcPrice(); this.lblPrices.Text = item.Price.ToString(); MessageBox.Show("删除成功!"); Btn(); return; } } return; } } } public void Btn() { foreach (HealthCheckSet item in Set) { if (item.Name.Equals(cboExams.Text)) { /* * 判断删除按钮的状态,如果集合里有数据为启用,无数据为禁用 * */ if (item.Items.Count > 0) { this.btnDel.Enabled = true; } else { this.btnDel.Enabled = false; } return; } } } }