C # create 3x3 2d map with shortcuts

Is there a way to create 3 rows with 3 columns with labels (or similar) to create a 2nd “map” with data for easy data management?

just placing 9 labels is easy, but I want each label to be accessed by the same array.

How it looks in the form:

label1 label2 label3
etiketka4 etiketka5 etiketka6
etiketka7 etiketka8 etiketka9

If I need to change the label5 property, I would like to get something like this:
labelarray [1] [1] .Text = "Test"; (labelarray [row] [column] .Property)

How to do it?

Or can it be achieved differently?

+3
source share
4
class Data
{
    private string text;
    public string Text
    {
        get { return text; }
        set { text = value; }
    }
}
class Program
{
    static void Main(string[] args)
    {
        Data[,] map = new Data[3, 3];
        map[1, 1] = new Data();
        map[1, 1].Text = "Test";
    }
}

: ​​.

+2
 private void button1_Click(object sender, EventArgs e)
    {
        string[] nine_labels = { "a", "b", "c", "d", "e", "f", "g", "h", "i" };

        var labelarray= new Label[3,3];

        // putting labels into matrix form

        int c = 0;

        for (int i = 0; i < 3; i++)
        {
            for (int j = 0; j < 3; j++)
            {
                var lbl = new Label();

                lbl.Text = nine_labels[c];

                lbl.Top = i * 100;
                lbl.Left = j * 100;

                labelarray[i, j] = lbl; 

                c++;
            }
        }

        // adding labels to form
        foreach (var item in labelarray)
        {
            this.Controls.Add(item);
        }

        // test

        labelarray[1, 1].Text = "test";
    }

. Click .

+1

The answer from tehMick actually throws a run-time exception in .NET 2.0, but other than that, the example is straight to the point.

Types of a two-dimensional array must have a publicly accessible property, so you can access it directly, as you said:

public class DTO
{
    private String myStrProperty;
    public String MyStrProperty
    {
        get {return myStrProperty; }
        set { myStrProperty = value; }
    }

    public DTO(string myStrProperty)
    {
        this.myStrProperty = myStrProperty;
    }
}

class Program
{
    private static Logger logger;
    static void Main(string[] args)
    {
        DTO[,] matrix = 
        {
            {new DTO("label1"), new DTO("label2")},
            {new DTO("label3"), new DTO("label4")}
        };

        matrix[0, 1].MyStrProperty = "otherValue";
    }
}
0
source

Here is an example specific to winforms. Hope this answers the question a bit:

    const int spacing = 50;
    Label[][] map = new Label[3][];
    for (int x = 0; x < 3; x++)
    {
        map[x] = new Label[3];
        for (int y = 0; y < 3; y++)
        {
            map[x][y] = new Label();
            map[x][y].AutoSize = true;
            map[x][y].Location = new System.Drawing.Point(x * spacing, y * spacing);
            map[x][y].Name = "map" + x.ToString() + "," + y.ToString();
            map[x][y].Size = new System.Drawing.Size(spacing, spacing);
            map[x][y].TabIndex = 0;
            map[x][y].Text = x.ToString() + y.ToString();
        }
        this.Controls.AddRange(map[x]);
    }
    map[1][1].Text = "Test";
0
source

Source: https://habr.com/ru/post/1729707/


All Articles