I need help with my final project, I think I have the basic structures set up already. I'm a little lost on loops, do I need one to populate the list? and one to go through the list to display the information back in the text boxes? Also how do I go about displaying the information back? I know I have a good bit to go but if I could just get pointed in the right direction hopefully everything will fall into place. It says to use arrays to store the addresses, but I am allowed to use a list which I think is easier. Here is the project:

Write a Windows Application that maintains an address book. This address book should hold up to 20 entries. You must store your data with arrays. Each address entry will have the following:
• Last Name, First Name
• Street Address
• City
• State
• Zip Code
Your program must have the following functionality:
• Display the address book (names only in alphabetical order by last name)
• Display all information about an individual entry (allow the user to select this)
• Add address entry
• Delete address entry

Here is what I have so far:

public partial class frmunit10 : Form
    {
        public struct addr
        {
            public string First;
            public string Last;
            public string Street;
            public string City;
            public string State;
            public int Zip;
        }
        public frmunit10()
        {
            InitializeComponent();
        }
        List<addr> addresses = new List<addr>();
        private void btnadd_Click(object sender, EventArgs e)
        {
            try
            {
                addr a1 = new addr();
                {
                    a1.First = txtfirst.Text;
                    a1.Last = txtlast.Text;
                    a1.Street = txtstreet.Text;
                    a1.City = txtcity.Text;
                    a1.State = txtstate.Text;
                    a1.Zip = int.Parse(txtzip.Text);
                }

                addresses.Add(a1);
                
                
                    lbone.Items.Add(a1.Last + ", " + a1.First);
                
                addresses.Clear();


                
            }
            catch
            {
            }
        }

        private void btndelete_Click(object sender, EventArgs e)
        {
            
            lbone.Items.Remove(lbone.SelectedItem);
        }

        private void btnexit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void lbone_SelectedIndexChanged(object sender, EventArgs e)
        {
            
        }
    }
}

Should I have a1 - a20 as addresses in the list seperate from the add button? then use a for each loop to add the addresses to the list box?

Well I figured out how to display the info back into the text boxes but, how can I delete an address from the list with the delete button?

There is a coupe of ways with List.

addresses.Remove(put the object in here);

addresses.RemoveAt(put the index number here);

addresses.RemoveRange(starting index number here, amount of items to remove here);

for removing by object, index, or a range.

ok, how do I know what the index will be if it is deleting the addresses selected in the listbox? it could be any one of the addresses. I tried this:

private void btndelete_Click(object sender, EventArgs e)
        {
            //delete lastname, firstname from list box
            lbone.Items.Remove(lbone.SelectedItem);
            //delete selected address from list
            addresses.RemoveAt(lbone.SelectedIndex);
        }

but it throws an exception of index out of range. Do I have to create something to search the list for the specific address?

@Mack
put a break point, check to see what items are in the List (if any). Just step through it and click the + signs to expand the list

I put a break point at the bottom of the add buttons event handler, the data was added to a1 but not to addresses. I even took out the addresses.Clear(); to see if that was clearing the list but, it still didn't get added to the list.

Nevermind the last post I found the addresses list in the debugger and I found the problem. I had to reverse the order of the delete button so that it would delete the index referenced, then delete the entry from lbone. Does this look like it satisfies the requirements of the assigment?

//create structure to hold list variables
        public struct addr
        {
            //declare variables
            public string First;
            public string Last;
            public string Street;
            public string City;
            public string State;
            public string Zip;
        }
        public frmunit10()
        {
            InitializeComponent();
        }
        //create list to hold addresses
        List<addr> addresses = new List<addr>();
        private void btnadd_Click(object sender, EventArgs e)
        {
            try
            {
                if (txtfirst.Text == "" || txtlast.Text == "")
                {
                    MessageBox.Show("Please enter at least a first and last name!");
                }
                else
                {
                    addr a1 = new addr();
                    {
                        a1.First = txtfirst.Text;
                        a1.Last = txtlast.Text;
                        a1.Street = txtstreet.Text;
                        a1.City = txtcity.Text;
                        a1.State = txtstate.Text;
                        a1.Zip = txtzip.Text;
                    }
                    //add data from text boxes to list
                    addresses.Add(a1);
                    //add lastname, firstname to list box
                    lbone.Items.Add(a1.Last + ", " + a1.First);

                    //clear text boxes
                    txtfirst.Text = "";
                    txtlast.Text = "";
                    txtstreet.Text = "";
                    txtcity.Text = "";
                    txtstate.Text = "";
                    txtzip.Text = "";
                    //return focus to txtfirst
                    txtfirst.Focus();
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(string.Format(" Exception caught: {0}", ex.Message));
            }
        }

        private void btndelete_Click(object sender, EventArgs e)
        {
            if (lbone.SelectedIndex >= 0)
            {
            //delete selected address from list
            addresses.RemoveAt(lbone.SelectedIndex);
            //delete lastname, firstname from list box
            lbone.Items.Remove(lbone.SelectedItem);
            }
            else
            {
                MessageBox.Show("Please Select a contact to delete!");
            }
        }

        private void btnexit_Click(object sender, EventArgs e)
        {
            //exit the program
            this.Close();
        }

        private void lbone_SelectedIndexChanged(object sender, EventArgs e)
        {
            //use if statement to display selected address in text boxes
            if (lbone.SelectedIndex >= 0)
            {
                addr objCont = (addr)addresses[lbone.SelectedIndex];

                txtfirst.Text = objCont.First;
                txtlast.Text = objCont.Last;
                txtstreet.Text = objCont.Street;
                txtcity.Text = objCont.City;
                txtstate.Text = objCont.State;
                txtzip.Text = objCont.Zip;
            }
        }
    }
}

Another thing I wanted to ask was is there another way to display the list back into the text boxes? when I change the variable Zip to int and parse it from the text box it still throws a cannot convert int to string exception in the selected index event handler. Or maybe there there is a another way around that?

Found a new problem, The list box is displaying the names in alphabetical order by last name, but when I tried to sort the list itself with addresses.Sort(); it throws an error "failed to compare two elements in the array" when I add a second address. Is there a way to sort the list by certain elements? like by last name?

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.