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 に答える