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 Text { public partial class FrmMain : Form { public FrmMain() { InitializeComponent(); } HealthCheckItem ShenGao, TiZhong, GanGongN; //存储所有检查项目 List<HealthCheckItem> AllItems = new List<HealthCheckItem>(); //存储与套餐相关的检查项目 List<HealthCheckItem> items = new List<HealthCheckItem>(); //保存医院中支持的所有TC,为了绑定左侧的套餐下拉框而编写 public Dictionary<string, TaoCan> HSet = new Dictionary<string, TaoCan>(); public void testSet() { ShenGao = new HealthCheckItem(5, "检查身高", "身高"); TiZhong = new HealthCheckItem(5, "检查体重", "体重"); GanGongN = new HealthCheckItem(5, "检查肝功能", "肝功能"); AllItems.Add(ShenGao); AllItems.Add(TiZhong); AllItems.Add(GanGongN); } //构建一个默认的套餐,并且扔到所有套餐(TC)集合中 public void Tc() { TaoCan tc = new TaoCan(); tc.Name = "入学体检"; tc.Items.Add(ShenGao); tc.Items.Add(TiZhong); //请选择 TaoCan tc2 = new TaoCan(); tc2.Name = "请选择"; HSet.Add(tc2.Name, tc2); HSet.Add(tc.Name, tc); } //更新套餐检查项目(职责分离) private void updateSet(TaoCan set) { dgvList.DataSource = new BindingList<HealthCheckItem>(set.Items); } //定义一个标记 判定当前Load事件是否执行完毕 int flag = 0; private void FrmMain_Load(object sender, EventArgs e) { //右侧下拉框 testSet(); //左侧 Tc(); //所有下拉框中的项目Clear() BindingSource source = new BindingSource(); source.DataSource = HSet.Values; cboSets.DisplayMember = "name"; cboSets.ValueMember = "items"; cboSets.DataSource = source; BindRightDGV(); Console.WriteLine(""); flag = 1; } private void BindRightDGV() { cboItems.DataSource = new BindingList<HealthCheckItem>(AllItems); } private void cboSets_SelectedIndexChanged(object sender, EventArgs e) { if (flag == 1) { string SetName = cboSets.Text; if (SetName == "请选择") { HSet[SetName].Items.Clear(); updateSet(HSet[SetName]); // dgvList.DataSource = null; lblSetName.Text = ""; lblSetPrice.Text = ""; return; } //设置套餐名称 lblSetName.Text = HSet[SetName].Name; HSet[SetName].CalcPrice(); //设置套餐总价 lblSetPrice.Text = HSet[SetName].Price.ToString(); //更新套餐检查项目 updateSet(HSet[SetName]); //设置清楚按钮为可用状态 btnDelete.Enabled = true; } } private void cboItems_SelectedIndexChanged(object sender, EventArgs e) { if (cboItems.Text != "请选择") { btnAdd.Enabled = true; } else { btnAdd.Enabled = false; } } private void btnTaoCanAdd_Click(object sender, EventArgs e) { if (string.IsNullOrEmpty(txtName.Text)) { MessageBox.Show("请选择套餐名称","提示",MessageBoxButtons.OK,MessageBoxIcon.Information); return; } else { TaoCan Hch = new TaoCan(); Hch.Name=txtName.Text; HSet.Add(Hch.Name,Hch); BindingSource source = new BindingSource(); source.DataSource = HSet.Values; cboSets.DisplayMember = "name"; cboSets.ValueMember = "items"; cboSets.DataSource = source; MessageBox.Show("成功!"); //将新加入的套餐默认选中 cboSets.SelectedIndex = HSet.Count-1; } } private void btnDelete_Click(object sender, EventArgs e) { //体检项目的名称 if (dgvList.SelectedRows.Count>0) { string name=dgvList.SelectedRows[0].Cells[0].Value.ToString(); foreach (HealthCheckItem item in HSet[lblSetName.Text].Items) { if (item.Name==name) { HSet[lblSetName.Text].Items.Remove(item); break; } } updateSet(HSet[lblSetName.Text]); //价格 HSet[lblSetName.Text].CalcPrice(); lblSetPrice.Text = HSet[lblSetName.Text].Price.ToString(); } } } }
1.先创建以下几个类:
套餐类:TaoCan
检查项目类:HealthCheckItem
时间: 2024-11-04 18:52:08