效果

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

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. iOS 7新功能例子

    参考https://github.com/shu223/iOS7-Sampler Code examples for the new functions of iOS 7. Contents Dyna ...

  2. SSH: 本地.ssh目录下的公钥文件最好删掉

    这次ssh amazon ec2的instance,在家里电脑登录OK,到了公司电脑登录失败(只支持公钥机制).私钥已经拷贝到公司的ubuntu上了,奇怪. 后来发现是.ssh目录下存在一些公钥文件导 ...

  3. AssetBundle系列——场景资源之解包(二)

    本篇接着上一篇继续和大家分享场景资源这一主题,主要包括两个方面: (1)加载场景 场景异步加载的代码比较简单,如下所示: private IEnumerator LoadLevelCoroutine( ...

  4. Adroid学习系列-入门(1)

    1.  安装 安装Eclipse,安装Adroid插件.安装Adroid SDK. 2.  项目目录说明 2.1.       建立Adroid项目 与一般的Java项目一样 )src文件夹是项目的所 ...

  5. ruby -- 进阶学习(六) devise修改邮件发送者邮箱

    在config/environment.rb/development.rb或者config/environment/production.rb中, 简单示范例子: Text03::Applicatio ...

  6. ES6入门系列一(基础)

    1.let命令 Tips: 块级作用域(只在当前块中有效) 不会变量提升(必须先申明在使用) 让变量独占该块,不再受外部影响 不允许重复声明 总之:let更像我们熟知的静态语言的的变量声明指令 ES6 ...

  7. DDD:Can I DDD?

    下面是<实现领域驱动>的作者给出的一段话: You can implement DDD if you have: A passion for creating excellent soft ...

  8. Gulp:新一代前端构建利器

    1.什么是Gulp gulp.js 是一种基于流的,代码优于配置的新一代构建工具. Gulp 和 Grunt 类似.但相比于 Grunt 的频繁的 IO 操作,Gulp 的流操作,能更快地完成构建. ...

  9. Nginx: could not build the server_names_hash 解决办法

    # /etc/init.d/nginx reload * Reloading nginx configuration nginx [fail] # nginx -t nginx: [emerg] co ...

  10. Device.js – 快速检测平台、操作系统和方向信息

    在 Web 项目中,有时候我们需要根据程序运行的环境采取特定操作.Device.js 是一个很小的 JavaScript 库,它简化了编写和平台,操作系统或浏览器相关的条件 CSS 或 JavaScr ...