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 DictionaryDemo { public partial class Form1 : Form { Dictionary myDictionary = new Dictionary(); public Form1() { InitializeComponent(); myDictionary.Add("bat", "a flying mammal"); myDictionary.Add("cat", "a furry animal"); myDictionary.Add("ball", "a round object"); } private void button1_Click(object sender, EventArgs e) { string word = textBox1.Text; string def = textBox2.Text; bool found = myDictionary.ContainsKey(word); if (!found) myDictionary.Add(word, def); else richTextBox1.Text = "word already exists in the dictionary."; textBox2.Text = ""; } private void button2_Click(object sender, EventArgs e) { string word = textBox1.Text; string def; if (myDictionary.TryGetValue(word, out def)) richTextBox1.Text = word + ": " + def; else richTextBox1.Text = "word not found."; } private void button3_Click(object sender, EventArgs e) { string text = "DICTIONARY CONTENTS\n"; foreach(string word in myDictionary.Keys) { string def; myDictionary.TryGetValue(word, out def); text += word + ": " + def +"\n"; } richTextBox1.Text = text; } } }