使用HashTable与Dictionary 感觉很好用!实现了一个CheckListBox的选择记忆功能。窗体界面:一个CheckLIstBox,两个CombBox。

流程描述:1.选择CheckLIstBox1中的一个图层1名称,右面的CombBox显示图层1包含的字段。为CombBox选择项。

2.选择CheckLIstBox1中的另一个图层2名称,右面的CombBox显示图层2包含的字段。为CombBox选择项。

3.来回选择CheckLIstBox1,可以记忆CombBox中所作的选择。

HashTable与Dictionary_参考文献 HashTable与Dictionary_C#_02


HashTable与Dictionary_Dictionary_03HashTable与Dictionary_Dictionary_04View Code


1 class PrepareDrillLayerInfo
2     {
3        public string m_layerName;
4        public int m_bottomFieldIndex=-1;
5        public string m_bottomField;
6        public int m_ThicknessFieldIndex=-1;
7        public string m_ThicknessField;
8     }


窗体类私有变量:


HashTable与Dictionary_Dictionary_03HashTable与Dictionary_Dictionary_04View Code


1  Dictionary<int, PrepareDrillLayerInfo> myDictionary = new Dictionary<int, PrepareDrillLayerInfo>();


相关的几个事件:

FormLoad事件负责循环加载图层,同时创建字典Dictionary。


HashTable与Dictionary_Dictionary_03HashTable与Dictionary_Dictionary_04View Code


1              chklistDrillLayer.Items.Add(layer.Name);
2              PrepareDrillLayerInfo prpareDrillLyrInfo = new PrepareDrillLayerInfo();
3              myDictionary.Add(i, prpareDrillLyrInfo);


CheckLIstBox1的SelectedIndexChanged事件方法,负责更新字段,同时判断字典中当前所选图层的信息;

还有介绍CombBox的SelectedIndexChanged事件,负责更新当前所选图层在字典中的信息。