2

I have a custom class

class RouteStop
{
    public int number;
    public string location;
    public string street;
    public string city;
    public string state;
    public string zip;

    public RouteStop(int INnumber, string INlocation, string INstreet, string INcity, string INstate, string INzip)
    {
        this.number = INnumber;
        this.location = INlocation;
        this.street = INstreet;
        this.city = INcity;
        this.state = INstate;
        this.zip = INzip;
    }
}

Then I have a list where I store RouteStop items

private List<RouteStop> routeStops = new List<RouteStop>();

What I am trying to archive is to load all objects from list into a listbox. So far it does it's job but instead of actual address it just writes object name into a list such like shown below enter image description here

How can I make it shows let's say number + location + street + city instead of object name?

Also in future I will need to add OnSelect event to open a new window to edit each object's data. How would I pass information about which item is selected?

Added: Thank you very much everyone. Each answer helped. So what I did so far is changed data source to the list, overwrote ToString method to display full address in list, added new item to RouteStop with unique id and set the DisplayMember to the uniqe id so I can access selected item in future by id as well.

Thank you very much once again

4 Answers 4

8

There are several options,

  • override ToString(), but that has effect everywhere. Maybe useful.
  • insert formatted strings and then find back the object by index
  • use DataSource, DisplayMember and ValueMember - you'll need a pseudo property for number + location + street + city and use it as the Displaymember
  • handle the ListBox Format event

That last one may be the most practical. The ListBox.Items would still be RouteStops and you can create the string as you please.

The innards of the Format event would look like:

RouteStop rs = e.Item as RouteStop;
string s = ... // use rs to create a nice string 
e.Value = s;

To use it in a "double click" event later just do this, where url is just a property of RouteStop.

private void listBox1_MouseDoubleClick(object sender, MouseEventArgs e)
{
    Process.Start("iexplore.exe",((RouteStop)listBox1.SelectedItem).url);
}
Sign up to request clarification or add additional context in comments.

5 Comments

So if I changed something using the listbox thing it should be changed in the list itself since they are bonded. correct?
@jm2, yes. At most you need a ListBox.Invalidate(). Most of the other options would require a Re-Bind or Re-Fill.
Okay. Regarding my other question. When I selected an item I get the full address which is outputted by overwritten ToString method. How do I access selected object again?
No, SelectedItem shoud be a RouteStop.
Somehow it outputs address (from ToString method) instead of object itself. Double checking
0

If you are setting the listBox DataSource, set then DataMember to whatever property of RouteStop and it will be shown.

If not, override ToString() (any string returned there will be shown) and don't forget to set listBox1.FormattingEnabled = false;

1 Comment

Thanks :) Overwriting ToString method now shows full address as needed.
0

ListControl.DisplayMember is the property you are looking for.

listBox1.DataSource = routeStops;
listBox1.DisplayMember = "location";
listBox1.ValueMember = "number";

If you wanted to show the entire address I would create a readonly property called displayAddress or something that is in the format you would like to display.

For your second question. The Items collection will have your actual objects in it, so you can retrieve them with SelectedItem, or alternatively you could use SelectedValue and create a new object from the number property value returned.

2 Comments

This will only show the location, not number + location + street + city
That's why I mentioned creating a display property that would have the values in the format you would like, and then binding DisplayMember to that.
-1

When adding a new ListItem to a ListBox in WinForms, you must pass in a string of what you want to display. It will let you pass in an object (i.e. any custom or built in type in the .net framework), but in that case, behind the scenes, it will simply invoke the object.ToString method on it to get the text that it will use for display.

I don't know what your code looks like that adds items to the list box, but here is a snippit that might help you:

foreach (RouteStop stop in routeStops) {
    listBox.Items.Add("{0} {1} {2} {3}", stop.number, stop.location, stop.street, stop.city);
}

1 Comment

This will fill the ListBox with strings - hard to find back the selected RouteStop.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.