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
  • I have added a tag 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
  • 3 You've not posted the code where you try to populate the lists. How are we supposed to see what's wrong with it when you don't include it in your question? – Ken White Commented Jan 27 at 1:09
  • 1 @Ken White — Exactly, the population of the lists is the absolutely important piece here. Without that, the question cannot be answered. – Sergey A Kryukov Commented Jan 27 at 1:13
  • Why do you use lists instead of arrays, when you have a constant size grid? – Olivier Jacot-Descombes Commented Jan 27 at 11:35
  • 1 @OlivierJacot-Descombes - I like the idea of using an array. I am new to c# and couldn't figure out how to declare a 9x9 array. Another post showed me how. I'll try it. – Don Ford Commented 2 days ago
 |  Show 10 more comments

1 Answer 1

Reset to default 0

You'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