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; using System.Collections; namespace UseHashtable { public partial class Form1 : Form { Hashtable hashtable = new Hashtable(); ICollection keys, values; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { // Populate hashtable hashtable.Add("1111 1111".Replace(" ", ""), "Ms. Dorothy Lesley Malone"); hashtable.Add("1111 1112".Replace(" ", ""), "Mr. George Alfred Snodgrass"); hashtable.Add("1111 1113".Replace(" ", ""), "Mr. Sammy Davis Junior"); hashtable.Add("1111 1114".Replace(" ", ""), "Mrs. Harriett Miriam Lowly"); hashtable.Add("1111 1115".Replace(" ", ""), "Ms. Julie Susan Rigg"); hashtable.Add("1111 1116".Replace(" ", ""), "Mr. Elliott J. Ness"); // get keys keys = hashtable.Keys; // get values values = hashtable.Values; } private void nameButton_Click(object sender, EventArgs e) { foreach (DictionaryEntry entry in hashtable) { if (entry.Value.ToString().ToUpper().Replace(" ", "") == nameTextBox.Text.ToString().ToUpper().Replace(" ", "")) { numberTextBox.Text = entry.Key.ToString(); } } } private void numberButton_Click(object sender, EventArgs e) { try { nameTextBox.Text = hashtable[numberTextBox.Text.ToString()].ToString(); } catch (Exception ex) { } } } }