c# - 如何在标签中显示二维数组的值?
问题描述
我有一个二维数组,如下所示。我想在 Windows 窗体上使用标签显示它,因此它采用表格格式(行和列)。我该如何实现?
string[,] map = new string[10, 10]
{
{ ".", ".", ".", ".", ".", ".", ".", ".", ".", "." },
{ ".", ".", ".", ".", ".", ".", ".", ".", ".", "." },
{ ".", ".", ".", ".", ".", ".", ".", ".", ".", "." },
{ ".", ".", ".", ".", ".", ".", ".", ".", ".", "." },
{ ".", ".", ".", ".", ".", ".", ".", ".", ".", "." },
{ ".", ".", ".", ".", ".", ".", ".", ".", ".", "." },
{ ".", ".", ".", ".", ".", ".", ".", ".", ".", "." },
{ ".", ".", ".", ".", ".", ".", ".", ".", ".", "." },
{ ".", ".", ".", ".", ".", ".", ".", ".", ".", "." },
{ ".", ".", ".", ".", ".", ".", ".", ".", ".", "." }
};
解决方案
使用monospaced
类似字体Consolas
和向左填充空格,如下所示:
Label label = new Label();
this.Controls.Add(label);
label.Size = new Size(500, 500); // Enter custom size or use Graphics.MeasureString method to find proper size dynamically
label.AutoSize = false;
label.Font = new Font("Consolas", 8);
for (int i = 0; i < map.GetLength(0); i++)
{
for (int j = 0; j < map.GetLength(1); j++)
{
label.Text += map[i, j].PadLeft(5, ' ');
}
label.Text += Environment.NewLine;
}
推荐阅读
- terraform - Terraform aws cognito 用户池
- database - 变量持续存在时的“延迟”顺序
- python - 如何在不使用任何模运算符的情况下修复此功能?
- python - Keras fit_generator 的验证准确性与使用相同数据手动计算时不同
- python - 打印python字典时如何更改键颜色
- java - com.liferay.portal.kernel.scheduler.SchedulerEngine:PORTLET_ID 无法解析或不是字段
- angular - Angular 8:如何将查询参数绑定到输入字段
- python - 使用 urllib [Errno 2] 下载图像没有这样的文件或目录
- c++ - C++ 字符串:size_t 和 string::npos
- r - 仅返回在 r 中一次匹配多个条件的行数