Reputation: 323
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
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
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