rusty molle rusty molle - 3 months ago 25
C# Question

How to get the first value from the listbox

I have a listbox where the user has generated a list of random numbers into it, and im trying to output the first value from the listbox to a label text. However, just seems to be outputting a random number instead.

while (lstHoldValue.Items.Count < MAX_ITEMS)
int value = rnd.Next(1 + 100);//ranges from 0 - 100
if (!lstHoldValue.Items.Contains(value))
int d = (Convert.ToInt32(lstHoldValue.Items[0]));
label11.Text = d.ToString();


You need to convert item to string first. Your code should be like:

int d = (Convert.ToInt32(lstHoldValue.Items[0].ToString()));
label11.Text = d.ToString(); 


I think @Visual Vincent is right. I've made a mistake here :). Your code should work without that. ToString method is not necessary.

int d = (Convert.ToInt32(lstHoldValue.Items[0]));
Text = d.ToString();