如有需要源代码的请到下载:http://d.download.csdn.net/down/598122/xianshuang
应用命名空间:using System.Collections;
介绍属性与方法:
//Hashtable属性
//Count---------获取哈希表中包含的键值对的数量
//Keys----------返回ICollection,它包含哈希表中的键
//Values--------返回ICollection接口,包含哈希表中的值
//Hashtable方法
//Add-----------该方法将带有指定键和值的元素添加到哈希表中
//GetEnumerator-该方法返回IDictionaryEnumerator,它可以遍历哈希表
//Remove--------该方法将带有指定键的元素从哈希表中移除
通过键值添加到Hashtable数据,然后可以通过键来查找到Hashtable中的值,而且还可以移除
以上为例图,加入键与值,还可能通过键找到值
在添加中写:
try
{
_hashtable.Add(txtKey.Text, txtValue.Text);
}
catch (ArgumentException)
{
MessageBox.Show("不能添加重复的键", "错误", System.Windows.Forms.MessageBoxButtons.OK, System.Windows.Forms.MessageBoxIcon.Information);
return; //有重复的键值给用户提示,插入动作终止
}
catch (Exception ex)
{
MessageBox.Show("产生系统错误");
Application.Exit();
}
lstAddedKey.Items.Add(txtKey.Text);
然后你在已添加键中点选择某键即可找到对应值:代码如下:
private void lstAddedKey_SelectedIndexChanged(object sender, EventArgs e)
{
string selectedKey = lstAddedKey.SelectedItem.ToString();
string selectedValue = (string)_hashtable[selectedKey];
txtSelectedValue.Text = selectedValue;
}
学习了,记下了,大家有什么建议提出来呀!共同进步