c# - 如何在 C# 的 TableLayoutPanel 中显示字典的内容?
问题描述
我需要在一个名为 TableLayoutPanelucOutputPredictionsResults
的字典中显示名为的内容,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);
}
}
推荐阅读
- jquery - 如何摆脱 emberjs 中的 SourceMapConcat 构建错误?
- haskell - 在 Haskell 中读取大型二进制文件的最快方法是什么?
- c# - 在 Owin 中间件中覆盖响应正文
- javascript - 替换两个数组之间不同的项目
- asp.net-core - 如何将社交登录与现有的 .Net 核心 Web API 后端和 Angular SPA 前端与有效的 OpenIddict 用户/密码和不记名令牌集成
- batch-file - 批处理脚本输出到文本文件部分重复
- javascript - 当组件使用 JavaScript 而不是 TypeScript [Angular] 时,component.html 404 Not Found
- apache-spark - AWS Glue:java.lang.UnsupportedOperationException:CSV 数据源不支持二进制数据类型
- c# - 用于机器人框架版本 4 C# 的 Adaptive Cards nuget 包
- android - 当设备中安装了多个浏览器并且用户将不同的浏览器设置为默认浏览器而不是 chrome 浏览器时,如何加载 chrome 自定义选项卡?