tags:

views:

601

answers:

3

Hi,

I know this is a bad question, but google has failed me.

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 can not/Do not want to reveal the values to the user (e.g "Item1 (value)).

Any assistance is appreciated.

EDIT: This is NOT for asp.net. It is 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 then what the user sees - if I want to.

A: 

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)

second
Could you show an example? each item needs their own individual value
CasperT
+5  A: 

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; };
}
Fredrik Mörk
When using DisplayMember, it makes more sense to set ValueMember="Value" and then read SelectedValue
Henk Holterman
@Henk: +1, I agree, I just wanted to point out that you can get access to the full object, not only "pre-assigned" properties.
Fredrik Mörk
Hi, your code seems to work, thanks. There is a minor error though. It will not display the item Text in the listbox. It writes Namespace.SomeData....The value part works perfectly though, thanks!
CasperT
@caspert; glad it helped. The Text property was just an example, you can of course use any suitable property from the objects to use in your list, just set DisplayMember to the name of the property to show.
Fredrik Mörk
I tried setting it to = Text; as it is the property it should show the clients in the listbox. Although it still writes Namespace.SomeData in the listbox instead.
CasperT
What does the object that you add to the list look like (you can update your question with a code sample)?
Fredrik Mörk
Alright :) updated my original post
CasperT
So did I, hope it works now :o)
Fredrik Mörk
Ah, didn't thought of that. It all works perfectly now. Thanks
CasperT
A: 

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.

odalet