簡體   English   中英

如何在標簽中顯示2D數組的值?

[英]How to display the values of an 2D array in a label?

我有一個2D數組,如下所示。 我想使用Windows窗體上的標簽顯示它,因此它采用表格格式(行和列)。如何實現此目的?

string[,] map = new string[10, 10] 
{ 
    { ".", ".", ".", ".", ".", ".", ".", ".", ".", "." }, 
    { ".", ".", ".", ".", ".", ".", ".", ".", ".", "." }, 
    { ".", ".", ".", ".", ".", ".", ".", ".", ".", "." }, 
    { ".", ".", ".", ".", ".", ".", ".", ".", ".", "." }, 
    { ".", ".", ".", ".", ".", ".", ".", ".", ".", "." }, 
    { ".", ".", ".", ".", ".", ".", ".", ".", ".", "." }, 
    { ".", ".", ".", ".", ".", ".", ".", ".", ".", "." }, 
    { ".", ".", ".", ".", ".", ".", ".", ".", ".", "." }, 
    { ".", ".", ".", ".", ".", ".", ".", ".", ".", "." }, 
    { ".", ".", ".", ".", ".", ".", ".", ".", ".", "." }
};

使用象Consolas這樣的monospaced字體,並在左邊留出填充空間,如下所示:

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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM