
214
LESSON 17 Using EnUmErations and strUctUrEs
public string Email;
public string Phone;
public ContactMethod PreferredMethod;
}
Create a
Dictionary<string, Address> field to hold the address data.
1. Use code similar to the following code at the form’s class level (not inside any event
handler):
// Make a Dictionary to hold addresses.
private Dictionary<string, Address> Addresses =
new Dictionary<string, Address>();
Add code to initially select the
ComboBox’s None entry when the form loads.
1. Use code similar to the following:
// Make sure the ComboBox starts with an item selected.
private void Form1_Load(object sender, EventArgs e)
{
preferredMethodComboBox.SelectedIndex = 0;
}
Add code to the Add button that creates the new entry in the
Dictionary.
1. Use code similar to the following. Optionally you can clear the TextBoxes to get ready
for the next address.
// Add a new address.
private void addButton_Click(object sender, EventArgs e)
{
// Fill in a new Address structure.
Address newAddress;
newAddress.Name = nameTextBox.Text;
newAddress.Street = streetTextBox.Text;
newAddress.City = cityTextBox.Text;
newAddress.State = stateTextBox.Text;
newAddress.Zip = zipTextBox.Text;
newAddress.Email = emailTextBox.Text;
newAddress.Phone = phoneTextBox.Text;
newAddress.PreferredMethod =
(ContactMethod)preferredMethodComboBox.SelectedIndex;
// Add the name and address to the dictionary. ...