I have a listbox i want to populate with elements from a database. I have an id and a name that I get from the database.

How can I change the listbox's default indices to the id's i get from the database? So that the listbox displayes the text, and has the id as index.

Recommended Answers

All 5 Replies

every item has value propery and text propety in listbox, bind name as text and indexid as value then u can easily access listbox1.item.value or
listbox1.item.text when what need

Yeah... you can't really change the index, like if the ID is 4, and it's the only one in the listbox, you're going to still access it as myListbox[0].

Just like kdcorp87 said, you can assign a value and a text property. You're probably looking for the Value property.

Okay.

Neither listbox1.item.value nor listbox1.item.text work. What I have been using is listbox1.Items.Add(string a). I'm using C# WPF. If that makes any difference.

Thanks.

In this situation, i tend to make a class or struct to hold my listview items. You can either use DisplayMember and ValueMember to control what is shown and returned:

private void Form1_Load(object sender, EventArgs e)
        {
            ListBoxItem newItem = new ListBoxItem();
            newItem.ID = 4;
            newItem.name = "Joe Bloggs";

            listBox1.Items.Add(newItem);
            listBox1.DisplayMember = "name";
            listBox1.ValueMember = "ID";
        }
        
        private class ListBoxItem
        {
            public int ID { get; set; }
            public string name { get; set; }
        }

or if you want to store more info than just Text and ID you can use ToString() to control what is displayed and cast SelectedItem to retrieve the data:

private void Form1_Load(object sender, EventArgs e)
        {
            ListBoxItem newItem = new ListBoxItem();
            newItem.ID = 4;
            newItem.name = "Joe Bloggs";
            newItem.department = "Admin";

            listBox1.Items.Add(newItem);
        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            ListBoxItem selectedItem = listBox1.SelectedItem as ListBoxItem;

            MessageBox.Show(string.Format("{0}: Staff member {1} works in {2}", selectedItem.ID, selectedItem.name, selectedItem.department));
        }
        
        private class ListBoxItem
        {
            public int ID { get; set; }
            public string name { get; set; }
            public string department { get; set; }

            public override string ToString()
            {
                return "User: " + name;
            }

        }

Just a note: the above code can be used with either a Class or a Struct, which you use depends on curcumstances and design requirements. The short difference between class and struct is that structs are a value type whilst calsses are reference. The debate about which to use when runs way too deep to cover here, but its worth reading into.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.