Join 136,482 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,730 people online right now. Registration is fast and FREE... Join Now!
Hi. I'd like to know how to set the value of an item in a listbox in C#. I don't want the index, I know how to get that. I don't want the item itself displayed. I just want to assign a numerical value to each item in the listbox and then display the item in a label or messagebox, using the numerical value to access the item. Possibly, it doesn't have to be a numerical value, but could be another variable? I am not sure. (I hope I have explained this correctly).
Yes, this is homework, but my instructor said we can use any resource we want, to get the answer. Apparently he doesn't care how we get the answer, just so we get it. Thanks:)
Here's my code so far, with notes to myself and failed attempts included.
int test = listBox1.SelectedIndices.Count; if (test == 0)//make sure they selected something from the listBox { MessageBox.Show("Please select at least one item", "Select something", MessageBoxButtons.OK, MessageBoxIcon.Information);
} else if (test >= 3) { MessageBox.Show("Please select no more than two from the list", "Try again");
}
/* HERE's WHERE THE PROBLEM IS, AND SOME THINGS I'VE TRIED:
label1.Text = listBox1.SelectedIndices.Count.ToString();//the number of items selected will appear in the label
label1.Text = listBox1.SelectedItems.ToString(); //displays "...selectedobjectcollection", not what we want to appear in the label label1.Text = listBox1.SelectedValue.ToString();//creates an exception
int listCount = listBox1.Items.Count;//gets the number of items in the collection, starts with 1 and x starts with 0 for (int x = 0; x < listCount; x++) { if (listBox1.SelectedIndices.Contains(x)) { MessageBox.Show(listBox1.Items[x].ToString());//put selected items in messagebox, one at a time--this works MessageBox.Show(listBox1.SelectedItems.GetEnumerator().ToString());//tried to put selected items in messagebox, one at a time--doesn't work }
label1.Text = listBox1.Items(x).item.value.tostring();//this is not right, won't work */ }
Your help is greatly appreciated!
This post has been edited by OliveOyl3471: 3 Mar, 2008 - 07:01 PM
I couldn't get that to work, either. I modified your suggestion to fit my existing code, but this doesn't work. It changes all entries to "value" instead of assigning a value to each name. You had "your function" where I have "value" but I didn't know what function to put there. Do you have an example of a function I could put there instead? Maybe that would work.
CODE
int listCount = listBox1.Items.Count;//gets the number of items in the collection, starts with 1 and x starts with 0 for (int x = 0; x < listCount; x++) {
int listCount = listBox1.Items.Count; for (int x = 0; x < listCount; x++) { for(int newNumber=0; newNumber<=x; newNumber++) { string y = listBox1.GetItemText(x).ToString(); newNumber=int.Parse(y);
label1.Text = newNumber.ToString();
This post has been edited by OliveOyl3471: 3 Mar, 2008 - 08:13 PM
int listCount = listBox1.Items.Count; for (int x = 0; x < listCount; x++) { for(int newNumber=0; newNumber<=x; newNumber++) { string y = listBox1.GetItemText(x).ToString(); newNumber=int.Parse(y);
label1.Text = newNumber.ToString();
Maybe your instructor is looking for enumeration of the items in your combo box?
CODE
enum boxItems { Mary, Joe, Mom, Dad, Sandy }
I dont know. I dont understand really what this would be for.
Thank you. I will try that, to display all the names in the label. (edit-I tried it, but I had to remove the () from Environment.NewLine(); It displays all items in the label, selected or not, but only the index numbers are displayed. So I guess that's not what I'm supposed to do, either)
I need to know how to get/set the value of the list item, not the index or the item itself, but a value?
Would you use the listbox.SelectedValue property, and then to get it into the label use this?
CODE
string y = listBox1.SelectedValue.ToString();
If so, how do you use the SelectedValue property to set the value of each item?
This is probably a lot easier than it seems, but I just can't figure it out, so far.
killnine, thanks for trying to help, but...it's a listbox. can you do enumeration with a listbox? I don't know what it would be used for either.
This post has been edited by OliveOyl3471: 4 Mar, 2008 - 09:14 AM
Might this possibly be correct? It doesn't give me an error, but it also doesn't display correctly. When I mouse over "ValueMember" it says "Gets or sets the property to use as the actual value for the items in the System.Windows.Forms.ListControl."
You are getting there, however your implementation is currently not going to work. You need to add the value member and the display name at the same time to the Listbox.
Here is the MSDN for the ListBox.ValueMember, it includes an example using a class and an ArrayList.
Thank you Jayman. I've been working on this most of the day, so I need to quit for today. I will try again tomorrow, using the suggestions from the site you linked. Hopefully I can get the answer tomorrow! OliveOyl:)