Note that there are some explanatory texts on larger screens.

plurals
  1. POListBox doesnt show any results
    primarykey
    data
    text
    <p>I'm trying to add an array of strings that holds information about people to a ListBox but i cant get it to show anything and i really dont know why.</p> <p>This is the first class that is called when the button to add contacts to the list is clicked.</p> <pre><code>public partial class MainForm : Form { private ContactManager m_contacts; public MainForm() { InitializeComponent(); m_contacts = new ContactManager(); InitializeGUI(); } private void InitializeGUI() { txtFirstName.Text = string.Empty; txtLastName.Text = string.Empty; txtStreet.Text = string.Empty; txtCity.Text = string.Empty; txtZipCode.Text = string.Empty; cmbCountry.Items.AddRange(Enum.GetNames(typeof(Countries))); cmbCountry.DropDownStyle = ComboBoxStyle.DropDownList; cmbCountry.SelectedIndex = (int)Countries.Sverige; UpdateGUI(); } private bool ReadInput(out Contact contact) { // skapar ett lokalt objekt av Contact-klassen contact = new Contact(); // Lägger in ReadAdress till ett objekt i klassen Adress. Address adr = ReadAddress(); contact.AddressData = adr; // skickar adress till contact-objekt //bool readNameOK = ReadName(); // ReadName är OK så skickas det till AddContact. if (ReadName()) { m_contacts.AddContact(contact); } return ReadName(); } // ReadInput private bool ReadName() { Contact contact = new Contact(); contact.FirstName = txtFirstName.Text; contact.LastName = txtLastName.Text; bool firstname = false; bool lastname = false; if (!InputUtility.ValidateString(contact.FirstName)) { MessageBox.Show("You must enter a first name with atleast one character (not a blank)", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error); txtFirstName.Focus(); txtFirstName.Text = " "; txtFirstName.SelectAll(); firstname = false; } else if (!InputUtility.ValidateString(contact.LastName)) { MessageBox.Show("You must enter a last name with atleast one character (not a blank)", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error); txtLastName.Focus(); txtLastName.Text = " "; txtLastName.SelectAll(); lastname = false; } return firstname &amp;&amp; lastname; } private Address ReadAddress() { Address address = new Address(); address.Street = txtStreet.Text; address.City = txtCity.Text; address.ZipCode = txtZipCode.Text; address.Country = (Countries)cmbCountry.SelectedIndex; return address; } private void button1_Click(object sender, EventArgs e) { Contact contact; if (ReadInput(out contact)) { UpdateGUI(); } } private void UpdateGUI() { lstContacts.Items.Clear(); lstContacts.Items.AddRange(m_contacts.GetContactsInfo()); lblCount.Text = m_contacts.Count().ToString(); } private void lstContacts_SelectedIndexChanged(object sender, EventArgs e) { UpdateContactInfoFromRegistry(); } private void UpdateContactInfoFromRegistry() { Contact contact = m_contacts.GetContact(lstContacts.SelectedIndex); cmbCountry.SelectedIndex = (int)contact.AddressData.Country; txtFirstName.Text = contact.FirstName; txtLastName.Text = contact.LastName; txtCity.Text = contact.AddressData.City; txtStreet.Text = contact.AddressData.Street; txtZipCode.Text = contact.AddressData.ZipCode; } } </code></pre> <p>This class then calls this class </p> <pre><code>public class ContactManager { private List&lt;Contact&gt; m_contactRegistry; public ContactManager() { m_contactRegistry = new List&lt;Contact&gt;(); } public int Count() { int count = m_contactRegistry.Count(); return count; } public bool CheckIndex(int index) { if (index &gt;= 0 &amp;&amp; index &lt; m_contactRegistry.Count()) return true; else return false; } public bool AddContact(string firstName, string lastName, Address addressIn) { Contact contactObj = new Contact(); bool result = false; if (!result) { contactObj.FirstName = firstName; contactObj.LastName = lastName; contactObj.AddressData = addressIn; m_contactRegistry.Add(contactObj); result = true; } return result; } public bool AddContact(Contact contactIn) { if (contactIn == null) { return false; } else { m_contactRegistry.Add(contactIn); return true; } } public bool changeContact(Contact contactIn, int index) { if (CheckIndex(index)) { Contact contact = (Contact)m_contactRegistry[index]; //contact.ToString = contactIn; return true; } else return false; } public bool DeleteContact(int index) { if (CheckIndex(index)) { m_contactRegistry.RemoveAt(index); return true; } else return false; } public Contact GetContact(int index) { if (!CheckIndex(index)) return null; else return m_contactRegistry[index]; } public string[] GetContactsInfo() { string[] strInfoStrings = new string[m_contactRegistry.Count]; int i = 0; foreach (Contact contactObj in m_contactRegistry) { strInfoStrings[i++] = contactObj.ToString(); } return strInfoStrings; } } </code></pre> <p>Any help regarding why the arrays wont show up in the listbox would be much appriciated, thanks.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload