public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//保存所有体检项目
List<HealthCheckItem> AllItems = new List<HealthCheckItem>();
//保存套餐中的体检项目
List<HealthCheckSet> items = new List<HealthCheckSet>();
Dictionary<string, HealthCheckSet> HealthSet = new Dictionary<string, HealthCheckSet>();
public void Project()
{
HealthCheckItem height = new HealthCheckItem("身高", "用于测量身高",20);
HealthCheckItem weight = new HealthCheckItem("体重", "用于测量体重", 30);
HealthCheckItem see = new HealthCheckItem("视力", "用于测量视力", 40);
HealthCheckItem ear = new HealthCheckItem("听力", "用于测量听力", 50);
HealthCheckItem gan = new HealthCheckItem("肝功能", "用于测量肝功能", 60);
HealthCheckItem Bchao = new HealthCheckItem("B超", "用于测量身孕", 70);
HealthCheckItem heart = new HealthCheckItem("心电图", "用于测量心脑", 80);
AllItems.Add(height);
AllItems.Add(weight);
AllItems.Add(see);
AllItems.Add(ear);
AllItems.Add(gan);
AllItems.Add(Bchao);
AllItems.Add(heart);
foreach (HealthCheckItem item in this.AllItems)
{
this.Check.Items.Add(item.Name);
}
this.Check.SelectedIndex = 0;
}
public void GoSchool()
{
HealthCheckItem height = new HealthCheckItem("身高", "用于检查身高", 20);
HealthCheckItem weight = new HealthCheckItem("体重", "用于检查体重", 30);
HealthCheckItem see = new HealthCheckItem("视力", "用于检查视力", 40);
AllItems.Add(height);
AllItems.Add(weight);
AllItems.Add(see);
HealthCheckSet health = new HealthCheckSet("入学体检", AllItems);
HealthSet.Add("入学体检", health);
this.tcList.Items.Add("请选择");
this.Check.SelectedIndex = 0;
foreach (var item in HealthSet)
{
this.tcList.Items.Add(item.Key);
}
}
//下拉列表事件
private void tcList_SelectedIndexChanged(object sender, EventArgs e)
{
string setName = this.tcList.Text;
if (setName == "请选择")
{
this.dgvShow.DataSource = new BindingList<HealthCheckItem>();
labName.Text = "";
labPrice.Text = "";
return;
}
else
{
labName.Text =this.HealthSet[setName].Name;
labPrice.Text = this.HealthSet[setName].Price.ToString();
UpdateSet(HealthSet[setName]);
this.DelectBtn.Enabled = true;
}
}
//更新套餐检查项目
//绑定 DataGridView
private void UpdateSet(HealthCheckSet set)
{
this.dgvShow.DataSource = new BindingList<HealthCheckItem>(set.Items);
}
//窗口加载 调用方法
private void Form1_Load(object sender, EventArgs e)
{
Project();
GoSchool();
}
//添加
private void Addbtn1_Click(object sender, EventArgs e)
{
string wantAdd = this.tcList.Text;
int index = this.Check.SelectedIndex-1;
MessageBox.Show(index.ToString());
try
{
if (!this.HealthSet[wantAdd].Items.Contains(AllItems[index]))
{
this.HealthSet[wantAdd].Items.Add(AllItems[index]);
labName.Text = this.HealthSet[wantAdd].Name;
HealthSet[wantAdd].CalcPrice();
labPrice.Text = this.HealthSet[wantAdd].Price.ToString();
UpdateSet(HealthSet[wantAdd]);
MessageBox.Show("添加成功", "提示");
}
else
{
MessageBox.Show("该项目存在", "提示");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void Addbtn2_Click(object sender, EventArgs e)
{
string sename = this.tcName.Text;
int count = this.tcList.Items.Count;
if (!HealthSet.Keys.ToList().Contains(sename))
{
List<HealthCheckItem> health = new List<HealthCheckItem>();
HealthCheckSet hea = new HealthCheckSet(sename,health);
HealthSet.Add(sename,hea);
this.tcList.Items.Add(sename);
this.tcList.SelectedIndex = count;
UpdateSet(HealthSet[sename]);
}
else {
MessageBox.Show("该套餐已经存在","提示");
}
}
//删除
private void DelectBtn_Click(object sender, EventArgs e)
{
string sename = this.tcList.Text;
if (MessageBox.Show("确认删除吗?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
{
int index = this.dgvShow.SelectedRows[0].Index;
if (this.dgvShow.SelectedRows.Count > 0)
{
this.HealthSet[sename].Items.RemoveAt(index);
labName.Text = this.HealthSet[sename].Name;
HealthSet[sename].CalcPrice();
labPrice.Text = this.HealthSet[sename].Price.ToString();
UpdateSet(HealthSet[sename]);
}
else
{
MessageBox.Show("请选中一行", "提示");
}
}
}
//检查项目
public class HealthCheckItem
{
public string Name { get; set; } //项目名称
public string Description { get; set; } //项目描述
public int Price { get; set; } //项目价格
public HealthCheckItem() { }
public HealthCheckItem(string name, string description, int price)
{
this.Name = name;
this.Description = description;
this.Price = price;
}
//体检套餐
public class HealthCheckSet
{
public int Price { get; set; } //套餐价格
public string Name { get; set; } //套餐名称
private List<HealthCheckItem> items; // HealthCheckItem 的集合 使用泛型集合存储
public List<HealthCheckItem> Items
{
get { return items; }
set { items = value; }
}
public HealthCheckSet() { }
public HealthCheckSet(string name, List<HealthCheckItem> items)
{
this.Name = name;
this.Items = items;
}
//套餐价格计算的方法
public void CalcPrice()
{
int totalPrice = 0;
foreach (HealthCheckItem item in this.Items)
{
totalPrice = this.Price;
}
this.Price = totalPrice;
}
体检套餐 关键代码
最新推荐文章于 2023-12-11 10:00:00 发布
