作业要求:仅用26×26文本框网格制作电子表格,求技术指导
Hey there! I feel your pain—building a 26x26 TextBox grid without being able to use DataGridView or rely on Excel imports sounds like a tough constraint, but it’s totally manageable with some simple programmatic setup. Let me walk you through how to do this step by step, assuming you’re working with Windows Forms (the most common context for TextBox-based grids like this).
Step 1: Prep Your Form
First, make sure your form has enough space to fit the grid. Turn on AutoScroll in the form’s properties so users can scroll if the grid is larger than the default window size. You can also set a minimum size for the form to avoid weird layout issues.
Step 2: Dynamically Generate TextBoxes with Loops
Dragging 676 TextBoxes manually is a nightmare—instead, use nested loops to create them programmatically. This keeps your code clean and easy to adjust later.
Drop this code into your form’s Load event handler (adjust values like size/spacing to fit your needs):
private void YourForm_Load(object sender, EventArgs e) { // Define basic grid settings int cellSize = 32; // Width/height of each TextBox int gap = 2; // Space between cells int startX = 15; // Starting X position on the form int startY = 15; // Starting Y position on the form // Store all TextBoxes in a list for easy access later List<TextBox> gridCells = new List<TextBox>(); // Nested loops for rows (26) and columns (26) for (int row = 0; row < 26; row++) { for (int col = 0; col < 26; col++) { // Create a new TextBox TextBox cell = new TextBox(); // Calculate position based on row/column, size, and gap cell.Location = new Point( startX + (col * (cellSize + gap)), startY + (row * (cellSize + gap)) ); // Set cell size cell.Size = new Size(cellSize, cellSize); // Optional: Tweak usability settings cell.MaxLength = 1; // Restrict to single character (great for grid-style input) cell.TextAlign = HorizontalAlignment.Center; cell.BorderStyle = BorderStyle.FixedSingle; // Add the cell to the form and our tracking list this.Controls.Add(cell); gridCells.Add(cell); } } }
Step 3: Work with Grid Data
Once your grid is generated, use the gridCells list to read or update specific cells. Since the list is ordered row-by-row, you can calculate the index of any cell like this:
// Get the cell at row 4, column 2 (0-indexed—so row 5, column 3 if you count from 1) int cellIndex = (4 * 26) + 2; TextBox targetCell = gridCells[cellIndex]; // Set a value in the cell targetCell.Text = "A"; // Read the value from the cell string cellValue = targetCell.Text;
Step 4: Optional Nice-to-Haves
- Grid Lines: If you want clearer cell separation, add a Panel behind each TextBox with a gray border, or use a single Panel as a container and adjust the gap size to create visible lines.
- Input Validation: Add a
TextChangedevent handler to the TextBoxes to restrict input (e.g., only letters, no numbers). - Responsive Resizing: Calculate
cellSizedynamically based on the form’s client size so the grid scales when the user resizes the window.
内容的提问来源于stack exchange,提问作者Mark Bonello

