标签:
套餐管理系统
本套套餐管理系统,主要是使用泛型list<> 和泛型 dictionary<>集合的使用
public class HealthCheckItem
{
public string description { get; set; }
public string name { get; set; }
public int price { get; set; }
public HealthCheckItem(string name, string description,int price)
{
this.description = description;
this.name = name;
this.price = price;
}
}
先创建项目类
public class HealthCheckSet
{
public List<HealthCheckItem> itemss { get; set; } 创建泛型list用来储存当前套餐
public string name { get; set; } 包含的所有的项目集合
public int price { get; set; }
public HealthCheckSet(string name, List<HealthCheckItem> items)
{
this.name=name;
this.itemss=items;
}
public void prices() 用于计算总价格并给当前字段赋值
{
int mun = 0;
foreach (HealthCheckItem item in itemss)
{
mun += item.price;
}
this.price = mun;
}
}
创建套餐类
首先分析项目的需求,然后确定从哪着手!
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
List<HealthCheckItem> ct = new List<HealthCheckItem>();//当前项目套餐的所有项;
List<HealthCheckItem> hc = new List<HealthCheckItem>();//所有套餐项目;
Dictionary<string,HealthCheckSet> hcs = new Dictionary<string,HealthCheckSet>();//当前套餐
HealthCheckItem height, weight, shili, xingzhang, boold, xingdt; 创建对象
public void chushi()
{
height = new HealthCheckItem("身高", "用于测量身高",20);
weight = new HealthCheckItem("体重", "用于测量体重", 20);
shili = new HealthCheckItem("视力", "用于测量视力", 20);
xingdt = new HealthCheckItem("心电图", "用于测量心电图", 80);
boold = new HealthCheckItem("血液", "用于测量血液", 30);
xingzhang = new HealthCheckItem("心脏", "用于测量心脏", 60);
hc.Add(height);
hc.Add(weight);
hc.Add(shili);
hc.Add(xingdt);
hc.Add(boold);
hc.Add(xingzhang);
foreach (HealthCheckItem item in hc)
{
this.comboBox2.Items.Add(item.name);
}
comboBox2.SelectedIndex = 0;
}
这是创建所有项目并储存到泛型List<HealthCheckItem> hc中去并且实现绑定
组合框绑定默认第一项被选中
public void chushitaoc()
{
height = new HealthCheckItem("身高", "用于测量身高", 20);
weight = new HealthCheckItem("体重", "用于测量体重", 20);
shili = new HealthCheckItem("视力", "用于测量视力", 20);
ct.Add(height);
ct.Add(weight);
ct.Add(shili);
HealthCheckSet h = new HealthCheckSet("入职体检",ct);
hcs.Add("入职体检",h);
this.comboBox1.Items.Add("请选择");
comboBox1.SelectedIndex = 0;
foreach (var item in hcs)
{
this.comboBox1.Items.Add(item.Key);
}
}
这是初始套餐"入职体检"的默认三项项目并绑定组合框omboBox1这里使用froreach遍历集合添加,并且添加"请选择"默认项如果使用this.comboBox1.DataSource=对象会使"请选择"也成为对象,所以最好使用foreach遍历它的key值添加
private void Form1_Load(object sender, EventArgs e)
{
chushi();
chushitaoc();
this.label6.Text ="";
this.label7.Text = "";
}
在窗体load事件中让两个组合框显示
public void add(HealthCheckSet set)
{
this.dataGridView1.DataSource = new BindingList<HealthCheckItem>(set.itemss);
}
//添加数据到datagradeview创建方法以便后续调用
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
int index = this.comboBox1.SelectedIndex;
string se = this.comboBox1.Text;
if (se == "请选择")
{
this.dataGridView1.DataSource = null;
label6.Text = "";
label7.Text = "";
}
else
{
label6.Text = hcs[se].name; //绑定lable框
hcs[se].prices(); //更新价钱
label7.Text = hcs[se].price.ToString();//绑定lable框
add(hcs[se]); //绑定datagridview
}
}
根据套餐的组合框的点击来绑定到dataGridView
private void button2_Click(object sender, EventArgs e)
{
int index = this.comboBox2.SelectedIndex;
string se = this.comboBox1.Text;
if (!this.hcs[se].itemss.Contains(hc[index]))
{
this.hcs[se].itemss.Add(hc[index]);
label6.Text = hcs[se].name; //刷新label上的显示文本
hcs[se].prices(); //当前套餐的总价钱
label7.Text = hcs[se].price.ToString();//刷新label上的显示文本
add(hcs[se]); //更行dataGridView
}
else
{
MessageBox.Show("你所选的项目已存在","提示!");
}
}
实现啊添加当前项目不存在的项目然后刷新各个控件以及dataGridView
private void button3_Click(object sender, EventArgs e)
{
string se = this.comboBox1.Text;
int index = this.dataGridView1.SelectedRows[0].Index;
if (this.dataGridView1.SelectedRows.Count > 0)
{
hcs[se].itemss.RemoveAt(index);
label6.Text = hcs[se].name; //刷新label上的显示文本
hcs[se].prices(); //当前套餐的总价钱
label7.Text = hcs[se].price.ToString(); //刷新label上的显示文本
add(hcs[se]); //更行dataGridView
}
else
{
MessageBox.Show("请选中一行!","提示");
}
}
同添加一样删除项目
private void button1_Click(object sender, EventArgs e)
{
string se = this.textBox1.Text;
if (!hcs.Keys.ToList().Contains(se))
{
List<HealthCheckItem> ct1 = new List<HealthCheckItem>(); //创建新的对象
HealthCheckSet h2 = new HealthCheckSet(se, ct1); //new出新的空间
hcs.Add(se, h2); //套餐中添加新的套餐
this.comboBox1.Items.Add(se); //添加新的套餐名到套餐下拉列表中
this.comboBox1.SelectedIndex = this.comboBox1.Items.Count - 1; //并且当前的套餐
add(hcs[se]); 默认被选中
}
else
{
MessageBox.Show("该套餐已存在","提示");
}
}
添加套餐
标签:
原文地址:http://www.cnblogs.com/meixinyuan/p/4641625.html