效果

首先,我们先来准备我们需要的类

1.检查项目类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace 第五章_体检套餐管理系统_
{
//项目类
public class HealthCheckItem
{
//项目描述
public string Description { get; set; }
//项目名称
public string Name { get; set; }
//项目价格
public int Price { get; set; } //无参构造
public HealthCheckItem() { } //带参构造
public HealthCheckItem(string name,string description,int price)
{
this.Name = name;
this.Price = price;
this.Description = description;
}
}
}

2.套餐类

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace 第五章_体检套餐管理系统_
{ //套餐类
public class HealthCheckSet
{
//HealthCheckItem的集合
public List<HealthCheckItem> Item { get; set; } //套餐价格
public int Price { get; set; } //套餐名称
public string Name { get; set; } //无参构造
public HealthCheckSet() { } //带参构造
public HealthCheckSet(string name,List<HealthCheckItem> item)
{
this.Name = name;
this.Item = item;
}
//计算总价格
public void CalcPrice()
{
int totalPrice = ;
foreach (HealthCheckItem item in this.Item)
{
totalPrice += item.Price;
}
this.Price = totalPrice;
}
}
}

然后我们就可以来实现主页面的一些功能键了

1.主界面的初始工作

        //建立所有 检查项目 集合
// List<HealthCheckItem> Alllist = new List<HealthCheckItem>();
Dictionary<string, HealthCheckItem> Alllist = new Dictionary<string, HealthCheckItem>(); //建立 套餐中的 检查项目 集合
List<HealthCheckItem> list = new List<HealthCheckItem>(); //使用字典保存套餐集合
Dictionary<string, HealthCheckSet> dictionary = new Dictionary<string, HealthCheckSet>(); //初始化检查项目
HealthCheckItem item, item2, item3, item4, item5, item6, item7; //定义一个默认套餐
HealthCheckSet moren; //初始化检查项目的方法
public void main()
{
item = new HealthCheckItem("身高","用于检查身高",);
item2 = new HealthCheckItem("体重","用于检查体重",);
item3 = new HealthCheckItem("视力","用于检查视力",);
item4 = new HealthCheckItem("听力","用于检查听力",);
item5 = new HealthCheckItem("肝功能","用于检查肝功能",);
item6 = new HealthCheckItem("B超","用于检查B超", );
item7 = new HealthCheckItem("心电图","用于检查心电图",); Alllist.Add(item.Name,item);
Alllist.Add(item2.Name,item2);
Alllist.Add(item3.Name,item3);
Alllist.Add(item4.Name, item4);
Alllist.Add(item5.Name,item5);
Alllist.Add(item6.Name,item6);
Alllist.Add(item7.Name,item7); //dictionary.Add(item.Name,item); } //生成默认套餐数据
public void yuan()
{
list.Add(item);
list.Add(item2);
list.Add(item3); moren = new HealthCheckSet("入学体检",list);
//计算价格
moren.CalcPrice(); this.dictionary.Add("入学体检", moren); } //套餐列表 下拉框的加载方法
public void combox()
{
cbm_sum.Items.Clear();
cbm_sum.Items.Add("--请选择--");
foreach (string item in dictionary.Keys)
{
cbm_sum.Items.Add(item);
}
//默认第一项为选中
cbm_sum.SelectedIndex = ;
}
//检查项目 下拉框的加载方法
public void combox2()
{
cmb_xiang.Items.Clear();
cmb_xiang.Items.Add("--请选择--");
foreach (string item in Alllist.Keys)
{
cmb_xiang.Items.Add(item);
}
//默认第一项为选中
cmb_xiang.SelectedIndex = ;
}
private void FrmMain_Load(object sender, EventArgs e)
{
main(); //初始化检查项目
yuan(); //生成默认套餐数据
combox(); //套餐列表 下拉框的加载
combox2();//检查项目 下拉框的加载 }
   //填充套餐的DataGridView的方法
public void UpdateSet(HealthCheckSet set)
{
if (set.Item == null)
{
//给DataGridView的数据源赋空值
dgv.DataSource = new BindingList<HealthCheckItem>();
return;
}
else
{
dgv.DataSource = new BindingList<HealthCheckItem>(set.Item);
} }

2.添加套餐按钮

  //添加套餐
private void but_add_Click(object sender, EventArgs e)
{
if (txt_name.Text!="")
{
//判断字典中是否有你想要添加的套餐
if (dictionary.Keys.Contains(txt_name.Text))
{ MessageBox.Show("已经有该套餐了", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
return;
}
else
{
//给health实例化
List<HealthCheckItem> hao = new List<HealthCheckItem>();
HealthCheckSet health = new HealthCheckSet();
health.Item = hao;
health.Name = "";
health.Price = ;
this.dictionary.Add(txt_name.Text, health);
combox();
cbm_sum.Text = txt_name.Text;
txt_name.Text = ""; }
}
else
{
MessageBox.Show("添加的不能为空!", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
} }

3.选择(改变)套餐

  //选择套餐
private void cbm_sum_SelectedIndexChanged(object sender, EventArgs e)
{ string setName = cbm_sum.Text;
if (cbm_sum.Text=="--请选择--")
{
//给DataGridView的数据源赋空值
dgv.DataSource = new BindingList<HealthCheckItem>(); lab_xianshiname.Text = "";
cmb_xiang.Text = "";
lab_xianshiprice.Text = "";
but_new.Enabled = false;
return; }
else
{
lab_xianshiname.Text = setName;
if ( dictionary[setName]!=null)
{
//根据套餐名给DataGridView绑定数据
UpdateSet(dictionary[setName]);
}
else
{
//给DataGridView的数据源赋空值
dgv.DataSource = new BindingList<HealthCheckItem>();
} //根据套餐名给其中的检查项求总价格
lab_xianshiprice.Text = dictionary[setName].Price.ToString();
} }

4.添加检查项目按钮

//添加项目
private void but_new_Click(object sender, EventArgs e)
{
string name = cmb_xiang.Text;
//根据你选择的套餐找到相应的项目集合,同时判断聚合中是否有你想要添加的项
if (!dictionary[cbm_sum.Text].Item.Contains(Alllist[name]))//没有,添加
{
dictionary[cbm_sum.Text].Item.Add(Alllist[name]);
MessageBox.Show("添加成功!","提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
dgv.DataSource = new BindingList<HealthCheckItem>(dictionary[cbm_sum.Text].Item);
dictionary[cbm_sum.Text].CalcPrice();
//根据套餐名给其中的检查项求总价格
lab_xianshiprice.Text = dictionary[cbm_sum.Text].Price.ToString();
}
else//有,则提示
{
MessageBox.Show("已经有该项目的存在了", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
} }

5.删除按钮

        //删除
private void but_shan_Click(object sender, EventArgs e)
{
string key = dgv.SelectedRows[].Cells[].Value.ToString();
this.dictionary[cbm_sum.Text].Item.Remove(Alllist[key]); dgv.DataSource = new BindingList<HealthCheckItem>(dictionary[cbm_sum.Text].Item);
but_shan.Enabled = false;//删除按钮的禁用 }
public string name;
//选中
private void dgv_CellClick(object sender, DataGridViewCellEventArgs e)
{
if (dgv.SelectedRows.Count!=||dgv.SelectedRows[].Cells[].Value==null)
{
MessageBox.Show("请你正确的选择一行!","提示",MessageBoxButtons.OKCancel,MessageBoxIcon.Error);
return;
}
else
{
name = dgv.SelectedRows[].Cells[].Value.ToString();
but_shan.Enabled = true;//删除按钮的可用
} }

6.添加按钮的可用或禁用

 //添加按钮的 是否可用(检查项目下拉框的SelectedIndexChanged事件)
private void cmb_xiang_SelectedIndexChanged(object sender, EventArgs e)
{ if (cmb_xiang.Text == "--请选择--"||cbm_sum.Text=="--请选择--")
{
but_new.Enabled = false;//禁用
}
else
{
but_new.Enabled = true;//可用
} }

这样,我们的项目就完成了,有一些可以优化的,忘大家别忘了,没有一样东西可以是永远的经典哟!

字典集合Dictionary<K,V>和构造的应用==>>体检套餐项目的更多相关文章

  1. C#泛型集合—Dictionary<K,V>使用技巧

    转载:http://blog.csdn.net/a125138/article/details/7742022 1.要使用Dictionary集合,需要导入C#泛型命名空间 System.Collec ...

  2. 转载C#泛型集合—Dictionary<K,V>使用技巧

    1.要使用Dictionary集合,需要导入C#泛型命名空间 System.Collections.Generic(程序集:mscorlib) 2.描述 1).从一组键(Key)到一组值(Value) ...

  3. 键值对集合Dictionary<K,V>根据索引提取数据

    Dictionary<K,V>中ToList方法返回 List<KeyValuePair<K,V>>定义可设置检索的键/值对

  4. 基础才是重中之重~Dictionary<K,V>里V的设计决定的性能

    回到目录 字典对象Dictionary<K,V>我们经常会用到,而在大数据环境下,字典使用不当可能引起性能问题,严重的可能引起内在的溢出! 字典的值建议为简单类型,反正使用Tuple< ...

  5. C#泛型集合之Dictionary<k, v>使用技巧

    1.要使用Dictionary集合,需要导入C#泛型命名空间 System.Collections.Generic(程序集:mscorlib) 2.描述 1).从一组键(Key)到一组值(Value) ...

  6. C#基础精华03(常用类库StringBuilder,List<T>泛型集合,Dictionary<K , V> 键值对集合,装箱拆箱)

    常用类库StringBuilder StringBuilder高效的字符串操作 当大量进行字符串操作的时候,比如,很多次的字符串的拼接操作. String 对象是不可变的. 每次使用 System. ...

  7. 泛型集合List<T> Dictionary<K,V>

    List<T>类似于ArrayList,ArrayList的升级版. 各种方法:Sort().Max().Min().Sum()…   Dictionary<K,V>类似于Ha ...

  8. 10集合:List<T>,Dictionary<K,V>

    List<T>泛型集合 List<T>是C#中一种快捷.易于使用的泛型集合类型,使用泛型编程为编写面向对象程序增加了极大的效率和灵活性.   1.List<T>用法 ...

  9. Array,ArrayList、List<T>、HashSet<T>、LinkedList<T>与Dictionary<K,V>

    Array: 数组在C#中最早出现的.在内存中是连续存储的,所以它的索引速度非常快,而且赋值与修改元素也很简单. 但是数组存在一些不足的地方.在数组的两个数据间插入数据是很麻烦的,而且在声明数组的时候 ...

随机推荐

  1. 从远程服务器数据库中同步数据到本地数据库 sql server 2008 开启分布

    控制面板\所有控制面板项\管理工具 打开“管理工具――组件服务”,以此打开“组件服务――计算机”,在“我的电脑”上点击右键.在MSDTC选项卡中,点击“安全配置”按钮. 在安全配置窗口中做如下设置: ...

  2. 多条件动态LINQ 组合查询

    本文章转载:http://www.cnblogs.com/wangiqngpei557/archive/2013/02/05/2893096.html 参考:http://dotnet.9sssd.c ...

  3. 给animator动态添加事件

    using UnityEngine; using System.Collections; public class setAnimationEvent : MonoBehaviour { public ...

  4. xml和xsd架构文档相关知识

    1.使用架构(XSD)验证XML文件 2.使用自动生成工具: 工具目录:C:\Program Files (x86)\Microsoft SDKs\Windows\v8.0A\bin\NETFX 4. ...

  5. 公钥、私钥、CA认证、数字签名、U盾

    感谢传智播客的方立勋老师,在一个教学视频上,他巧妙地以蒋介石给宋美龄写密信作为例子,生动地讲述了软件密码学知识. 加密分为对称加密和非对称加密,我们传统理解的,发送数据之前使用一个加密器加密,接到数据 ...

  6. [转]UML八大误解

    潘加宇 本文删节版发表于<程序员>2013年11期 UML(统一建模语言)是软件建模的表示法标准.我从2002年开始专门从事研究和推广UML的工作,在为软件组织提供UML相关需求和设计技能 ...

  7. SQL Server 中关于 @@error 的一个小误区

    在SQL Server中,我常常会看到有些前辈这样写: ) ROLLBACK TRANSACTION T else COMMIT TRANSACTION T 一开始,我看见别人这么写,我就想当然的以为 ...

  8. java攻城狮之路(Android篇)--Activity生命

    一:Activity的激活 1.写一个类 extends Activity Activity是android的四大组件之一.Activity的激活分为显式意图激活和隐式意图激活.如果一个activit ...

  9. MyBatis知多少(3)

    解决存储过程固有限制的方法之一就是将SQL嵌入到更加通用的语言中去.与存储过程将业务逻辑移入数据库相反,内联SQL将SQL从数据库移入了应用程序代码.这就使得SQL语句可以直接与语言进行交互.从某种意 ...

  10. Tips9: Destroy( )函数中的 延迟摧毁 功能

    你知道Object.Destroy()函数吗?在脚本中用来摧毁一个游戏物体或组件,可是你知道他能在执行后延迟一段时间后才摧毁物体吗,其实很简单: using UnityEngine; public c ...