体检套餐

1.A类初始化套餐项目

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FirstCase
{
    //体检项目类
   public  class A
    {
       //名称
        public string  Name { get; set; }
       //描述 
       public string  MiaoShu { get; set; }
       //价格 
       public int Price { get; set; }
       /// <summary>
       /// 带参构造
       /// </summary>
       /// <param name="name"></param>
       /// <param name="miaoshu"></param>
       /// <param name="price"></param>
       public A(string name,string miaoshu,int price)
       {
           this.Name = name;
           this.MiaoShu = miaoshu;
           this.Price = price;
       }
    }
}

2.B类保存套餐和套餐项目

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace FirstCase
{
    //套餐类
   public  class B
    {
       public B()
       {
       }
       //套餐名称
        public string Name { get; set; }
       //体检项目对象
        public List<A> list { get; set; }
       //总价格
        public int Price { get; set; }
       //构造
        public B(string name,List<A> list,int price)
        {
            this.Name = name;
            this.list = list;
            this.Price = price;
        }
       //计算价格
        public void SumPrice()
        {
            int sum = 0;
            foreach (A item in list)
            {
                sum += item.Price;
            }
          
            this.Price = sum;
        }
    }
}

3.主窗体实现个控件绑定数据的功能(5个)

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 FirstCase
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        //初始话体检套餐
        List<A> list1 = new List<A>();
        //保存体检套餐中的项目
        List<A> list2 = new List<A>();
        //保存体检套餐和项目
        Dictionary<string, B> dic = new Dictionary<string, B>();
        private void Form1_Load(object sender, EventArgs e)
        {
        
            A a1 = new A("小红","一个女孩子",10);
            A a2 = new A("小名", "一个男孩子", 20);
            A a3 = new A("小军", "一个好老师", 30);
            list1.Add(a1);
            list1.Add(a2);
            list1.Add(a3);
            comboBox2.DataSource = new BindingList<A>(list1);
            comboBox2.ValueMember = "name";
            list2.Add(a1);
            list2.Add(a2);
            B b = new B("入学体检",list2,0);
            b.SumPrice();
            dic.Add(b.Name,b);
            foreach (string item in dic.Keys)
            {
                comboBox1.Items.Add(item);
            }
            //BindingSource bs = new BindingSource();
            //bs.DataSource = dic.Values;
            //comboBox1.DataSource = bs;
           // comboBox1.ValueMember = "name";
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            label4.Text = dic[comboBox1.Text].Name;
            label6.Text = dic[comboBox1.Text].Price.ToString();
            dataGridView1.DataSource = new BindingList<A>(dic[comboBox1.Text].list);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            B b = new B(textBox1.Text,new List<A>(),0);
            dic.Add(b.Name, b);       
            comboBox1.Text = b.Name;
            b.SumPrice();
            this.dataGridView1.DataSource = new BindingList<A>(dic[comboBox1.Text].list);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //MessageBox.Show(comboBox2.SelectedValue.ToString());
            //list2.Add(list1[comboBox2.SelectedIndex]);
            //B b = new B(textBox1.Text,list2,0);
            //dic.Add(textBox1.Text, b);
            //b.SumPrice();
            //dataGridView1.DataSource = new BindingList<A>(dic[comboBox1.Text].list);


            dic[comboBox1.Text].list.Add(list1[comboBox2.SelectedIndex]);
            dic[comboBox1.Text].SumPrice();
            dataGridView1.DataSource = new BindingList<A>(dic[comboBox1.Text].list);
            label4.Text = dic[comboBox1.Text].Name;
            label6.Text = dic[comboBox1.Text].Price.ToString();
        }

        private void button3_Click(object sender, EventArgs e)
        {
              dic[comboBox1.Text].list.Remove(list1[comboBox2.SelectedIndex]);
              dic[comboBox1.Text].SumPrice();
              dataGridView1.DataSource = new BindingList<A>(dic[comboBox1.Text].list);
              label4.Text = dic[comboBox1.Text].Name;
              label6.Text = dic[comboBox1.Text].Price.ToString();
        }
    }
}

因为需要的时候能看懂所以就不加注释了

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 FirstCase
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        //初始话体检套餐
        List<A> list1 = new List<A>();
        //保存体检套餐中的项目
        List<A> list2 = new List<A>();
        //保存体检套餐和项目
        Dictionary<string, B> dic = new Dictionary<string, B>();
        private void Form1_Load(object sender, EventArgs e)
        {
            comboBox2.ValueMember = "name";
            A a1 = new A("小红","一个女孩子",10);
            A a2 = new A("小名", "一个男孩子", 20);
            A a3 = new A("小军", "一个好老师", 30);
            list1.Add(a1);
            list1.Add(a2);
            list1.Add(a3);
           
          
            list2.Add(a1);
            list2.Add(a2);
            B b = new B("入学体检",list2,0);
            b.SumPrice();
            dic.Add(b.Name,b);
            foreach (string item in dic.Keys)
            {
                comboBox1.Items.Add(item);
            }
           // comboBox2.DataSource = new BindingList<A>();
            //BindingSource bs = new BindingSource();
            //bs.DataSource = dic.Values;
            //comboBox1.DataSource = bs;
           // comboBox1.ValueMember = "name";
          
    

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            label4.Text = dic[comboBox1.Text].Name;
            label6.Text = dic[comboBox1.Text].Price.ToString();
            dataGridView1.DataSource = new BindingList<A>(dic[comboBox1.Text].list);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            B b = new B(textBox1.Text,new List<A>(),0);
            dic.Add(b.Name, b);       
           
            b.SumPrice();
            this.dataGridView1.DataSource = new BindingList<A>(dic[comboBox1.Text].list);
            comboBox1.Items.Add(b.Name);
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //MessageBox.Show(comboBox2.SelectedValue.ToString());
            //list2.Add(list1[comboBox2.SelectedIndex]);
            //B b = new B(textBox1.Text,list2,0);
            //dic.Add(textBox1.Text, b);
            //b.SumPrice();
            //dataGridView1.DataSource = new BindingList<A>(dic[comboBox1.Text].list);


            dic[comboBox1.Text].list.Add(list1[comboBox2.SelectedIndex]);
            dic[comboBox1.Text].SumPrice();
            dataGridView1.DataSource = new BindingList<A>(dic[comboBox1.Text].list);
            label4.Text = dic[comboBox1.Text].Name;
            label6.Text = dic[comboBox1.Text].Price.ToString();
        }

        private void button3_Click(object sender, EventArgs e)
        {
              dic[comboBox1.Text].list.Remove(list1[comboBox2.SelectedIndex]);
              dic[comboBox1.Text].SumPrice();
              dataGridView1.DataSource = new BindingList<A>(dic[comboBox1.Text].list);
              label4.Text = dic[comboBox1.Text].Name;
              label6.Text = dic[comboBox1.Text].Price.ToString();
        }
    }
}

有点小问题改进一下

原文地址:https://www.cnblogs.com/lcycn/p/7040752.html