using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Lesson5_1
{
public partial class FrmMain : Form
{
public FrmMain()
{
InitializeComponent();
}
List<HealthCheckItem> AllItems = new List<HealthCheckItem>();
Dictionary<string, HealthCheckSet> d = new Dictionary<string, HealthCheckSet>();
/// <summary>
/// 加载数据
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void FrmMain_Load(object sender, EventArgs e)
{
Ban();
Healthset();
}
/// <summary>
/// 总价格
/// </summary>
public void Calcpriceadd()
{
int price = 0;
foreach (var item in d)
{
if (item.Key.Equals(cmbTlist.Text.Trim()))
{
item.Value.CalcPrice();
price = item.Value.TPrice;
break;
}
}
lblprice.Text = price.ToString();
}
public void Ban()
{
//绑定检查项目数据
HealthCheckItem he = new HealthCheckItem();
he.Name = "请选择";
HealthCheckItem h = new HealthCheckItem("用于检查身高。", "身高", 5);
HealthCheckItem h1 = new HealthCheckItem("用于检查体重。", "体重", 5);
HealthCheckItem h2 = new HealthCheckItem("用于检查视力。", "视力", 5);
HealthCheckItem h3 = new HealthCheckItem("用于检查听力。", "听力", 5);
HealthCheckItem h4 = new HealthCheckItem("用于检查肝功能。", "肝功能", 50);
HealthCheckItem h5 = new HealthCheckItem("用于检查B超。", "B超", 30);
HealthCheckItem h6 = new HealthCheckItem("用于检查心电图。", "心电图", 60);
AllItems.Add(he);
AllItems.Add(h);
AllItems.Add(h1);
AllItems.Add(h2);
AllItems.Add(h3);
AllItems.Add(h4);
AllItems.Add(h5);
AllItems.Add(h6);
BindItem();
}
/// <summary>
/// 数据绑定
/// </summary>
public void BindItem()
{
cmbcheck.DataSource = new List<HealthCheckItem>(AllItems);
cmbcheck.DisplayMember = "Name";
cmbcheck.ValueMember = "";
}
public void Healthset()
{
HealthCheckItem h = new HealthCheckItem("用于检查身高。", "身高", 5);
HealthCheckItem h1 = new HealthCheckItem("用于检查体重。", "体重", 5);
HealthCheckItem h2 = new HealthCheckItem("用于检查视力。", "视力", 5);
//绑定套餐列表
HealthCheckSet sss = new HealthCheckSet();
sss.TName = "入学体检";
HealthCheckSet ccc = new HealthCheckSet();
ccc.TName = "请选择";
d.Add("请选择",ccc);
d.Add("入学体检", sss);
d["入学体检"].Items.Add(h);
d["入学体检"].Items.Add(h1);
d["入学体检"].Items.Add(h2);
BindSet();
BindDgv();
}
/// <summary>
/// 绑定DGV
/// </summary>
public void BindDgv()
{
dgvList.AutoGenerateColumns = false;
dgvList.DataSource = new List<HealthCheckItem>(d[cmbTlist.Text.Trim()].Items);
}
public void BindSet()
{
//绑定数据
BindingSource bs = new BindingSource();
bs.DataSource = d.Keys;
cmbTlist.DataSource = bs;
cmbTlist.DisplayMember = "Tname";
}
/// <summary>
/// 显示数据
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void cmbTlist_SelectedIndexChanged(object sender, EventArgs e)
{
if (cmbTlist.Text.Trim().Equals("请选择"))
{
dgvList.DataSource = null;
lblName.Text = "";
lblprice.Text = "";
}
else
{
lblName.Text = cmbTlist.Text.Trim();
Calcpriceadd();
BindItem();
BindDgv();
}
}
/// <summary>
/// 给套餐添加项
/// </summary>
/// <returns></returns>
public HealthCheckItem xunhan()
{
foreach (var item in AllItems)
{
if (cmbcheck.Text.Trim().Equals(item.Name))
{
return item;
}
}
return null;
}
/// <summary>
/// 添加套餐
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnInsert_Click(object sender, EventArgs e)
{
if (rox() == true)
{
MessageBox.Show("添加成功!");
d.Add(txtTName.Text.Trim(), new HealthCheckSet());
BindSet();
txtTName.Text = "";
}
else
{
MessageBox.Show("有重复!");
}
}
/// <summary>
/// 判断套餐是否重复
/// </summary>
/// <returns></returns>
public bool rox ()
{
foreach (var item in d.Keys)
{
if (txtTName.Text.Trim().Equals(item))
{
return false;
}
}
return true;
}
/// <summary>
/// 添加检查列表
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnInsert1_Click(object sender, EventArgs e)
{
if (hel() == true)
{
var v = d[cmbTlist.Text.Trim()];
v.Items.Add(xunhan());
BindDgv();
Calcpriceadd();
}
else
{
MessageBox.Show("有重复!");
}
}
/// <summary>
/// 是否有重复检查项
/// </summary>
/// <returns></returns>
public bool hel()
{
var c = d[cmbTlist.Text.Trim()];
foreach (var item in c.Items)
{
if (item.Name.Equals(cmbcheck.Text.Trim()))
{
return false;
}
}
return true;
}
/// <summary>
/// 按钮
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void cmbcheck_TextChanged(object sender, EventArgs e)
{
if (cmbcheck.Text.Equals("请选择"))
{
btnInsert1.Enabled = false;
btndelete.Enabled = false;
}
else
{
btnInsert1.Enabled = true;
btndelete.Enabled = true;
}
}
/// <summary>
/// 删除添加数据
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btndelete_Click(object sender, EventArgs e)
{
string name = dgvList.SelectedRows[0].Cells[0].Value.ToString();
if (dgvList.SelectedRows.Count > 0)
{
for (int i = 0; i < d[cmbTlist.Text.Trim()].Items.Count; i++)
{
if (name.Equals(d[cmbTlist.Text.Trim()].Items[i].Name))
{
DialogResult result = MessageBox.Show("是否删除" + name + "项", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
if (result == DialogResult.Yes)
{
d[cmbTlist.Text.Trim()].Items.RemoveAt(i);
BindDgv();
Calcpriceadd();
MessageBox.Show("删除" + name + "成功");
return;
}
}
}
}
else
{
MessageBox.Show("请选择要删除的项!");
}
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Lesson5_1
{
public class HealthCheckSet
{
public List<HealthCheckItem> Items { get; set; }
public int TPrice { get; set; }
public string TName { get; set; }
//套餐总价格
public void CalcPrice()
{
int totalPrice = 0;
foreach (var item in Items)
{
totalPrice += item.Price;
}
this.TPrice = totalPrice;
}
public HealthCheckSet()
{
this.Items = new List<HealthCheckItem>();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Lesson5_1
{
public class HealthCheckItem
{
public string Description { get; set; }
public string Name { get; set; }
public int Price { get; set; }
public HealthCheckItem()
{
}
public HealthCheckItem(string description,string name ,int price)
{
this.Description = description;
this.Name = name;
this.Price = price;
}
}
}