rusty molle rusty molle - 13 days ago 5
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))
lstHoldValue.Items.Add(value);
}
int d = (Convert.ToInt32(lstHoldValue.Items[0]));
label11.Text = d.ToString();

Answer

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(); 

Edited:

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();