2012-08-06 36 views
0

我無法將List<>發送到文本框,我不確定問題所在。我已經檢查過,所以List實際上具有值,並且它從類中正確地轉移到這個塊中。我想在列表框中顯示列表

代碼:

public void Listtest(List<string> m_customers) 
    { 
     lstRegistry.Items.Clear(); 


     for (int index = 0; index == m_customers.Count; index++) 
     { 
      lstRegistry.Items.Add(m_customers[index]); 
     } 
    } 

正在發送List<>

 class CustomManager 
{ 
    //private List<Customer> m_customers; 
    List<string> m_customers = new List<string>();   

    public void CreateNewString(string adresslist, string emaillist, string phonelist, string namelist) 
    { 
     MainForm strIn = new MainForm(); 
     string newlist = string.Format("{0,-3} {1, -10} {2, -20} {3, -30}", namelist, phonelist, emaillist, adresslist);   
     m_customers.Add(newlist); //líst is created. 
     strIn.Listtest(m_customers);   
    } 
} 

我只是不能得到它的工作,我很堅持的其他類。 :/

感謝您的任何和所有幫助和想法!

//問候

回答

9

改變循環條件:index < m_customers.Count

編輯 你也可能想創建一個類這樣的數據:

class Person 
{ 
    public string Name {get; set;} 
    public string Address {get; set;} 
    public string Email {get; set;} 
} 

這樣就可以使一個人名單:List<Person>

2

Erno的回答守ld照顧你的問題,但我也建議你閱讀foreach。使用這將改變從您的代碼:

for (int index = 0; index < m_customers.Count; index++) 
    { 
     lstRegistry.Items.Add(m_customers[index]); 
    } 

foreach (string cust in m_customers) 
    { 
     lstRegistry.Items.Add(cust); 
    } 

這我認爲是更容易閱讀。