效果

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

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. Spring AOP Schema aop:config、tx:advice

    Spring AOP Schema  aop:config.tx:advice 一.      利用aop:config标签实现AOP 首先看个例子,如下 接口代码: package com.lei. ...

  2. OceanBase RPC机制简要说明

    http://note.youdao.com/share/?id=d2163a7ba8ec1ec58e64683a961c5121&type=note RPC是OB一个比较重要的各个组件间进行 ...

  3. 【工作代码】复杂 JSON 值替换处理

    总结下最近的工作遇到的点:入参复杂 JSON 层层嵌套,Java 怎么优雅的处理. 一.关于 JSON JSON 是类似 XML 用于存储和交互文本信息.但优于 XML ,其更小,更快,更易懂和解析. ...

  4. Android开发之ProgressDialog在独立Thread线程中更新进度

    简单的需求:在一个工作Thread中更新进度对话框ProgressDialog 遇到的问题: 1,创建需要Context,这个需要传进来 2,Thread中不能创建ProgressDialog,否则需 ...

  5. SQL设置SQLServer最大连接数查询语句

    设置最大连接数 下面的T-SQL 语句可以配置SQL Server 允许的并发用户连接的最大数目. exec sp_configure 'show advanced options', 1exec s ...

  6. C++11 智能指针unique_ptr使用 -- 以排序二叉树为例

    用智能指针可以简化内存管理.以树为例,如果用普通指针,通常是在插入新节点时用new,在析构函数中调用delete:但有了unique_ptr类型的智能指针,就不需要在析构函数中delete了,因为当u ...

  7. (转)关于tcp和udp的缓冲区

    (一)基础知识 IPv4 数据报最大大小是65535(16位),包括IPv4头部. IPv6 数据报最大大小是65575,包括40个字节的IPv4头部 MTU,这是由硬件规定的,如以太网的MTU是15 ...

  8. [转]非OpenVZ下利用谷歌TCP-BBR协议单边加速你的VPS

    前段时间谷歌推出了新的 TCP-BBR 开源算法,可以起到单边加速 TCP 连接的效果,也就是不用客户端的配合,用来替代收费的锐速再合适不过,毕竟开源免费.TCP-BBR 的目的是要尽量跑满带宽,并且 ...

  9. Python使用libsvm的“ImportError: No module named svmutil”问题

    from:http://blog.csdn.net/xmu_jupiter/article/details/46830327 这几天快被Python的一些细节问题整死了,浪费了不少时间.现在把这些记录 ...

  10. AssetBundle系列——共享资源打包/依赖资源打包

    有人在之前的博客中问我有关共享资源打包的代码,其实这一块很简单,就两个函数: BuildPipeline.PushAssetDependencies():依赖资源压栈: BuildPipeline.P ...