15

This must be very easy but I am stuck. I have a listBox with X Items. Each Item has a text description (Which appears in the listBox) and its value(numerical). I want to be able to get the value property of an item, using the index number of the item.

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
tzam
  • 1,802
  • 3
  • 14
  • 15

7 Answers7

17

It would be

String MyStr = ListBox.items[5].ToString();
Lorenz Meyer
  • 19,166
  • 22
  • 75
  • 121
user3666179
  • 171
  • 1
  • 3
  • Items may be `DataRowView` or a Complex Object or other types. Underlying value of an item should be calculated base on `ValueMember`. – Reza Aghaei Jul 11 '16 at 11:07
13

Here I can't see even a single correct answer for this question (in WinForms tag) and it's strange for such frequent question.

Items of a ListBox control may be DataRowView, Complex Objects, Anonymous types, primary types and other types. Underlying value of an item should be calculated base on ValueMember.

ListBox control has a GetItemText which helps you to get the item text regardless of the type of object you added as item. It really needs such GetItemValue method.

GetItemValue Extension Method

We can create GetItemValue Extension Method to get item value which works like GetItemText:

using System;
using System.Windows.Forms;
using System.ComponentModel;
public static class ListControlExtensions
{
    public static object GetItemValue(this ListControl list, object item)
    {
        if (item == null)
            throw new ArgumentNullException("item");

        if (string.IsNullOrEmpty(list.ValueMember))
            return item;

        var property = TypeDescriptor.GetProperties(item)[list.ValueMember];
        if (property == null)
            throw new ArgumentException(
                string.Format("item doesn't contain '{0}' property or column.",
                list.ValueMember));
        return property.GetValue(item);
    }
}

Using above method you don't need to worry about settings of ListBox and it will return expected Value for an item. It works with List<T>, Array, ArrayList, DataTable, List of Anonymous Types, list of primary types and all other lists which you can use as data source. Here is an example of usage:

//Gets underlying value at index 2 based on settings
this.listBox1.GetItemValue(this.listBox1.Items[2]);

Since we created the GetItemValue method as an extension method, when you want to use the method, don't forget to include the namespace which you put the class in.

This method is applicable on ComboBox and CheckedListBox too.

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398
5

If you are working on a windows forms project you can try the following:

Add items to the ListBox as KeyValuePair objects:

listBox.Items.Add(new KeyValuePair(key, value);

Then you will be able to retrieve them the following way:

KeyValuePair keyValuePair = listBox.Items[index];
var value = keyValuePair.Value;
Akram Shahda
  • 14,655
  • 4
  • 45
  • 65
4

I'm using a BindingSource with a SqlDataReader behind it and none of the above works for me.

Question for Microsoft: Why does this work:

  ? lst.SelectedValue

But this doesn't?

   ? lst.Items[80].Value

I find I have to go back to to the BindingSource object, cast it as a System.Data.Common.DbDataRecord, and then refer to its column name:

   ? ((System.Data.Common.DbDataRecord)_bsBlocks[80])["BlockKey"]

Now that's just ridiculous.

Mike K
  • 1,313
  • 2
  • 18
  • 28
  • Alternatively I guess I could iterate through the Items collection, selecting each item and then getting SelectedValue. But I shouldn't have to do this, and it's a kludge anyway. – Mike K Sep 02 '11 at 15:00
  • An Item in `Items` collection of a `ListBox` is of type object and it doesn't have a `Value` property. The item may be a `DataRowView`, a complex object or a primary type. The value should be calculated using `ValueMember` property, the same way `SelectedValue` works. – Reza Aghaei Jul 12 '16 at 12:06
2

This works for me:

ListBox x = new ListBox();
x.Items.Add(new ListItem("Hello", "1"));
x.Items.Add(new ListItem("Bye", "2"));

Console.Write(x.Items[0].Value);
DaveShaw
  • 52,123
  • 16
  • 112
  • 141
  • 7
    As far as I know, `ListBox.Items` is of type `System.Windows.Forms.ListBox.ObjectCollection` which means `ListBox.Items[index]` is an Object which does not have a Value property !! – Akram Shahda Jun 28 '11 at 09:36
  • 1
    This was an answer for WebForms, as he is referring to Settings a Text and Value, that made me assume WebForms, not WinForms. – DaveShaw Jun 28 '11 at 09:40
2

Suppose you want the value of the first item.

ListBox list = new ListBox();
Console.Write(list.Items[0].Value);
Bibhu
  • 4,053
  • 4
  • 33
  • 63
1

simply try this 'listBox' is your list and 'yu' is a veriable to which the value on index 0 will be assigned

string yu = listBox1.Items[0].ToString();
MessageBox.Show(yu);
  • In fact at the best case it will return text for an item. Items may be `DataRowView` or a complex object or other types. The underlying value of an item should be calculated base on `ValueMember`. – Reza Aghaei Jan 14 '19 at 17:45