简体   繁体   中英

How to show listbox added items count in Label?

车间选择器GUI

private void button5_Click(object sender, EventArgs e)
{
    int gTotal = 1;


    for (int gCount = 0; gCount < listBox3.Items.Count - 1; gCount++)

    gTotal += (listBox3.Items.Add(gCount));

    label1.Text = gTotal.ToString();
}

Hey guys i'm not entirely sure of how to use Listboxes, but my question is how can I display the numbers that populate in my list box 3 to go into the label?

private void button5_Click(object sender, EventArgs e)
{
    int gTotal = 1;
    var collection = listBox3.Items.Cast<String>().ToList();

    for (int gCount = 0; gCount < collection.Count - 1; gCount++)
    {
        int item;
        if (int.TryParse(collection[gCount], out item)
        {
            gTotal += item;
        }
    }

    label1.Text = gTotal.ToString();
}

Cast the items to a List then make sure that items are ints, if so add them up.

Parse your items.

private void button5_Click(object sender, EventArgs e)
{
    int gTotal = 1;

    for (int gCount = 0; gCount < listBox3.Items.Count; gCount++)
         gTotal += int.Parse(listBox3.Items[gCount].ToString()); 
         // assuming all items in the listbox is an int.

    label1.Text = gTotal.ToString();
}

What trying to do

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM