赞
踩
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 Dictionary_demo { public partial class Form1 : Form { Dictionary<string, string> dictDemo = new Dictionary<string, string>(); public Form1() { InitializeComponent(); } private void add_dict_Click(object sender, EventArgs e) { this.textBoxInfo.AppendText("------------- Add item -------------\r\n"); if (dictDemo.ContainsKey(textKey.Text)) { this.textBoxInfo.AppendText("Key : " + textKey.Text + " already exits!\r\n"); } else { dictDemo.Add(textKey.Text, textValue.Text); this.textBoxInfo.AppendText("Add Key : " + textKey.Text + " \r\n"); this.textBoxInfo.AppendText("Add Value : " + textValue.Text + " \r\n"); } } private void btnLogClean_Click(object sender, EventArgs e) { this.textBoxInfo.Clear(); } private void btnFindFromKey_Click(object sender, EventArgs e) { this.textBoxInfo.AppendText("------------- find item with Key -------------\r\n"); textValue.Text = ""; if (dictDemo.ContainsKey(textKey.Text)) { string value = ""; this.textBoxInfo.AppendText("Key : " + textKey.Text + " exits!\r\n"); if (dictDemo.TryGetValue(textKey.Text, out value)) { textValue.Text = value; this.textBoxInfo.AppendText("Find : " + "Key = " + textKey.Text + "Value = " + value + "\r\n"); } else { this.textBoxInfo.AppendText("Key : " + textKey.Text + " find error!\r\n"); } } else { this.textBoxInfo.AppendText("Key : " + textKey.Text + " not exits!\r\n"); } } private void btnFindFromValue_Click(object sender, EventArgs e) { this.textBoxInfo.AppendText("------------- find item with value -------------\r\n"); foreach (KeyValuePair<string, string> kvp in dictDemo) { if (kvp.Value.Equals(textValue.Text)) { this.textBoxInfo.AppendText("key = " + kvp.Key + " Value = " + kvp.Value + " \r\n"); } } this.textBoxInfo.AppendText("------------- print end -------------\r\n"); } private void btnDictPrint_Click(object sender, EventArgs e) { this.textBoxInfo.AppendText("------------- print start -------------\r\n"); foreach (KeyValuePair<string, string> kvp in dictDemo) { this.textBoxInfo.AppendText("key = " + kvp.Key + " Value = " + kvp.Value + " \r\n"); } this.textBoxInfo.AppendText("------------- print end -------------\r\n"); } private void btnDictDelete_Click(object sender, EventArgs e) { this.textBoxInfo.AppendText("------------- remove item -------------\r\n"); dictDemo.Remove(textKey.Text); this.textBoxInfo.AppendText("Remove itme : Key = " + textKey.Text + " \r\n"); } } }
Dictionary<string, string> dictDemo = new Dictionary<string, string>();
dictDemo.Add(textKey.Text, textValue.Text);
dictDemo.Remove(textKey.Text);
dictDemo.clear();
if (dictDemo.ContainsKey(textKey.Text))
string value = dictDemo[textKey.Text];
foreach (KeyValuePair<string, string> kvp in dictDemo)
{
if (kvp.Value.Equals(textValue.Text))
{
this.textBoxInfo.AppendText("key = " + kvp.Key + " Value = " + kvp.Value + " \r\n");
}
}
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。