效果

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

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. WebDriver等待和同步对象(基于C#)

    WebDriver等待和同步对象(基于C#) http://www.docin.com/p-748352113.html

  2. 3种归并操作js代码

    /**良哥的*/ function merge(a, b) { var aLen = a.length, bLen = b.length, maxLen = Math.max(aLen, bLen), ...

  3. Microsoft 2013 新技术学习笔记 三

    什么是代码结构的组织?asp.net MVC 5 默认创建出的几个目录的标准含义分别如下: Controllers目录存放MVC模式中的Controler Models目录存放MVC模式中的Model ...

  4. php 5.4 5.5 如何连接 ms sqlserver

    https://msdn.microsoft.com/en-us/sqlserver/ff657782.aspx

  5. 另类angularjs应用

    回顾 上一篇文章主要讲解了创建兼容任意浏览器(主要是ie的一些奇葩问题)的angularjs web应用,但是项目开发中其实更重要的还是在功能的模块化.代码自动压缩上面,这样项目在后期维护或者功能的重 ...

  6. 在 Ubuntu 配置 PPTP Server

    本文在 Ubuntu 12.4 或 14 亲测有效. 建立 PPTP 服务器 首先安装 pptp 服务器. # apt-get install pptpd 然后配置 pptpd. # sudo vi ...

  7. win7激活

    应亲戚要求,装了次win7系统,重新删除分区,格盘,重新划分好分区.完毕.发现系统分区全自动变成动态磁盘.使用win7激活工具时,注意选择使用 小马通用版激活工具. 动态磁盘 稍后解释 小马工具

  8. java中图片文件的判断

    javax.imageio 类 ImageIO BufferedImage bi = ImageIO.read(resFile);//resFile --- InputStream if(bi == ...

  9. Tomcat--配置tomcat,使其除了接受本地访问外,拒绝其他 IP 的访问

    解决方案:修改server.xml文件,在</host>前添加代码: <Valve className="org.apache.catalina.valves.Remote ...

  10. webpack多页面开发与懒加载hash解决方案

    之前讨论了webpack的hash与chunkhash的区别以及各自的应用场景,如果是常规单页面应用的话,上篇文章提供的方案是没有问题的.但是前端项目复杂多变,应对复杂多页面项目时,我们不得不继续踩w ...