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");
                    }

                }



            }
        }
    }
}

Well I turned it in, and it was not finished. Oh well. I still could use some help because it is really aggrivating me that I can not figure this out. Come on mr or mrs experts help a fellow learning programmer out. Not asking for the answer spelled out for me that is not what I want. Just maybe some hints or point out code that is not right or anything.

The ListBox's Item collection is of the 'object' type. The value displayed in the listbox is the result of the ToString() method of the object. If you override the ToString() method of your struct you can determine what is returned, and thus, what is displayed in the ListBox if you use the Address struct in the Item collection.
Rather than add a new string to the ListBox for each address, you can add an instance of your struct, which holds all the information you have entered into it. You can then unbox the SelectedItem of the ListBox to access all the data stored in the Address object:

public partial class Form1 : Form
    {
        Address[] AddressBook = new Address[2];
        public Form1()
        {
            InitializeComponent();

            //create some test addresses
            Address add = new Address();
            add.FirstName = "Test";
            add.LastName = "Address";
            add.Index = 0;
            AddressBook[0] = add;

            add = new Address();
            add.FirstName = "Fake";
            add.LastName = "Address";
            add.Index = 1;
            AddressBook[1] = add;

            //add the addresses to the ListBox
            foreach(Address address in AddressBook)
            {
                addressListBox.Items.Add(address);
            }
        }

        private void addressListBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            //convert selected item back into Address type
            Address selectedAddress = (Address)addressListBox.SelectedItem;
   
            //you can now access the members of your selected address
            MessageBox.Show(selectedAddress.Index.ToString());
        }
    }

    public struct Address
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public int Index { get; set; }

        //set the value that is returned when ToString is called on your struct objects
        public override string ToString()
        {
            return LastName + ", " + FirstName;
        }
    }

How you implement the above is up to you, you can either use a search function to locate the selected item in your array:
could override the Equals method of your struct and use it to compare each address with the selected address to find a match. You could keep the Index property of your Address objects synced with their index in the array and use that to find/delete their matching item in the array.

Since you have now handed it in and are no longer bound by the requirements of the assignment, i would suggest you look into the List<T> class. It offers much more functionality; it doesn't require a fixed size when created and has useful methods like Find and RemoveAt which would help you out here :)

Also, a small note. It isn't critical, but i noticed you called your struct AddressBook. Since each instance of the struct represents a single address it would be more appropriate to call it Address and name the collection of addresses AddressBook as i have in my example above.

EDIT
Another note: It doesn't matter so much with a simple struct like yours, but its a good habit to get into; rather than having public variables like you have used, you should have a private variable which is exposed via a public property. The struct in my example uses the shorthand syntax for this:

public string FirstName { get; set; }

is the equivalent of

private string _FirstName;
public string FirstName
{
    get { return _FirstName; }
    set { _FirstName = value; }
}

It is correct OO design to handle external access through properties as you can validate input and output and also you can change the type of the internal member without external objects needing to be updated. This all falls under the principle of Encapsulation :)

Hey Ryshad thank you for your input. I have taken your advice and looked into using the list<array>. Wow it is so much easier to use than what I have used. It makes it easier to add and delete items in the list. Thanks again I appreciate your input and I am sure I will be back for more questions since I am still learning.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.