所以我試圖創建一個8X8的文本框網格。當我需要搜索文本框時,我希望能夠訪問文本框。我考慮過一個嵌入式列表(即List<List<TextBoxes>>
),其中內部列表有8個插槽,外部列表也有8個。我想知道是否有更簡單的方法。創建文本框的二維數組
另外我如何將我的表單中的文本框添加到這個2d數組中?
感謝您的幫助。
-Lewis
所以我試圖創建一個8X8的文本框網格。當我需要搜索文本框時,我希望能夠訪問文本框。我考慮過一個嵌入式列表(即List<List<TextBoxes>>
),其中內部列表有8個插槽,外部列表也有8個。我想知道是否有更簡單的方法。創建文本框的二維數組
另外我如何將我的表單中的文本框添加到這個2d數組中?
感謝您的幫助。
-Lewis
你可以使用一個TextBox[,]
用於此目的:
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, ...},
,,,
};
}
然後可以訪問textbox00
作爲textboxes[0,0]
,textbox56
如textboxes[5,6]
等
試試這個:
private class Position
{
internal int Row;
internal int Col;
}
var txtBoxesDict=new Dictionary<Position, TextBox>();
txtBoxesDict.Add(new Position{Row=0,Col=0},txtBox0);
到第四行中訪問THRID文本框,你可以使用:
MessageBox.Show(txtBoxesDict[new Position{Row=3, Col=2}].Text);
使用標準的2D陣列文本框[8,8]