views:

151

answers:

4

Is there a way to create 3 rows with 3 colums with labels (or similar) to create a 2d "map" with data to manipulate in an easy way?

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

How it looks like in the form:

label1 label2 label3
label4 label5 label6
label7 label8 label9

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

How do I do this?

Or could this be achieved in another way?

+2  A: 
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";
    }
}

Edit: fixed error.

Eric Mickelsen
A: 

The answer from tehMick actually lead to a runtime exception in .NET 2.0 but, except for that, the example is straight to the point.

The types of the two dimensional array must have a public 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";
    }
}
mamoo
A: 

Here's an example that's specific to winforms. Hopefully it will answer the question a little bit better:

    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";
Eric Mickelsen
Works perfectly in Visual Studio 2010 with .NET 4.0 . How do I make this public accessible so I can use map[XPOS][YPOS].Text = "Test"; everywhere in my code?
Qrew
Just define the map at the appropriate scope. It's probably best to make it a member of the form that contains the labels. You can make it public as well, so that it can be accessed externally.
Eric Mickelsen
+1  A: 
 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";
    }

NOTE: You'll need to add one button and call this function on Click of that button.

TheMachineCharmer