The program begins by defining several symbolic constants for the program (see Listing 1):
MAXELEMENTS sets the maximum size of the control array and is used in some of the looping code.
The BOXWIDTH and BOXHEIGHT constants set the width and height of the text boxes shown in the two figures.
I like to use symbolic constants in my programs for two reasons. First, if I need to change the array size or the size of the text boxes, I can change the appropriate constant at this one place in the code, and voilà! All instances of the constant are self-changing throughout the program. Second, symbolic constants help document what the code is doing. Which makes more sense:
For i = 0 To 80
For i = 0 To MAXELEMENTS - 1
The number 80 just looks like a magic number itself; without looking further at the code, I don't have much idea what 80 means or why it was used. The second version at least gives me some idea of what's going on. In any program, magic numbers are bad...symbolic constants are good.
Listing 1 Defining Symbolic Constants and Class Member Data
Public Class clsMagicSquare Inherits System.Windows.Forms.Form '================ Member Data ===================================== Private Const MAXELEMENTS As Integer = 81 ' Max is 9x9 matrix Private Const BOXWIDTH As Integer = 30 ' Each textbox this wide Private Const BOXHEIGHT As Integer = 20 ' Each textbox this high Private Const SPACEBETWEEN As Integer = 12 ' Space between textboxes Private Const ROWOFFSET As Integer = 80 ' Space between rows Private txtElement(MAXELEMENTS) As TextBox Private MatrixSize As Integer
The variable MatrixSize holds the size of the matrix that the user wants to calculate. Notice that the control array is declared as an array of text boxes. Because the array declaration uses MAXELEMENTS for its dimension, there is sufficient room to define 82 text boxes in the array (elements 0 through 81).
Listing 2 presents the code that defines the text boxes. Object MySize is an object of type Size. Size is a new object in VBN that lets you set the width and height of an object. In this example, MySize is responsible for the size of each text box in the matrix.
Listing 2 Defining the Control Array
Private Sub InitializeTextboxes() ' Purpose: This subroutine is used to create the textboxes used in the program. ' ' Argument List: ' none ' ' Return value: ' n/a Dim i, j As Integer Dim MySize As Size MySize.Width = BOXWIDTH ' Each textbox this size MySize.Height = BOXHEIGHT For i = 0 To MAXELEMENTS - 1 ' Create for max matrix size txtElement(i + 1) = New TextBox ' Statement 1: Make a new one Me.Controls.Add(txtElement(i + 1)) ' Statement 2: Put on form txtElement(i + 1).Size = MySize ' Statement 3: textbox size txtElement(i + 1).TextAlign = HorizontalAlignment.Center txtElement(i + 1).Visible = False Next i txtMagicNumber.Visible = False lblMagicNumber.Visible = False End Sub
The For loop creates and then sets several properties for each of the text boxes. The first statement in the For loop body defines a new text box object. The second statement is responsible for placing the newly defined text box on the form (that is, part of the forms collection). The next three statements in the loop set the size of each text box, center any text that may appear in the text box, and hide it temporarily.
Notice that the InitializeTextboxes() subroutine needs to be called only once to create all the elements in the text box control array. If the user runs the program multiple times using different matrix sizes, the code simply shows those text boxes that are needed and hides the rest.
The last two statements simply hide the magic number text box and its label in preparation for another run of the program.