删除字典!
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 WindowsFormsApplication1
{
public partial class Form1 : Form
{
//所有项目啊
public List<HealthCheckItem> AllItem=new List<HealthCheckItem>();
public HealthCheckSet healthset;
//字典存套餐
public Dictionary<string,HealthCheckSet> setdic=new Dictionary<string,HealthCheckSet>();
public Form1()
{
InitializeComponent();
}
//loding
private void Form1_Load(object sender, EventArgs e)
{
Init();
//项目
this.cmbitem.DataSource = AllItem;
this.cmbitem.DisplayMember = "name";
//套餐
foreach(string key in setdic.Keys)
{
this.cmbset.Items.Add(key);
}
}
//初始化
public void Init()
{
HealthCheckItem hci = new HealthCheckItem("用于检查身高","身高",5);
HealthCheckItem hci1 = new HealthCheckItem("用于检查体重", "体重", 5);
HealthCheckItem hci2 = new HealthCheckItem("用于检查肝功能", "肝功能", 30);
HealthCheckItem hci3 = new HealthCheckItem("用于B超检查", "B超", 50);
HealthCheckItem hci4 = new HealthCheckItem("用于检查身高1", "身高adf", 5);
HealthCheckItem hci5 = new HealthCheckItem("用于检查体重1", "体重adf", 5);
HealthCheckItem hci6 = new HealthCheckItem("22222", "11", 30);
HealthCheckItem hci7 = new HealthCheckItem("33333B超检查", "B213", 50);
HealthCheckItem hci8= new HealthCheckItem("44444", "A", 5);
HealthCheckItem hci9 = new HealthCheckItem("444", "B", 5);
HealthCheckItem hci10 = new HealthCheckItem("44", "C", 30);
//HealthCheckItem hci33 = new HealthCheckItem("用于B5", "BD", 50);
//HealthCheckItem hci111 = new HealthCheckItem("7", "E", 5);
//HealthCheckItem hci1111 = new HealthCheckItem("64", "F", 5);
//HealthCheckItem hci2222 = new HealthCheckItem("6", "G", 30);
//HealthCheckItem hci3333 = new HealthCheckItem("用于B5", "H", 50);
//HealthCheckItem hci444 = new HealthCheckItem("用于检查4", "HH", 5);
//HealthCheckItem hci114 = new HealthCheckItem("用于3体重AC", "I", 5);
//HealthCheckItem hci2212 = new HealthCheckItem("用于2功能EE", "II", 30);
//HealthCheckItem hci3231 = new HealthCheckItem("用于1检查ADF", "BII", 50);
AllItem.Add(hci);
AllItem.Add(hci1);
AllItem.Add(hci2);
AllItem.Add(hci3);
AllItem.Add(hci4);
AllItem.Add(hci5);
AllItem.Add(hci6);
AllItem.Add(hci7);
AllItem.Add(hci8);
AllItem.Add(hci9);
AllItem.Add(hci10);
healthset = new HealthCheckSet();
healthset.Name = "入学套餐";
healthset.Items.Add(hci4);
healthset.Items.Add(hci5);
healthset.Items.Add(hci6);
healthset.Items.Add(hci7);
healthset.CalcPrice();
setdic.Add("入学套餐", healthset);
healthset = new HealthCheckSet();
healthset.Name = "普通套餐";
healthset.Items.Add(hci1);
healthset.Items.Add(hci2);
healthset.Items.Add(hci4);
healthset.Items.Add(hci5);
healthset.CalcPrice();
setdic.Add("普通套餐", healthset);
}
//刷新
public void Update(HealthCheckSet set)
{
this.dataGridView1.DataSource = new BindingList<HealthCheckItem>(set.Items);
set.CalcPrice();
}
private void groupBox1_Enter(object sender, EventArgs e)
{
}
//================新加套餐
private void btnAddSet_Click(object sender, EventArgs e)
{
healthset = new HealthCheckSet();
string name = this.txtsetname.Text.Trim();
healthset.Name = name;
setdic.Add(name,healthset);
foreach (string key in setdic.Keys)
{
this.cmbset.Items.Add(key);
}
}
//================删除
private void btndel_Click(object sender, EventArgs e)
{
string name = this.cmbitem.Text.ToString();
string setname = this.cmbset.Text.ToString();
foreach (HealthCheckItem item in AllItem)
{
if (item.name == name)
{
if (setdic[name].Items.Contains(item))
{
HealthCheckSet hs = setdic[name];
hs.Items.Remove(item);
Update(hs);
break;
}
else
{
MessageBox.Show("该套餐没有此项目");
}
break;
}
}
}
private void labname_Click(object sender, EventArgs e)
{
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
string name = this.cmbset.SelectedItem.ToString();
// foreach (string key in setdic.Keys)
// {
this.dataGridView1.DataSource = setdic[name];
// }
}
//=====================显示套餐
public void show()
{
}
private void cmbset_SelectedIndexChanged(object sender, EventArgs e)
{
string setName = cmbset.SelectedItem.ToString();
if (setdic.ContainsKey(setName))
{
HealthCheckSet hs = setdic[setName];
this.dataGridView1.DataSource = hs.Items;
this.labname.Text = setName;
this.labpri.Text = hs.Price + "";
}
}
//=====================添加检查项目
private void btnAdd_Click(object sender, EventArgs e)
{
//添加的检查项目
string name = cmbitem.Text.ToString();
//当前的套餐名
string setname = this.cmbset.Text.ToString();
HealthCheckSet hcs = setdic[setname];
//得到选中的项目
if (setdic[setname] != null)
{
foreach (HealthCheckItem item in AllItem)
{
if (item.name == name)
{
if (hcs.Items.Contains(item))
{
MessageBox.Show("该项目已经存在");
}
else
{
//this.btnAdd.Visible = true;
hcs.Items.Add(item);
hcs.CalcPrice();
Update(hcs);
break;
}
}
}
}
else
{
MessageBox.Show("没有该套餐");
}
}
private void labpri_Click(object sender, EventArgs e)
{
//string name = this.cmbset.Text.ToString();
//int money=setdic[name].CalcPrice;
}
private void cmbitem_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void cmbitem_SelectedIndexChanged(object sender, EventArgs e)
{
string name = cmbitem.Text.ToString();
//当前的套餐名
string setname = this.cmbset.Text.ToString();
//得到选中的项目
if (setname !="请选择")
{
foreach (HealthCheckItem item in AllItem)
{
if (item.name == name)
{
HealthCheckSet hcs = setdic[setname];
if (hcs.Items.Contains(item))
{
this.btnAdd.Enabled = false;
//MessageBox.Show("该项目已经存在");
}
else
{
this.btnAdd.Enabled = true;
hcs.Items.Add(item);
hcs.CalcPrice();
Update(hcs);
break;
}
}
}
}
else
{
return;
}
}
}
这是效果。如果添加的已经有了,则添加按纽显示不可用。现在的情况是一有添加过的,按纽永久是不可用的。删除的时候说if (setdic[name].Items.Contains(item))
{
HealthCheckSet hs = setdic[name];
hs.Items.Remove(item);
Update(hs);
break;
}给定关键字不在字典中。 没明白啥意思呢。
}
--------------------编程问答-------------------- 加断点跟踪一下就知道了 --------------------编程问答-------------------- 你说有人会看么、上来就一大堆
--------------------编程问答-------------------- 你只给出了FROM.CS的代码,前台DESIGN的代码没给,你的类HealthCheckItem没给出,虽然可以根据读代码能判断出来,但是明显你的代码给的不完整。还好有截图,不然估计难度会大些。有空帮你瞧瞧。 --------------------编程问答-------------------- 你调试一下吧。 这个问题应该好弄。 --------------------编程问答-------------------- 你的字典里存的是以setname为key的数据,你用name当然查不到了,要换成setname就好用了。
if (setdic[setname].Items.Contains(item))
{
HealthCheckSet hs = setdic[setname];
hs.Items.Remove(item);
Update(hs);
break;
}
补充:.NET技术 , C#