ListBox不显示任何结果

时间:2013-06-07 14:10:46

标签: c# listbox

我正在尝试添加一个字符串数组,其中包含有关人员的信息到ListBox,但我无法让它显示任何内容,我真的不知道为什么。

这是在单击按钮添加联系人按钮时调用的第一个类。

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 && 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;
    }



}

然后该类调用此类

public class ContactManager
{
    private List<Contact> m_contactRegistry;

    public ContactManager()
    {
        m_contactRegistry = new List<Contact>();
    }

    public int Count()
    {
        int count = m_contactRegistry.Count();

        return count;
    }

    public bool CheckIndex(int index)
    {
        if (index >= 0 && index < 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;
    }
}

关于为什么阵列不会出现在列表框中的任何帮助都会非常有用,谢谢。

1 个答案:

答案 0 :(得分:1)

您的ReadName()始终返回false,因为它永远不会添加联系人。

编辑: 更清晰的代码

private Contact ReadInput()
{
    Contact contact = new Contact();
    contact.FirstName = txtFirstName.Text;
    contact.LastName = txtLastName.Text;
    contact.AddressData = new Address
        {
            Street = txtStreet.Text,
            City = txtCity.Text,
            ZipCode = txtZipCode.Text,
            Country = (Countries) cmbCountry.SelectedIndex
        };
    return contact;
}

private bool ValidateContact(Contact contact)
{
    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();
        return 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();
        return false;
    }
    return true;
}

private void button1_Click( object sender, EventArgs e )
{
    Contact contact = ReadInput();
    if ( ValidateContact( contact ) )
    {
        m_contacts.AddContact(contact);
        UpdateGUI();
    }
}