admin管理员组文章数量:1186277
I have a grid of 81 modified TextBox
items arranged in a 9x9 grid.
I added properties for row, column, block, and valid int entries for each box.
I was able to populate the row, column, and block properties interactively, but not the list of valid integers.
I want to create lists of rows, columns, and blocks so that I can iterate through them.
I tried this (I named my class GridBox
):
public List<System.Windows.Forms.TextBox> RowList =
new List<System.Windows.Forms.TextBox>();
public List<System.Windows.Forms.TextBox> ColumnList =
new List<System.Windows.Forms.TextBox>();
public List<System.Windows.Forms.TextBox> BlockList =
new List<System.Windows.Forms.TextBox>();
I get error messages when I try to populate these lists with lists of nine items:
IDE0059 Unnecessary assignment of a value to myRow1List
I want to end up being able to type a valid digit into a box, and have my code enter it at the proper location in a 9x9 array of nullable integers, where 0 has special meaning to create a null value at a location.
I have a grid of 81 modified TextBox
items arranged in a 9x9 grid.
I added properties for row, column, block, and valid int entries for each box.
I was able to populate the row, column, and block properties interactively, but not the list of valid integers.
I want to create lists of rows, columns, and blocks so that I can iterate through them.
I tried this (I named my class GridBox
):
public List<System.Windows.Forms.TextBox> RowList =
new List<System.Windows.Forms.TextBox>();
public List<System.Windows.Forms.TextBox> ColumnList =
new List<System.Windows.Forms.TextBox>();
public List<System.Windows.Forms.TextBox> BlockList =
new List<System.Windows.Forms.TextBox>();
I get error messages when I try to populate these lists with lists of nine items:
IDE0059 Unnecessary assignment of a value to myRow1List
I want to end up being able to type a valid digit into a box, and have my code enter it at the proper location in a 9x9 array of nullable integers, where 0 has special meaning to create a null value at a location.
Share Improve this question edited Jan 27 at 11:34 Olivier Jacot-Descombes 112k14 gold badges147 silver badges198 bronze badges asked Jan 26 at 20:32 Don FordDon Ford 1 New contributor Don Ford is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct. 15 | Show 10 more comments1 Answer
Reset to default 0You're reporting an error:
IDE0059 Unnecessary assignment of a value to myRow1List
If I'm understanding the code that you did post, it seems likely that it's going to create way more than the 81 textboxes you actually need and this will make for overlaps and conflicting assignments. To solve, make only the 81 display controls and work with collections that contain references to those controls, not the controls themselves. Here's what I mean.
Game Board
Idiomatically, a "game board UI" is often represented in WinForms as a TableLayoutPanel
. Using inheritance, a control like Label
can be used as the basis for a Square
. Making an indexer
for MainForm
allows squares to be easily referenced by their coordinates.
Indexer
Allows statements like this[column, row] = square;
Square this[int column, int row]
{
get => gameBoard.GetControlFromPosition(column, row) as Square
?? throw new InvalidOperationException();
set => gameBoard.Controls.Add(value, column, row);
}
The uninitialized game board can be populated by iterating its rows and columns
public MainForm()
{
InitializeComponent();
for (int column = 0; column < 9; column++)
{
for (int row = 0; row < 9; row++)
{
// Square inherits from Label
var square = new Square
{
Size = new Size(48, 48),
Margin = new Padding(1),
Anchor = (AnchorStyles)0xf,
BackColor = Color.DarkGray,
ForeColor = Color.WhiteSmoke,
BorderStyle = BorderStyle.FixedSingle,
TextAlign = ContentAlignment.MiddleCenter,
Font = new Font(Font.FontFamily, 16),
};
square.Click += Any_SquareClicked;
this[column, row] = square;
}
}
InitGame();
}
Game Board, after running InitGame
and player makes a few moves.
Columns, Rows, and Nonants
Now you're set up to where instead of the RowList
, ColumnList
and BlockList
you simply leverage the indexer to make the collections that you need.
Square[] GetRow(int row) =>
Enumerable
.Range(0, 9)
.Select(_ => this[_, row])
.ToArray();
Square[] GetColumn(int column) =>
Enumerable
.Range(0, 9)
.Select(_ => this[column, _])
.ToArray();
Square[,] GetNonant(int oneOfNine)
{
int startRow = (oneOfNine / 3) * 3;
int startColumn = (oneOfNine % 3) * 3;
Square[,] nonant = new Square[3, 3];
for (int row = 0; row < 3; row++)
{
for (int column = 0; column < 3; column++)
{
nonant[row, column] = this[startColumn + column, startRow + row];
}
}
return nonant;
}
本文标签: cPopulating a list of lists of TextBoxStack Overflow
版权声明:本文标题:c# - Populating a list of lists of TextBox - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738296487a2073404.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
winforms
and fixed some formatting. Please always take care of these things. Please review my edit after the approval. – Sergey A Kryukov Commented Jan 26 at 23:47