Lewis Cutter III
Lewis Cutter III

Reputation: 323

Creating a 2d array for textboxes

So I'm trying to create an 8X8 grid of textboxes. I want to have the ability to also access the textboxes when I need to search through them. I have looked into considering an embedded List (i.e. List<List<TextBoxes>>) Where the inner list has 8 slots and the outer lists also has 8. I was wondering if there was an easier way.

Also how would I add my textboxes from my form into this 2d array?

Thanks for the help.

-Lewis

Upvotes: 0

Views: 2530

Answers (3)

Michael
Michael

Reputation: 3608

Use a standard 2D array TextBoxes[8,8]

Upvotes: 0

cdhowie
cdhowie

Reputation: 169018

You could use a TextBox[,] for this purpose:

private TextBox[,] textboxes;

public YourClass() {
    // Add this after the text boxes have actually been set up...

    textboxes = new TextBox[,] {
        {textbox00, textbox01, textbox02, ...},
        {textbox10, textbox11, textbox12, ...},
        ,,,
    };
}

Then you can access textbox00 as textboxes[0,0], textbox56 as textboxes[5,6], etc.

Upvotes: 2

TheVillageIdiot
TheVillageIdiot

Reputation: 40507

try this:

private class Position
{
    internal int Row;
    internal int Col;
}

var txtBoxesDict=new Dictionary<Position, TextBox>();

txtBoxesDict.Add(new Position{Row=0,Col=0},txtBox0);

To access thrid textbox in fourth row, you can use:

MessageBox.Show(txtBoxesDict[new Position{Row=3, Col=2}].Text);

Upvotes: 0

Related Questions