CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 58

Threaded View

  1. #35
    Join Date
    Jan 2010
    Posts
    32

    Re: Display highlighted data from Listbox in Textboxes

    How about this:

    Code:
    public struct Contacts
    {
                public string FirstName;
                public string LastName;
                public string Street;
                public string City;
                public string State;
                public string Zip;
    
    }           Contacts [] arrayOfContacts = new Contacts [20];
                int index = 0;
    
                private void btnExit_Click(object sender, EventArgs e)
                {
                    // Close program
                    this.Close();
                }
    
                private void btnAdd_Click(object sender, EventArgs e)
                {
                    // Declare variables
                    Contacts entry;
                    entry.FirstName = tbxFirstName.Text;
                    entry.LastName = tbxLastName.Text;
                    entry.Street = tbxStreet.Text;
                    entry.City = tbxCity.Text;
                    entry.State = tbxState.Text;
                    entry.Zip = tbxZip.Text;
    
                    // Add contact to array
                    if (index < 20)
                    {
                        arrayOfContacts[index++] = entry;                  
    
                    }
    
                    else
                    {
                        MessageBox.Show("The Address Book Can Hold a Maximum of 20 Contacts.", "Address Book Full");
                    }
    
                    
                    // Display Contacts (First and Last name only) in Listbox
                    lstContacts.Items.Add(entry.LastName + ", " + entry.FirstName);
    
                    // Return message when Array reaches maximum of 20 entries
    
                    // Clear Text Boxes
                    tbxFirstName.Text = "";
                    tbxLastName.Text = "";
                    tbxStreet.Text = "";
                    tbxCity.Text = "";
                    tbxState.Text = "";
                    tbxZip.Text = "";
    
                } 
    
           
        }
    }
    Ok, so that seems to work fine. I was trying to code the "Delete" button which will delete the entry in the array that corresponds to the name selected in the listbox. I tried to do this with a for loop seen below, but I am getting a lot of errors. Should I be doing this a different way? This works fine in another program I have that uses an array rather than an array of structures.

    Code:
    private void btnDelete_Click(object sender, EventArgs e)
                {
                    for (int k = 0; k < arrayOfContacts.Length; k++)
                
    
                    try
                    {
    
                        if (arrayOfContacts[k] = lstContacts.SelectedItem.ToString())
                        {
    
                            arrayOfContacts[k] = null;
    
                        }
    
                    }
    
                    catch
                    {
    
                    }
    
    
    
                }
    
                //Display Refreshed Records in ListBox
                lstContacts.Items.Clear();
    
                for (int j = 0; j < index; j++)
                {
    
                    if (arrayOfContacts[j] != null)
                    {
    
                        lstContacts.Items.Add(arrayOfContacts[j].ToString().Trim());
    
                    }
    Last edited by CarlMartin; January 25th, 2010 at 09:38 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured