19.12.2012 Views

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Index 1<br />

lblColLab() Caption A<br />

Index 1<br />

txtCell() Text (blank)<br />

Index 1<br />

Height 1095<br />

Width 285<br />

■ CODING THE PROGRAM<br />

A Case Study: Calculating with a Spreadsheet 203<br />

The top row of Figure 6-12 shows the different events to which the program must respond.<br />

Table 6.8 identifies the corresponding event procedures <strong>and</strong> the general procedures they call.<br />

Let’s examine each event procedure.<br />

1. Form_Load assigns the number of rows (16) <strong>and</strong> columns (6) in our spreadsheet<br />

to the form-level variables maxRow <strong>and</strong> maxCol. Form_Load then calls<br />

three general procedures to create <strong>and</strong> initialize the spreadsheet.<br />

The procedure CreateSpreadsheet loads each element of the txtCell( ) control<br />

array in order from left to right, top to bottom. Cell 1, which is to be the first<br />

cell in the first row, is not loaded because it was created at design time. The Top<br />

property of a new cell is set so that the top edge of the new cell overlaps the<br />

bottom edge of the previous cell in the column. The Top property of the first<br />

cell in a column is not modified, <strong>and</strong> so the value of the Top property is the<br />

same as cell 1. Similarly, the Left property of a new cell is set so that the left<br />

edge of the new cell overlaps the right edge of the previous cell in the row. The<br />

Left property of the first cell in a row is not modified, <strong>and</strong> so the value of the<br />

Left property is the same as cell 1. CreateSpreadsheet also loads the additional<br />

row <strong>and</strong> column label elements <strong>and</strong> assigns an appropriate caption. Create-<br />

Spreadsheet’s final task is to set the Height <strong>and</strong> Width properties of frm-<br />

SpreadSheet to accommodate all the objects that have been loaded. The<br />

numbers 500 <strong>and</strong> 200, which appear in these statements, were obtained by trial<br />

<strong>and</strong> error <strong>and</strong> are necessary to account for the space used by the form caption<br />

<strong>and</strong> borders.<br />

The procedure SetStructure assigns heading values to various cells of the<br />

spreadsheet in accordance with the specific application we were asked to program.<br />

The user will not be able to alter the value in these cells, because the rules<br />

for which cells are to be totaled, <strong>and</strong> where these totals are to be placed are<br />

“hard wired” into the program <strong>and</strong> cannot be changed by the user. SetStructure<br />

also assigns values to a set of form-level variables so that other procedures in<br />

the program can be coded using meaningful names rather than possibly obscure<br />

numbers. Besides Form_Load, SetStructure is also called by the<br />

cmdNew_Click event procedure.<br />

The procedure SetDefaults assigns the income <strong>and</strong> expense category headings<br />

shown in Figure 6-9 to the appropriate cells. The user may change these headings<br />

at any time, <strong>and</strong> must supply them if the “New” comm<strong>and</strong> is issued.<br />

2. txtCell_GotFocus checks to see if the cell that has received the focus may,<br />

according to the rules of this application, be edited by the user. The row <strong>and</strong> column<br />

numbers for the cell are computed from the cell’s index. If the cell that has<br />

received the focus is in a column after stopCol, the last editable column, then the<br />

column to be edited is changed to startCol, the first editable column, <strong>and</strong> the row<br />

to be edited is advanced by one. If the row to be edited does not contain any<br />

editable cells, then the row to be edited is advanced to the next row containing<br />

editable cells. (When the focus goes past the last row of editable cells, the next<br />

editable row is the first editable row, that is, incStartRow.) Finally, focus is set to<br />

the adjusted row <strong>and</strong> column, but only if an adjustment has been made. If the test<br />

Indx(row,col)Index were not made <strong>and</strong> focus were reset to a cell that already

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!