如何在C#中的TableLayoutPanel中显示词典的内容?

如何在C#中的TableLayoutPanel中显示词典的内容?,c#,dictionary,tablelayoutpanel,C#,Dictionary,Tablelayoutpanel,我需要在名为ucOutputPredictionsResults的TableLayoutPanel中显示名为predictionDictionary的词典的内容,并在第一列中显示名称,在第二列中显示值。在我的字典中,所有键和值都有类型字符串 我可以显示键和值,但不能按我请求的顺序显示 以下是我所做的: this.ucOutputPredictionsResults.RowCount = 0; this.ucOutputPredictionsResults.ColumnCount = 0; fo

我需要在名为
ucOutputPredictionsResults
的TableLayoutPanel中显示名为
predictionDictionary
的词典的内容,并在第一列中显示名称,在第二列中显示值。在我的字典中,所有键和值都有类型字符串

我可以显示键和值,但不能按我请求的顺序显示

以下是我所做的:

this.ucOutputPredictionsResults.RowCount = 0;
this.ucOutputPredictionsResults.ColumnCount = 0;

foreach (KeyValuePair<string, string> kvp in (_testExecution as TestExecutionAlveoGraph)
                                             .predictionDictionary)
{
   Label lb = new Label();
   lb.Text = kvp.Key;

   this.ucOutputPredictionsResults.Controls.Add(lb,
             this.ucOutputPredictionsResults.ColumnCount,
             this.ucOutputPredictionsResults.RowCount);

   Label valueLbl = new Label();
   valueLbl.Text = kvp.Value;

   this.ucOutputPredictionsResults.Controls.Add(valueLbl,
            this.ucOutputPredictionsResults.ColumnCount +1, 
            this.ucOutputPredictionsResults.RowCount);
}
this.ucOutputPredictionsResults.RowCount=0;
this.ucOutputPredictionsResults.ColumnCount=0;
foreach(KeyValuePair kvp in(_TestExecutionas TestExecutionAlveoGraph)
.predictionDictionary)
{
标签lb=新标签();
lb.Text=kvp.Key;
这个.ucOutputPredictionsResults.Controls.Add(lb,
此.ucOutputPredictionsResults.ColumnCount,
此参数为.ucOutputPredictionsResults.RowCount);
标签值lbl=新标签();
valueLbl.Text=kvp.Value;
此.ucOutputPredictionsResults.Controls.Add(valueLbl,
此.ucOutputPredictionsResults.ColumnCount+1,
此参数为.ucOutputPredictionsResults.RowCount);
}
但结果并不是我所期望的:


虽然我同意TaW的观点,即您应该明确设置TableLayoutPanel并以更可控的方式添加控件,但您可以通过将ColumnCount设置为2并使用只接收控件的add()重载来解决“问题”。届时将按预期添加标签

简化代码:

private void button1_Click(object sender, EventArgs e)
{
    this.ucOutputPredictionsResults.Controls.Clear();
    this.ucOutputPredictionsResults.RowCount = 0;
    this.ucOutputPredictionsResults.ColumnCount = 2;

    foreach (KeyValuePair<string, string> kvp in _testExecution)
    {
        Label lb = new Label();
        lb.Text = kvp.Key;

        this.ucOutputPredictionsResults.Controls.Add(lb);

        Label valueLbl = new Label();
        valueLbl.Text = kvp.Value;

        this.ucOutputPredictionsResults.Controls.Add(valueLbl);
    }
}
private void按钮1\u单击(对象发送者,事件参数e)
{
this.ucOutputPredictionsResults.Controls.Clear();
this.ucOutputPredictionsResults.RowCount=0;
this.ucOutputPredictionsResults.ColumnCount=2;
foreach(测试执行中的KeyValuePair kvp)
{
标签lb=新标签();
lb.Text=kvp.Key;
this.ucOutputPredictionsResults.Controls.Add(lb);
标签值lbl=新标签();
valueLbl.Text=kvp.Value;
this.ucOutputPredictionsResults.Controls.Add(valueLbl);
}
}

您的目标是什么:Winforms、WPF、ASP。。?您应该始终正确标记您的问题,以便人们可以在问题页面上看到它!您依赖TLP的布局来增长所需的列和行。但是当添加第一个控件时,您强制它将其放置在某个位置,对吗?为此,它必须同时创建一列和一行!!现在colcount和rowCount为1,因此第二个标签将添加到第1行,而不是0等最好自己创建正确的数字和计算正确的指数。。!谢谢它工作得非常好。很抱歉缺少信息,但我是一名新的开发人员。下次我会做得更好。再次感谢。