效果






首先,我們先來准備我們需要的類
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 = 0;
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("身高","用於檢查身高",10);
item2 = new HealthCheckItem("體重","用於檢查體重",5);
item3 = new HealthCheckItem("視力","用於檢查視力",15);
item4 = new HealthCheckItem("聽力","用於檢查聽力",20);
item5 = new HealthCheckItem("肝功能","用於檢查肝功能",100);
item6 = new HealthCheckItem("B超","用於檢查B超", 10);
item7 = new HealthCheckItem("心電圖","用於檢查心電圖",100);
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 = 0;
}
//檢查項目 下拉框的加載方法
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 = 0;
}
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 = 0;
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[0].Cells[1].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!=1||dgv.SelectedRows[0].Cells[1].Value==null)
{
MessageBox.Show("請你正確的選擇一行!","提示",MessageBoxButtons.OKCancel,MessageBoxIcon.Error);
return;
}
else
{
name = dgv.SelectedRows[0].Cells[1].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;//可用
}
}
這樣,我們的項目就完成了,有一些可以優化的,忘大家別忘了,沒有一樣東西可以是永遠的經典喲!