Hi,

Im trying to use a listBox to search some objects, it then adds the object to the listbox if it meets the condition. So far i have this.

foreach (Order ord in Form1.cutomerArray)
            {
                Order searchCust = (Order)Form1.cutomerArray[i];
                if (searchCust.FirstName.ToLower() == txtFirstName.Text.ToLower())//if match is found
                {
                    lstbSearch.Items.Add(searchCust);//add object to search box
                }
            }

This works fine but it just shows the form name and class name.

Is it possible to still add the object to the listBox but change what is shown.

I need to add the object as later i get the cutomer number from that object.

Thanks

Recommended Answers

All 3 Replies

>This works fine but it just shows the form name and class name.

Override ToString() method of Customer class.

public class Customer
{
    public int ID { get; set; }
    public string Name { get; set; }
    public override  string ToString() { return Name; }
}
List<Customer> cust = new List<Customer>()
            {
                new Customer(){ ID=1,Name="A"},
                new Customer(){ ID=1,Name="B"}
            };
            foreach (var t in cust)
                listBox1.Items.Add(t);

Thats great, it works.

One more thing.

Is it possible to display more than just the first name? Ive tried putting

return Name,secondname;

and a couple of other ways but nothing worked.

Do you have to create a variable or something and then return the variable string?

Thanks

Might have found it on google, will check later

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.