Make ListBox items have a different value than item text

前端 未结 5 1601
旧时难觅i
旧时难觅i 2020-12-08 21:18

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 calle

相关标签:
5条回答
  • 2020-12-08 21:49

    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.

    0 讨论(0)
  • 2020-12-08 21:55

    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; };
    }
    
    0 讨论(0)
  • 2020-12-08 22:01

    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)

    0 讨论(0)
  • 2020-12-08 22:03

    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);
            }
    
    0 讨论(0)
  • 2020-12-08 22:04

    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);
            }
    
        }
    
    0 讨论(0)
提交回复
热议问题