CasperT
CasperT

Reputation: 3475

Make ListBox items have a different value than item text

I want a ListBox full of items. Although, each item should have a different value. So when the user selects an item and presses a button, a method will be called which will use the value the select item has.

I don't want to reveal the item values to the user.

EDIT: This is not for ASP.NET, it's for a Windows Forms application. I just thought the HTML example would be easy to read.

I have the inspiration from HTML:

<form>
<input type="radio" name="sex" value="Value1" /> Male
<br />
<input type="radio" name="sex" value="Value2" /> Female
</form>

This also allows me to use different values than what the user sees.

Upvotes: 23

Views: 59517

Answers (5)

iTai Ortix
iTai Ortix

Reputation: 21

Very simple:

foreach(var item in *Your Source List*)
        {
            ListItem dataItem =  new ListItem();
            dataItem.Text = "value to show";
            dataItem.Value = *another value you want*;
            listBox.Items.Add(dataItem);
        }

Upvotes: 2

cexars
cexars

Reputation: 1

Easy!

protected void Page_Load(object sender, EventArgs e)
    {
        llenaListBox(ListBox1, 0, 10);
    }

    private void llenaListBox(ListBox PoListBox, int PiMinimo, int PiMaximo)
    {
        int Li;
        for (Li = PiMinimo; Li <= PiMaximo; Li++)
        {
            ListItem obj = new ListItem();
            obj.Text  = Li.ToString();
            obj.Value = Li.ToString();
            PoListBox.Items.Add(obj);
        }

    }

Upvotes: -1

odalet
odalet

Reputation: 1419

As stated by the 1st answer, the use of DisplayMember works whether you are using asp.net or winforms.

And to comment a bit more, it also works if you are using the rather old fashion Items.add way of adding items to a ListBox.

Just for fun, here is a simple demo of what you need (just create a new form and drop on it a ListBox and a Label):

public partial class Form1 : Form
{
    class Customer
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }

        public override string ToString()
        {
            return string.Format("{0} {1}", LastName, FirstName);
        }
    }

    public Form1() { InitializeComponent(); }

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);                        
        listBox1.DisplayMember = "LastName";            
        listBox1.DataSource = GetCustomers();
        //listBox1.Items.AddRange(GetCustomers().ToArray());            
    }

    private IEnumerable<Customer> GetCustomers()
    {
        return new List<Customer>()
        {
            new Customer() { FirstName = "Gustav", LastName = "MAHLER" },
            new Customer() { FirstName = "Johann Sebastian", LastName = "BACH" }
        };
    }

    private void lb_SelectedIndexChanged(object sender, EventArgs e)
    {
        label1.Text = listBox1.SelectedItem.ToString();
    }        
}

Enjoy

PS: @2nd post Tag is not available to ListBox: because it accepts an array of object, not a specific item container like ListView... but you don't need any in your case. Tag is useful when you want to carry additional data along with a specific TreeViewItem or ListViewItem for example. By the way, Tag is defined at the Control level and so exists for Button, Label, and so on... but for my part I think it is rather a bad idea to store business data in it (untyped, UI coupled...) apart from the ListView and TreeView cases for which it is rather convenient.

Upvotes: 1

Fredrik M&#246;rk
Fredrik M&#246;rk

Reputation: 158309

You can choose what do display using the DisplayMember of the ListBox.

List<SomeData> data = new List<SomeData>();
data.Add(new SomeData() { Value = 1, Text= "Some Text"});
data.Add(new SomeData() { Value = 2, Text = "Some Other Text"});
listBox1.DisplayMember = "Text";
listBox1.DataSource = data;

When the user selects an item, you can read the value (or any other property) from the selected object:

int value = (listBox1.SelectedItem as SomeData).Value;

Update: note that DisplayMember works only with properties, not with fields, so you need to alter your class a bit:

public class SomeData
{
    public string Value { get; set; };
    public string Text { get; set; };
}

Upvotes: 38

second
second

Reputation: 28637

items have a property called 'Tag', which you can use to store any information you want (hidden from the user)

ListViewItem myItem = new ListViewItem();
myItem.Text = "Users see this";
myItem.Tag = "Users don't see this";

(or set the appropriate properties in the property explorer)

Upvotes: 3

Related Questions