Create an array of structs for an addressbook to hold up to 20 entries with first name, last name, street address, city, state, zipcode. Only the names should show in a listbox and the entries should be placed in separate text boxes. There should be an add button, delete button, and exit button. When the name in the list box is highlighted the information should display in the textboxes. I am completely confused on how to show the contact information in the textbox, and how to clear the array entry with the delete button. Please note when I run my project I do not get any errors, but it does not function properly. It does not show the contact information in the textboxes and it does not clear the array entry.
Thank you in advance for any help provided.
{
public partial class unit10Form : Form
{
//Declare variables
int itemCountInteger;
//Create Contact Structure
public struct Contact
{
public string lastNameString;
public string firstNameString;
public string streetAddressString;
public string cityString;
public string stateString;
public string zipCodeString;
}
//create array for contacts
Contact[] contactInfo = new Contact[20];
public unit10Form()
{
InitializeComponent();
}
private void unit10Form_Load(object sender, EventArgs e)
{
}
private void addAcceptButton_Click(object sender, EventArgs e)
{
try
{
foreach(Contact contactInfoIndex in contactInfo)
//Adding user input / new contact to array
contactInfo[itemCountInteger].lastNameString = lastNameTextBox.Text;
contactInfo[itemCountInteger].firstNameString = firstNameTextBox.Text;
contactInfo[itemCountInteger].streetAddressString = streetTextBox.Text;
contactInfo[itemCountInteger].cityString = cityTextBox.Text;
contactInfo[itemCountInteger].stateString = stateTextBox.Text;
contactInfo[itemCountInteger].zipCodeString = zipCodeTextBox.Text;
//declare names as string variable and = to last and first name
string names = contactInfo[itemCountInteger].lastNameString + ", " + contactInfo[
itemCountInteger].firstNameString;
//using the names variable add first and last name to listbox
nameListBox.Items.Add(names);
//increment itemcountinteger everytime contact is added
itemCountInteger++;
//select item just added in listbox
nameListBox.SelectedItem = names;
}
catch
{
MessageBox.Show("Contacts are limited to 20. Please delete some contacts prior to adding more.");
}
}
private void deleteButton_Click(object sender, EventArgs e)
{
//remove name from listbox
nameListBox.Items.RemoveAt(nameListBox.SelectedIndex);
}
private void exitButton_Click(object sender, EventArgs e)
{
//exit program
this.Close();
}
private void nameListBox_SelectedIndexChanged(object sender, EventArgs e)
{
//foreach loop to run through array list
foreach (Contact contactInformationIndex in contactInfo)
{
//if statement stating if array info = selected listbox item then display info in txtboxes per below request
if (contactInfo.ToString() == nameListBox.SelectedIndex.ToString())
{
lastNameTextBox.Text = contactInfo[itemCountInteger].lastNameString;
firstNameTextBox.Text = contactInfo[itemCountInteger].firstNameString;
streetTextBox.Text = contactInfo[itemCountInteger].streetAddressString;
cityTextBox.Text = contactInfo[itemCountInteger].cityString;
stateTextBox.Text = contactInfo[itemCountInteger].stateString;
zipCodeTextBox.Text = contactInfo[itemCountInteger].zipCodeString;
break;
}
else
{
//clear all textboxes
lastNameTextBox.Text = "";
firstNameTextBox.Text = "";
streetTextBox.Text = "";
cityTextBox.Text = "";
stateTextBox.Text = "";
zipCodeTextBox.Text = "";
}
}
}
}
}
This post has been edited by jimblumberg: 02 August 2012 - 08:56 AM
Reason for edit:: Added missing Code Tags, Please learn to use them.

New Topic/Question
Reply



MultiQuote



|