user1501127
user1501127

Reputation: 865

I want to display a list in an listbox

I am having trouble sending a List<> to a textbox and i am not sure what the trouble is. I have checked so the List actually has values in it and that it gets properly transfered from the class into this block.

The code:

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

The other class that is sending the 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);           
    }
}  

I just cant get it to work and I am really stuck. :/

Thanks for any and all help and ideas!!!

//Regards

Upvotes: 0

Views: 94

Answers (2)

Abe Miessler
Abe Miessler

Reputation: 85036

Erno's answer should take care of your issue, but I would also recommend that you read up on foreach. Using this would change your code from:

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

to

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

Which I would argue is easier to read.

Upvotes: 2

Emond
Emond

Reputation: 50672

Change the loop condition to: index < m_customers.Count

Edit Also you might want to create a class for this data:

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

So you can make a list of Persons: List<Person>

Upvotes: 9

Related Questions