体检套餐

窗体搭建搭建效果图:

窗体实现的功能:

1.显示指定套餐的项目明细

2.向指定套餐添加检查项目信息

3.删除套餐中的项目信息

4.新建套餐

首先创建系统中的检查项目类(HealthCheckItem)、体检套餐类(HealthCheckSet)

代码:

public class HealthCheckItem //检查项目类 {   public string Descripion { get; set; } //项目描述           public string Name { get; set; } //项目名称           public int Price { get; set; }  //项目价格

          //无参构造           public HealthCheckItem() { }

  //带参构造           public HealthCheckItem(string name, string descripion,  int price)   {     this.Descripion = descripion;     this.Name = name;     this.Price = price;   }     }

public class HealthCheckSet  //体检套餐类 {   //检查项目的集合           public List<HealthCheckItem> items { get; set; }   public int Price { get; set; } //套餐价格           public string Name { get; set; } //套餐名称

          //无参构造           public HealthCheckSet()   {     items = new List<HealthCheckItem>();   }   //带参构造           public HealthCheckSet(string name, List<HealthCheckItem> list)   {     this.Name = name;     this.items = list;   }

  //套餐价格计算           public void CalcPrice()   {     int totaPrice = 0;     foreach (HealthCheckItem item in items)     {       totaPrice += item.Price;     }     Price = totaPrice;   }

    }

在frmMain窗体里

//定义几个检查项目 HealthCheckItem height, weight, sight, shearing, liverFun, ekg, bWaves;         //定义一个系统默认的检查套餐“入学体检” HealthCheckSet setA;         //记录套餐中的体检项目 List<HealthCheckItem> list = new List<HealthCheckItem>();         //记录所有体检项目 Dictionary<String, HealthCheckItem> AllItems = new Dictionary<string, HealthCheckItem>();         //使用字典保存项目套餐 Dictionary<string, HealthCheckSet> dic = new Dictionary<string, HealthCheckSet>();

在Load窗体事件里初始化套餐和项目信息

private void FrmMain_Load(object sender, EventArgs e)         {   InitItems(); //初始化体检项目               InitItemsList(); //初始化体检项目列表               InitSets(); //初始化套餐               InitSetsList();  //添加套餐下拉列表 }

添加套餐代码:

private void btnNameAdd_Click(object sender, EventArgs e)         {   string name = txtName.Text;

  //判断添加不能为空               if (!txtName.Text.Equals(string.Empty))   {     //判断是否在体检套餐中存在                     if (dic.Keys.Contains(name))     {       MessageBox.Show("已经有该套餐了", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);       return;     }     else     {       //添加新的体检项目                           list = new List<HealthCheckItem>();       setA = new HealthCheckSet(name, list);       setA.CalcPrice(); //体检套餐价格                           dic.Add(name, setA);       InitSetsList(); //刷新体检套餐下拉列表                           cboSets.Text = name; //体检套餐下拉列表等于新添加的套餐名称                           txtName.Text = "";     }   }   else   {     MessageBox.Show("添加条件不能为空!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);   }         }

效果图:

//绑定datagridView显示信息方法

private void UpdateSet(HealthCheckSet set)         {   //判断套餐中检查项目集合为空的时候DataGridView就不显示信息               if (set.items == null)   {     dgvHealthList.DataSource = new BindingList<HealthCheckItem>();   }   else   {     dgvHealthList.DataSource = new BindingList<HealthCheckItem>(set.items);   }         }

套餐列表选中的事件代码:

private void cboSets_SelectedIndexChanged(object sender, EventArgs e)         {   //套餐列表等于请选择的时候删除按钮Enabled就为false   if (cboSets.Text == "请选择")   {                 this.btnDelete.Enabled = false;   }   else   {     this.btnDelete.Enabled = true;     lblSetName.Text = this.dic[cboSets.Text].Name;     lblSetPrice.Text = this.dic[cboSets.Text].Price.ToString();     if (dic[cboSets.Text] != null)     {       UpdateSet(dic[cboSets.Text]); //调用显示方法                     }   }         }

检查项目选中事件代码:

private void cboProject_SelectedIndexChanged(object sender, EventArgs e)         {   if (cboProject.Text == "请选择")   {     this.btnProjectAdd.Enabled = false;   }   else   {     btnProjectAdd.Enabled = true;   }   }

检查项目添加代码:

private void btnProjectAdd_Click(object sender, EventArgs e)         {   //判断套餐是否存在要添加的体检项目               if (!dic[cboSets.Text].items.Contains(AllItems[cboProject.Text]))   {     dic[cboSets.Text].items.Add(AllItems[cboProject.Text]);     MessageBox.Show("添加成功");     //绑定datagridView     dgvHealthList.DataSource = new BindingList<HealthCheckItem>(dic[cboSets.Text].items);     dic[cboSets.Text].CalcPrice(); //计算套餐价格                     lblSetPrice.Text = dic[cboSets.Text].Price.ToString();   }   else   {     MessageBox.Show("已经有该项目的存在了", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);   }         }

效果图:

检查项目删除代码:

private void btnDelete_Click(object sender, EventArgs e)         {   //判断是否选中一行               if (dgvHealthList.SelectedRows.Count == 1)   {     string key = dgvHealthList.SelectedRows[0].Cells[1].Value.ToString();     dic[cboSets.Text].items.Remove(AllItems[key]);                   MessageBox.Show("删除成功!");     UpdateSet(dic[cboSets.Text]);     dic[cboSets.Text].CalcPrice();     lblSetPrice.Text = dic[cboSets.Text].Price.ToString();   }   else   {     MessageBox.Show("请选择选中一行!");   }         }

效果图:

原文地址:https://www.cnblogs.com/superws/p/5373425.html