0

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

2
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by fredted40x
0

>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);
0

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

Edited by fredted40x: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.