1

我想在单独的标签中显示字典中的值。我得到这样的字典:

Dictionary<string, int> counts = new Dictionary<string, int>();
foreach (string code in myCodeList)
{
    if (!counts.ContainsKey(code))
    {
       counts.Add(code, 1);
    }
    else
    {
       counts[code]++;
    }
}

//now counts contains the expected values

我想动态生成标签,因为其中的元素counts只能在运行时确定。

4

2 回答 2

2
Dictionary<string, int> counts = new Dictionary<string, int>();
foreach (string code in myCodeList)
{
    if (!counts.ContainsKey(code))
    {
       counts.Add(code, 1);
    }
    else
    {
       counts[code]++;
    }
}

Point p = new Point(12, 12); //initial location - adjust it suitably
foreach (var item in counts)
{
    var label = new Label();
    label.Text = item.Key + " - " + item.Value;
    label.Location = new Point(p.X, p.Y);
    label.Tag = item; //optional
    Controls.Add(label);

    p.Y += label.Height + 6; //to align vertically
    //p.X += label.Width + 18; //to align horizontally. 
}

这是基本方法。您所要做的就是p根据您的设计调整变量以正确定位标签。

于 2012-07-23T07:01:58.440 回答
0

这应该足够了

编辑

YourLabel.Text = counts.Count.ToString();
于 2012-07-23T06:26:57.630 回答