如何在Label中显示列表框添加的项目数?

时间:2017-11-09 09:12:50

标签: c#

Workshop Selector 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();
}

嘿伙计们我不完全确定如何使用Listbox,但我的问题是如何显示填充在我的列表框3中的数字以进入标签?

2 个答案:

答案 0 :(得分:0)

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

将项目转换为List,然后确保项目是int,如果是,则添加它们。

答案 1 :(得分:0)

解析你的物品。

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