tags:

views:

202

answers:

8

I'm having trouble figuring out how to add items to a ListBox in WinForms.

I'm trying:

list.DisplayMember = "clan";
list.ValueMember = sifOsoba;

How can I add items to the list with an int value (ValueMember) and some text (for the DisplayMember)?

list.Items.add(?)

I can't use ListBoxItem. Why?

+6  A: 
list.Items.add(new ListBoxItem("name", "value"));

The internal (default) data structure of the ListBox is the ListBoxItem.

monksy
listBoxItem? why I do not have that?
Ante B.
Was there an error? I've added an link to the class doc
monksy
You can just add an object - it doesn't have to be a ListBoxItem.
Philip Wallace
That is why I said the default item was ListBoxItem, with another object you must specify the TextField and ValueField bindings. I didn't mention this, because it would be distracting towards the answer.
monksy
Googled for this exact problem, SO to the rescue again! ...and +1 for you, thanks :)
mizipzor
+2  A: 

You have to create an item of type ListBoxItem and add that to the Items collection:

list.Items.add( new ListBoxItem("clan", "sifOsoba"));
+3  A: 

In WinForms, ValueMember and DisplayMember are used when data-binding the list. If you're not data-binding, then you can add any arbitrary object as a ListItem.

The catch to that is that, in order to display the item, ToString() will be called on it. Thus, it is highly recommended that you only add objects to the ListBox where calling ToString() will result in meaningful output.

John Rudy
+2  A: 

DisplayMember and ValueMember are mostly only useful if you're databinding to objects that have those properties defined. You would then need to add an instance of that object.

e.g.:

public class MyObject
{
     public string clan { get; set; }
     public int sifOsoba { get; set; }
     public MyObject(string aClan, int aSif0soba)
     {
        this.clan = aClan;
        this.sif0soba = aSif0soba;
     }

     public override string ToString() { return this.clan; }
 }

 ....

 list.Items.Add(new MyObject("hello", 5));

If you're binding it manually then you can use the example provided by goggles

zincorp
+1  A: 

You might want to checkout this SO question:

C# - WinForms - What is the proper way to load up a ListBox?

Kelsey
A: 

If you are adding integers, as you say in your question, this will add 50 (from 1 to 50):

for (int x = 1; x <= 50; x++)
{
   list.Items.Add(x);
}

You do not need to set DisplayMember and ValueMember unless you are adding objects that have specific properties that you want to display to the user. In your example:

listbox1.Items.Add(new { clan = "Foo", sifOsoba = 1234 });
Philip Wallace
A: 

The way I do this - using the format Event

  MyClass c = new MyClass();
  listBox1.Items.Add(c);

  private void listBox1_Format(object sender, ListControlConvertEventArgs e)
    {
        if(e.ListItem is MyClass)
        {
            e.Value = ((MyClass)e.ListItem).ToString();
        }
        else
        {
            e.Value = "Unknown item added";
        }
    }

e.Value being the Display Text

Then you can attempt to cast the SelectedItem to MyClass to get access to anything you had in there.
Also note, you can use anything (that inherits from object anyway(which is pretty much everything)) in the Items Collection.

PostMan
A: 

ListBoxItem is a WPF class, NOT a WinForms class.

For WPF, use ListBoxItem.

For WinForms, the item is a Object type, so use one of these:
1. Provide your own ToString() method for the Object type.
2. Use databinding with DisplayMemeber and ValueMember (see Kelsey's answer)

AZ