[英]How to display the content of a dictionary in a TableLayoutPanel in C#?
我需要在名為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);
}
但結果不是我所期望的:
盡管我同意 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);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.