一.首先创建好项目需要的类: HealthCheckItem(检查项目) TaoCan(体检套餐)
二:在各个类中定义项目需要的属性和字段以及方法:
(1)体检套餐类:
Items:HealthCheckItem的集合.采用泛型集合Lis<T>作为存储HealthCheckItem的数据结构。
Price:套餐价格,即Items属性中检查项目的价格之和。
Name:套餐名称。
public class TaoCan { public TaoCan() { items = new List<HealthCheckItem>(); } public TaoCan(string name, List<HealthCheckItem> items) { this.Name = name; this.items = items; } public void CalcPrice() { int totalPrice = 0; foreach (HealthCheckItem item in items) { totalPrice += item.Price; } this.price = totalPrice; } 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; } } }
(2)检查项目类:
Description:项目描述。
Name:项目名称。
Price:项目价格。
public class HealthCheckItem
{
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.Name = name;
this.Description = description;
this.Price = price;
}
}
三.采用泛型集合和Dictionary<K,V>保存体检项目
HealthCheckItem height, weight, sight, hearing, liverFun, ekg, bWaves, bloodPressure, bloodTest; TaoCan setA; //存储所有检查项目 List<HealthCheckItem> AllItems = new List<HealthCheckItem>(); //存储与套餐相关的检查项目 List<HealthCheckItem> items = new List<HealthCheckItem>(); //用Dictionary<K,V>保存套餐中的项目 public Dictionary<string, TaoCan> HealthSet = new Dictionary<string, TaoCan>();
四.初始化以及填值
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); }
五.构建一个默认的套餐,并且扔到所有套餐集合中
public void Tc() { TaoCan tc = new TaoCan(); tc.Name = "入学体检"; tc.Items.Add(ShenGao); tc.Items.Add(TiZhong); //请选择 TaoCan tc2 = new TaoCan(); tc2.Name = "请选择"; HSet.Add(tc2.Name, tc2); HSet.Add(tc.Name, tc); }
六.更新套餐检查项目,加载下拉框里的内容
private void updateSet(TaoCan set) { dgvList.DataSource = new BindingList<HealthCheckItem>(set.Items); } private void cboSets_SelectedIndexChanged(object sender, EventArgs e) { if (flag == 1) { string SetName = cboSets.Text; if (SetName == "请选择") { HSet[SetName].Items.Clear(); updateSet(HSet[SetName]); // dgvList.DataSource = null; lblSetName.Text = ""; lblSetPrice.Text = ""; return; } //设置套餐名称 lblSetName.Text = HSet[SetName].Name; HSet[SetName].CalcPrice(); //设置套餐总价 lblSetPrice.Text = HSet[SetName].Price.ToString(); //更新套餐检查项目 updateSet(HSet[SetName]); //设置清楚按钮为可用状态 btnDelete.Enabled = true; } } private void cboItems_SelectedIndexChanged(object sender, EventArgs e) { if (cboItems.Text != "请选择") { btnAdd.Enabled = true; } else { btnAdd.Enabled = false; } }
七.完善Load事件里的内容 调用必要的方法
private void BindRightDGV()
{
cboItems.DataSource = new BindingList<HealthCheckItem>(AllItems);
}
//定义一个标记 判定当前Load事件是否执行完毕
int flag = 0;
private void FrmMain_Load(object sender, EventArgs e)
{
//右侧下拉框
testSet();
//左侧
Tc();
//所有下拉框中的项目Clear()
BindingSource source = new BindingSource();
source.DataSource = HSet.Values;
cboSets.DisplayMember = "name";
cboSets.ValueMember = "items";
cboSets.DataSource = source;
BindRightDGV();
Console.WriteLine("");
flag = 1;
}
八.添加套餐
private void btnTaoCanAdd_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(txtName.Text)) { MessageBox.Show("请选择套餐名称","提示",MessageBoxButtons.OK,MessageBoxIcon.Information); return; } else { TaoCan Hch = new TaoCan(); Hch.Name=txtName.Text; HSet.Add(Hch.Name,Hch); BindingSource source = new BindingSource(); source.DataSource = HSet.Values; cboSets.DisplayMember = "name"; cboSets.ValueMember = "items"; cboSets.DataSource = source; MessageBox.Show("成功!"); //将新加入的套餐默认选中 cboSets.SelectedIndex = HSet.Count-1; } }
九.删除项目
private void btnDelete_Click(object sender, EventArgs e) { //体检项目的名称 if (dgvList.SelectedRows.Count>0) { string name=dgvList.SelectedRows[0].Cells[0].Value.ToString(); foreach (HealthCheckItem item in HSet[lblSetName.Text].Items) { if (item.Name==name) { HSet[lblSetName.Text].Items.Remove(item); break; } } updateSet(HSet[lblSetName.Text]); //价格 HSet[lblSetName.Text].CalcPrice(); lblSetPrice.Text = HSet[lblSetName.Text].Price.ToString(); }