首页 > 学院 > 开发设计 > 正文

C#第五章 套餐体检

2019-11-08 02:05:05
字体:
来源:转载
供稿:网友

using System;  using System.Collections.Generic;  using System.Linq;  using System.Text;    namespace 体检项目  {     public class CheckSet      {          public string Name { get; set; }          public int PRice { get; set; }          public List<Health> items { get; set; }          public CheckSet()          {              items = new List<Health>();          }          public CheckSet(string name,List<Health> item)          {              this.Name = name;              this.items = item;          }          public void CalPrice()           {              int total = 0;              foreach (Health h in items)              {                  total = total + h.Price;              }              this.Price = total;          }      }  }  [csharp] view plain copy 在CODE上查看代码片派生到我的代码片using System;  using System.Collections.Generic;  using System.Linq;  using System.Text;    namespace 体检项目  {      //检查项目类    public  class Health      {          public string Name { get; set; }          public string Description { get; set; }          public int Price { get; set; }          public Health()          {            }          public Health(string name,string ds,int price)          {              this.Name = name;              this.Description = ds;              this.Price = price;          }      }  }  [csharp] view plain copy 在CODE上查看代码片派生到我的代码片using System;  using System.Collections.Generic;  using System.ComponentModel;  using System.Data;  using System.Drawing;  using System.Linq;  using System.Text;  using System.Windows.Forms;    namespace 体检项目  {      public partial class Form1 : Form      {          public Form1()          {              InitializeComponent();          }          //采用泛型集合保存 所有 的体检项目          List<Health> AllItems = new List<Health>();          //采用泛型集合保存 套餐 中的的体检项目          List<Health> items = new List<Health>();         //使用字典保存套餐集合      public  Dictionary<string, CheckSet> HealthSet = new Dictionary<string, CheckSet>();          Health sg, ti, gan, B,shili;            CheckSet set = new CheckSet();          private void Form1_Load(object sender, EventArgs e)          {              this.label5.Text = "";              this.label7.Text = "";              ADD();              Student();          }          public void ADD()           {                              sg = new Health("身高", "用于检查身高", 5);                  ti = new Health("体重", "用于检查体重", 5);                  gan = new Health("肝功能", "用于检查肝功能", 50);                  B = new Health("B超", "用于检查B超", 30);                  shili = new Health("视力", "用于检查视力", 50);                  AllItems.Add(sg);                  AllItems.Add(ti);                  AllItems.Add(gan);                  AllItems.Add(B);                  AllItems.Add(shili);              //    this.dataGridView1.DataSource = new BindingList<Health>(AllItems);                   this.comboBox2.Items.Add("请选择");                  foreach (Health item in AllItems)                  {                      comboBox2.Items.Add(item.Name);                  }                  this.comboBox2.SelectedIndex = 0;                     }          //设置入学套餐          public void Student()           {              items = new List<Health>();              items.Add(sg);              items.Add(ti);              items.Add(gan);              set = new CheckSet("入学体检",items);              //计算套餐价格              set.CalPrice();              this.HealthSet.Add("入学体检",set);              //清空套餐下拉列表              this.comboBox1.Items.Clear();              //添加请选择              this.comboBox1.Items.Add("请选择");              //将 Dictionary的Key值 绑定到COMBOX1              foreach (string key in HealthSet.Keys)              {                  this.comboBox1.Items.Add(key);              }              //默认选择第一项              this.comboBox1.SelectedIndex = 0;             // this.dataGridView1.DataSource = new BindingList<Health>(items);          }          public void UpdateSet(CheckSet set)           {              this.dataGridView1.DataSource = new BindingList<Health>(set.items);          }            private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)          {              string sNAME = this.comboBox1.Text;              if (sNAME == "请选择")              {                  this.dataGridView1.DataSource = null;                  this.label5.Text = "";                  this.label7.Text = "";                  return;              }              //设置套餐名称              this.label5.Text = this.HealthSet[sNAME].Name;              //设置套餐总价              this.label7.Text = this.HealthSet[sNAME].Price.ToString();              UpdateSet(HealthSet[sNAME]);          }          /// <summary>          /// 添加 套餐 检查的项目 方法          /// </summary>          /// <param name="sender"></param>          /// <param name="e"></param>          private void button2_Click(object sender, EventArgs e)          {              if (this.comboBox2.SelectedIndex==0)              {                  MessageBox.Show("请选择一个项目");                return;              }              //声明一个套餐来接受  套餐 列表的文字              string CBO = comboBox1.Text;              if (CBO=="请选择")              {                  MessageBox.Show("请选择套餐!");                  return;              }             // 判断现有套餐是否存在              int index = this.comboBox2.SelectedIndex-1;              if (! this.HealthSet[CBO].items.Contains(AllItems[index]))              {                  this.HealthSet[CBO].items.Add(AllItems[index]);                  this.HealthSet[CBO].CalPrice();                  UpdateSet(this.HealthSet[CBO]);                  //刷新窗体集合名称                  this.label5.Text = this.HealthSet[CBO].Name;                  //刷新窗体集合的 价格                  this.label7.Text = this.HealthSet[CBO].Price.ToString();                  MessageBox.Show("添加成功!!!","提示");              }              else              {                  MessageBox.Show("该项目已存在","提示");              }          }          //删除项目          private void button3_Click(object sender, EventArgs e)          {              string setName = this.comboBox1.Text;              if (this.dataGridView1.SelectedRows.Count==0)              {                  MessageBox.Show("没有选择删除项");                  return;              }              //获取选中的索引             int index =this.dataGridView1.SelectedRows[0].Index;              this.HealthSet[setName].items.RemoveAt(index);              //重新计算价格              this.HealthSet[setName].CalPrice();              UpdateSet(HealthSet[setName]);              this.label5.Text=set.Name;              this.label7.Text=set.Price.ToString();              MessageBox.Show("删除成功");            }          /// <summary>          /// 添加套餐          /// </summary>          /// <param name="sender"></param>          /// <param name="e"></param>          private void button1_Click(object sender, EventArgs e)          {              if (this.textBox1.Text=="")              {                  MessageBox.Show("请输入要添加的套餐名称");                  return;              }              CheckSet se = new CheckSet();              this.HealthSet.Add(this.textBox1.Text, se);              this.AddSet();             // this.comboBox1.Items.Clear();             //this.comboBox1.Items.Add("请选择");             // foreach (string item in HealthSet.Keys)             // {             //     this.comboBox1.Items.Add(item);             // }             this.comboBox1.SelectedIndex = this.HealthSet.Count;              MessageBox.Show("添加成功");          }          public void AddSet()           {              this.comboBox1.Items.Clear();              this.comboBox1.Items.Add("请选择");              foreach (string item in HealthSet.Keys)              {                  this.comboBox1.Items.Add(item);              }              this.comboBox1.SelectedIndex = 0;          }          }      }  


发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表