2 Replies - 1441 Views - Last Post: 02 August 2012 - 10:13 AM Rate Topic: -----

#1 Sandra1717  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 02-August 12

How to make userinput array of struct show in listbox and clear entry

Posted 02 August 2012 - 08:51 AM

I am new in c# and am rather confused about how make sure my array is working correctly in adding user input to the array and structure. I believe I have my array, structure, and adding entries correct, but who knows, maybe not. This is a homework assignment and the requirements are as follows:
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.


Is This A Good Question/Topic? 0
  • +

Replies To: How to make userinput array of struct show in listbox and clear entry

#2 mvcwannab  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 26-July 12

Re: How to make userinput array of struct show in listbox and clear entry

Posted 02 August 2012 - 09:32 AM

change the name within the foreach to foreach (Contact contactInformation), makes code easier to read. You're getting back an object, deal with that object, instead of trying to call it an index. ContactInfo.ToString will always return the same thing, and it's not the value that is selected, so that code will never execute. Change it to something similar to this:
bool bFound = false;
foreach (Contact contactInformation in contactInfo)
            {
                //if statement stating if array info = selected listbox item then display info in txtboxes per below request
                if (contactInformaition.ToString() == nameListBox.SelectedValue)
                {
                    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;
bFound = true;
                    break;
                }
            }
// executes after loop if the entry was not found.  By putting it in the else to the for, if the entry is not the first entry in list, else will always clear out information.  It's possible for that code to execute up to 19 times in a 20 element array.
if (!bFound)
{
                    //clear all textboxes
                    lastNameTextBox.Text = "";
                    firstNameTextBox.Text = "";
                    streetTextBox.Text = "";
                    cityTextBox.Text = "";
                    stateTextBox.Text = "";
                    zipCodeTextBox.Text = "";
}


Was This Post Helpful? 0
  • +
  • -

#3 Sandra1717  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 02-August 12

Re: How to make userinput array of struct show in listbox and clear entry

Posted 02 August 2012 - 10:13 AM

I just tried that fix, but it does not work. If I do exactly as you said then the information does not show in the text boxes like needed and there is a warning stating the comparison with nameListBox.SelectedValue does not work. If I add .ToString to nameListBox.SelectedValue)I donot get the warning but it adds the name or contact information over and over until the catch shows stating there have been 20 entries.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1