2013-06-07 7 views
0

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



} 

이 클래스는이 클래스 배열 늘 목록 상자에 표시하는 이유에 관한 어떤 도움이 많이 appriciated 될 것이다

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

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(); 
    } 
} 
관련 문제