Hello all. I am new to programming. I have been working on this address book program for the past we ek and can not seem to get it to work out. I know I am pretty close. This is my final exam which is due tomorrow. I have been pulling my hair out trying to get this thing to work, and I need some help. The program must hold 20 entries in an array. It must contain the entries in a list box only showing the last and first name in that order, and it must be alphabetized. The program also must be able to show the address entries in text boxes when a user highlights the name in the list box. The program must also be able to delete an address entry from the list box and array (i know the array can not be deleted but the elements of the array need to be cleared). The program must than be able to add a new entry for the one deleted. Now I have gotten pretty far with this, I created a struct of array. I am able to add these to the list box. I am close to being able to store the entries back in the list box but I am missing something and I can not figure it out. I also am able to delete the entry from the listbox but I can not seem to figure out how to delete the array elements of the selected list box item. Any help to point me in the right direction would be very much appreciated. Here is my code:
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 final_project_addressbook_cs
{
public partial class final_project_addressbook_cs : Form
{
public final_project_addressbook_cs()
{
InitializeComponent();
this.addressListBox.SelectedIndexChanged += new EventHandler(addressListBox_SelectedIndexChanged_1);
}
public struct AddressBook
{
public string firstNameString;
public string lastNameString;
public string streetAddressString;
public string stateString;
public string cityString;
public int zipCodeInteger;
}
int index = 0;
AddressBook[] Addresses = new AddressBook[20];
private void addButton_Click(object sender, EventArgs e)
{
try
{
Addresses[index].firstNameString = firstNameTextBox.Text;
Addresses[index].lastNameString = lastNameTextBox.Text;
Addresses[index].streetAddressString = streetAddressTextBox.Text;
Addresses[index].stateString = stateTextBox.Text;
Addresses[index].cityString = cityTextBox.Text;
Addresses[index].zipCodeInteger = int.Parse(zipCodeTextBox.Text);
addressListBox.Items.Add(Addresses[index].lastNameString + ", " + Addresses[index].firstNameString);
index++;
clearForNextEntry();
addressListBox.Sorted = true;
}
catch
{
MessageBox.Show("Only 20 contacts allowed.", "Data Entry Error");
}
}
private void clearForNextEntry()
{
firstNameTextBox.Clear();
lastNameTextBox.Clear();
streetAddressTextBox.Clear();
stateTextBox.Clear();
cityTextBox.Clear();
zipCodeTextBox.Clear();
}
private void deleteButton_Click(object sender, EventArgs e)
{
if (addressListBox.SelectedIndex > -1)
addressListBox.Items.RemoveAt(addressListBox.SelectedIndex);
addressListBox.SelectedItem = "Deleted Contact";
}
private void addressListBox_SelectedIndexChanged_1(object sender, EventArgs e)
{
if (addressListBox.SelectedItem != null)
{
for (int index = 0; index < 20; index++ )
{
if (addressListBox.SelectedItem.ToString() == Addresses[index].ToString())
{
firstNameTextBox.Text = Addresses[index].firstNameString;
lastNameTextBox.Text = Addresses[index].lastNameString;
zipCodeTextBox.Text = Addresses[index].zipCodeInteger.ToString();
streetAddressTextBox.Text = Addresses[index].streetAddressString;
stateTextBox.Text = Addresses[index].stateString;
cityTextBox.Text = Addresses[index].cityString;
break;
}
else
{
MessageBox.Show("Error finding address", "Data error");
}
}
}
}
}
}