using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace _05体检套餐管理系统
{
public partial class FrmMain : Form
{
public FrmMain()
{
InitializeComponent();
}
//定义几个检查项目
HealthCheckItem height, weight, sight, hearing, liverFun, ekg, bWaves, bloodPressure, bloodTest;
//定义1个系统默认检查套餐"入学体检"
HealthCheckSet setA;
//保存所有的体检项目
List<HealthCheckItem> AllItems = new List<HealthCheckItem>();
//保存套餐中的体检项目,和套餐相关的项目
List<HealthCheckItem> items = new List<HealthCheckItem>();
//使用字典保存套餐集合
public Dictionary<string,HealthCheckSet> HealthSet=new Dictionary<string,HealthCheckSet>();
private void btnOK_Click(object sender, EventArgs e)
{
//添加
if (string.IsNullOrEmpty(txtHealthName.Text))
{
MessageBox.Show("请输入套餐名称", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;//结束方法
}
else
{
//声明一个套餐对象
HealthCheckSet Hch = new HealthCheckSet();
//将套餐对对象添加到Dictionary中
this.HealthSet.Add(this.txtHealthName.Text, Hch);
this.InitHealthSetList();
//下拉框显示刚添加的内容
this.cboSets.SelectedIndex = this.HealthSet.Count;
lblSetName.Text = cboSets.Text;
Hch.Name = cboSets.Text;
MessageBox.Show("添加成功!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
//创建Item对象并保存到集合中
public void InitItems()
{
height = new HealthCheckItem("身高",5,"用于检查身高");
weight = new HealthCheckItem("体重", 5, "用于检查体重.");
sight = new HealthCheckItem("视力", 10, "用于检查视力.");
hearing = new HealthCheckItem("听力", 10, "用于检查听力.");
liverFun = new HealthCheckItem("肝功能", 50, "用于检查肝功能.");
bWaves = new HealthCheckItem("B超", 30, "用于检查B超.");
ekg = new HealthCheckItem("心电图", 50, "用于检查心电图.");
bloodPressure = new HealthCheckItem("血压", 20, "用于检查血压.");
bloodTest = new HealthCheckItem("血常规", 20, "用于检查血常规.");
AllItems.Add(height);
AllItems.Add(weight);
AllItems.Add(sight);
AllItems.Add(hearing);
AllItems.Add(liverFun);
AllItems.Add(bWaves);
AllItems.Add(ekg);
AllItems.Add(bloodPressure);
AllItems.Add(bloodTest);
}
//生成默认套餐数据
private void InitSets()
{
//创建1种默认套餐对象
items = new List<HealthCheckItem>();
items.Add(height);
items.Add(weight);
items.Add(liverFun);
setA = new HealthCheckSet("入学体检",items);
//计算套餐价格
setA.CalcPrice();
this.HealthSet.Add("入学体检",setA);
}
//加载体检套餐下拉列表
public void InitHealthSetList()
{
//首先清空套餐下拉列表
this.cboSets.Items.Clear();
//添加请选择
this.cboSets.Items.Add("请选择");
//将Dictionary的key值绑定到combobox上,作为combobox的显示值
foreach (string key in this.HealthSet.Keys)
{
this.cboSets.Items.Add(key);
}
//默认第一项被选中
this.cboSets.SelectedIndex = 0;
}
private void FrmMain_Load(object sender, EventArgs e)
{
//窗体加载事件
//设置套餐名称的值为空
this.lblSetName.Text = "";
//设置套餐总价的值为空
this.lblSetPrice.Text = "";
//添加按钮不可用
btnAdd.Enabled = false;
//删除按钮不可用
btnDel.Enabled = false;
//初始化所有检查项目
InitItems();
//初始化默认套餐
InitSets();
//加载下拉列表框
InitHealthSetList();
dgvHealthList.AutoGenerateColumns = false;
}
//填充套餐的DataGridVIew
private void UpdateSet(HealthCheckSet set)
{
dgvHealthList.DataSource = new BindingList<HealthCheckItem>(set.Items);
}
private void cboSets_SelectedIndexChanged(object sender, EventArgs e)
{
string setName = this.cboSets.Text;
if (setName=="请选择")
{
this.dgvHealthList.DataSource = null;
lblSetName.Text = "";
lblSetPrice.Text = "";
return;
}
//设置套餐名称
lblSetName.Text = this.HealthSet[setName].Name;
//设置套餐总价
lblSetPrice.Text = this.HealthSet[setName].Price.ToString();
//更新套餐检查项目
UpdateSet(HealthSet[setName]);
//设置删除按钮为"可用状态"
btnDel.Enabled = true;
}
//删除检查项目
private void btnDel_Click(object sender, EventArgs e)
{
string setName = this.cboSets.Text;
if (this.dgvHealthList.SelectedRows.Count==0)
{
MessageBox.Show("没有选择删除项。","提示",MessageBoxButtons.OK,MessageBoxIcon.Error);
return;
}
//获取选中项目的索引
int index = this.dgvHealthList.SelectedRows[0].Index;
//删除检查项
this.HealthSet[setName].Items.RemoveAt(index);
//重新计算价格
this.HealthSet[setName].CalcPrice();
//更新DataGridView显示
UpdateSet(HealthSet[setName]);
//重设标签显示
lblSetName.Text = setA.Name;
string cboSetText = this.cboSets.Text;
lblSetPrice.Text = this.HealthSet[cboSetText].Price.ToString();
MessageBox.Show("删除成功","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
}
private void cboItems_SelectedIndexChanged(object sender, EventArgs e)
{
if (this.cboItems.Text!="请选择")
{
this.btnAdd.Enabled = true;
}
else
{
this.btnAdd.Enabled = false;
}
}
//"添加"检查项目
private void btnAdd_Click(object sender, EventArgs e)
{
if (this.cboItems.SelectedIndex==0)
{
MessageBox.Show("请选择一个项目");
return;
}
string cboSetText = this.cboSets.Text;
if (cboSetText=="请选择")
{
MessageBox.Show("请选择套餐!");
return;
}
int index = this.cboItems.SelectedIndex - 1;
if (!this.HealthSet[cboSetText].Items.Contains(AllItems[index]))
{
this.HealthSet[cboSetText].Items.Add(AllItems[index]);
this.HealthSet[cboSetText].CalcPrice();
UpdateSet(this.HealthSet[cboSetText]);
this.lblSetName.Text = this.HealthSet[cboSetText].Name; //刷新窗体集合A名称
this.lblSetPrice.Text = this.HealthSet[cboSetText].Price.ToString(); //刷新集合A价格
MessageBox.Show("添加成功。", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
else
{
MessageBox.Show("该项目存在", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void groupBox1_Enter(object sender, EventArgs e)
{
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace _05体检套餐管理系统
{
public class HealthCheckItem
{
//体检项目类
//保存一个体检项目包括项目名、描述、单价
//例如:肝功能、用于检查肝功能、60
//项目价格
private int price;
//项目描述
private string description;
//项目名称
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
public string Description
{
get { return description; }
set { description = value; }
}
public int Price
{
get { return price; }
set { price = value; }
}
public HealthCheckItem(string name,int price,string description)
{
//this
this.Name = name;
this.Description = description;
this.Price = price;
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace _05体检套餐管理系统
{
//体检套餐类
//每个套餐包括要检查的项目、套餐名称、总价
public class HealthCheckSet
{
public HealthCheckSet()
{
items=new List<HealthCheckItem>();
}
public HealthCheckSet(string name,List<HealthCheckItem> items )
{
this.Name = name;
this.items = items;
}
//套餐价格
private int price;
//检查项目
private List<HealthCheckItem> items;
private string name;
public string Name
{
get { return name; }
set { name = value; }
}
public List<HealthCheckItem> Items
{
get { return items; }
set { items = value; }
}
public int Price
{
get { return price; }
set { price = value; }
}
//套餐计算方法
public void CalcPrice()
{
int totalPrice = 0;
foreach (HealthCheckItem item in items)
{
totalPrice += item.Price;
}
this.price =totalPrice;
}
}
}