1

I know my question might be already asked, but no given answer worked for me. I have a class named "Item" and I want to add Items to my listbox, but with strings displayed like myItem.name. I tried the suggested solution at this question c# add object to listbox and show string of object in it which is:

listBox.DisplayMember = myItem.name;
listBox.ValueMember = myItem.id;
listBox.Items.Add(myItem);

but it keeps displaying namespace.Item not the item's name.

Also I added MouseEventHandler on MouseClick, how to get selected item in listBox_MouseClick function Any idea please!!!

my class code:

class Item
{
    public string name;
    public Item parent;
    public List<Item> sons = new List<Item>();
    public int depth = 0;
    public int id = 0;

    private static int nextID = 0;

    public Item()
    {

    }

    public Item(string Path)
    {
        this.name = Path;
        this.parent = null;
        this.sons.Clear();
        this.depth = 0;
        this.id = nextID;
        nextID++;
    }

    public Item(Item Parent, string Path, int Depth)
    {
        this.parent = Parent;
        this.name = Path;
        this.sons.Clear();
        this.depth = Depth;
        this.id = nextID;
        nextID++;
    }

    public bool isRoot()
    {
        bool root = false;
        if (this.parent == null)
            root = true;
        return root;
    }

    public bool isFile()
    {
        bool file = false;
        if (this.sons.Count == 0)
            file = true;
        return file;
    }
}
6
  • Your class lacks a nice ToString method! Commented Aug 12, 2016 at 16:21
  • Shall I override the ToString method? Commented Aug 12, 2016 at 16:22
  • @waelrazouk what happens if you do? :) Commented Aug 12, 2016 at 16:24
  • It worked! But why? where am I using ToString Method? Commented Aug 12, 2016 at 16:29
  • you don't use it, the ListBox does to display the content Commented Aug 12, 2016 at 16:35

3 Answers 3

4

You have to override the ToString method in your Item class to make the ListBox display what you want. Because it uses the default ToString method of the class which displays what you see.

try this:

public override string ToString()
{
    // choose any format that suits you and display what you like
    return String.Format("Name: {0}", this.name);
} 

and use your regular approach

or use binding to get your items into the ListBox. You would need a List with your Items

List<Item> itemList = new List<Item>();
// populate it with you items and in the end
// bind it to the data source
this.listBox1.DataSource = itemList;

in any case you have to override the ToString method.

If you want to change your itemList don't forget to refresh the ListBox:

listBox1.Refresh();
listBox1.Update(); 
Sign up to request clarification or add additional context in comments.

Comments

2

In C# 6 you can do it like this

listBox.DisplayMember = nameof( myItem.name);
listBox.ValueMember =nameof( myItem.id);

Update

If not use

listBox.DisplayMember =  "name;
listBox.ValueMember ="id";

Also bind your data instead of adding them one by one like this:

listBox.DataSource = myList;
listBox.Databound();

Update 2

If you don't want to use datasource, you have to add data 1 by 1 like this. you have nothing to do with display member, etc:

listBox.Add(new ListBoxItem(myItem.Name,myIten.Id.ToString()));

8 Comments

It didn't recognize "nameof"
@waelrazouk are you using c# 6? msdn.microsoft.com/en-us/library/dn986596.aspx
What do you mean "c# 6"? I have visual studio 2010
@waelrazouk then you are using c# 4 :).
Did you try giving the list as the datasourse?
|
1

you should use the field name , not the field value

listBox.DisplayMember = "name";

same for id

listBox.ValueMember = "id";

2 Comments

you mean like this? <listBox.DisplayMember = "name";> <listBox.ValueMember = "id";> It didn't work either
coulde you please ,add to your question , the myItem class declration

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.