rusty molle rusty molle - 1 year ago 75
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();

Answer Source

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();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download