📄 velp12.htm
字号:
<HTML><HEAD><TITLE>Visual Basic in 12 Easy Lessons velp12.htm </TITLE><LINK REL="ToC" HREF="index.htm"><LINK REL="Index" HREF="htindex.htm"><LINK REL="Next" HREF="velxa.htm"><LINK REL="Previous" HREF="vel24.htm"></HEAD><BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#800080"><A NAME="I0"></A><H2>Visual Basic in 12 Easy Lessons velp12.htm</H2><P ALIGN=LEFT><A HREF="vel24.htm" TARGET="_self"><IMG SRC="purprev.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Previous Page"></A><A HREF="index.htm" TARGET="_self"><IMG SRC="purtoc.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="TOC"></A><A HREF="velxa.htm" TARGET="_self"><IMG SRC="purnext.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Next Page"></A><HR ALIGN=CENTER><P><UL><UL><UL><LI><A HREF="#E68E185" >Stop & Type</A><UL><LI><A HREF="#E69E169" >The Program's Description</A><LI><A HREF="#E69E170" >Studying the Grid</A><LI><A HREF="#E69E171" >Descriptions</A><LI><A HREF="#E69E172" >Using the Command Buttons and Scroll Bars</A><LI><A HREF="#E69E173" >Description</A><LI><A HREF="#E69E174" >Close the Application</A></UL></UL></UL></UL><HR ALIGN=CENTER><A NAME="E66E36"></A><H1 ALIGN=CENTER><CENTER><FONT SIZE=6 COLOR="#FF0000"><B>Project 12</B></FONT></CENTER></H1><BR><A NAME="E67E39"></A><H2 ALIGN=CENTER><CENTER><FONT SIZE=6 COLOR="#FF0000"><B>Getting Exotic</B></FONT></CENTER></H2><BR><BR><A NAME="E68E185"></A><H3 ALIGN=CENTER><CENTER><FONT SIZE=5 COLOR="#FF0000"><B>Stop & Type</B></FONT></CENTER></H3><BR><P>This lesson taught you how to use the scroll bars and the grid control, to manage the mouse, and to use Visual Basic's integrated debugger. The scroll bars give users a flexible control for entering and selecting from a range of data values. The grid enables the user to see tables of information in a spreadsheet-like format. When you need to monitor the user's mouse movements, in addition to the built-in monitoring that Visual Basic performs for all control clicking with the mouse, you can write code that responds to the user's mouse movements and clicks.<BR><P>The debugger is an integrated development tool that helps you hunt down errors that appear in your code. The debugger allows you to single step through a program's code, monitor variables, and inquire into a program's data values at any point in the running of the program.<BR><P>In this lesson, you saw the following:<BR><UL><LI>How to place and monitor scroll bar controls<BR><BR><LI>How to set up a grid for tables of data<BR><BR><LI>How to determine selected cells within a grid<BR><BR><LI>How to monitor the user's mouse movements<BR><BR><LI>How to use Visual Basic's integrated debugger to remove obscure bugs in your programs<BR><BR></UL><BR><A NAME="E69E169"></A><H4 ALIGN=CENTER><CENTER><FONT SIZE=4 COLOR="#FF0000"><B>The Program's Description</B></FONT></CENTER></H4><BR><P>Figure P12.1 shows the PROJEC12.MAK opening Form window. Much of this project comes from a description of a scroll bar application found in the first unit of this lesson. As Figure P12.1 shows, the user first sees a formatted table of values stored in a grid.<BR><P><B> <A HREF="P12vel01.gif">Figure P12.1. Project 12's application begins with a simple form.</A></B><BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><B>Warning</B><FONT COLOR="#FF8000"><B><I>: </I></B></FONT>You must make sure that the GRID.VBX customer control file for the grid control appears in your Visual Basic's directory. See <A HREF="vel23.htm">Unit 23</A> for details on adding this file.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>The application demonstrates how to use, change, and monitor the user's use of a grid control as well as how you can use scroll bars to change the behavior of other controls on the form.<BR><BR><A NAME="E69E170"></A><H4 ALIGN=CENTER><CENTER><FONT SIZE=4 COLOR="#FF0000"><B>Studying the Grid</B></FONT></CENTER></H4><BR><P>The grid represents the lawn fertilizer's application price table for the 8 customer's 5 lawn applications. As described in <A HREF="vel23.htm">Unit 23</A>, this lawn care company just began and needs to make 40 service calls that year. As the company adds more customers, the grid can easily be expanded by changing the grid's property values and adding the new values in the code. Once the number of customers grew by just a few more, the lawn service would want to incorporate the disk drive and store the current pricing table on the disk drive for easy maintenance and for use by other programs such as an invoicing program.<BR><P>Try this: Click the grid's scroll bars to scroll around the pricing table looking at the 40 prices. Before worrying about the command buttons and horizontal scroll bars at the right of the screen, study listing P12.1 for a glimpse of the overall program's format.<BR><P><FONT COLOR="#000080"><B>Listing P12.1. The </B><B>Form_Load()</B><B> procedure that controls the flow of the initial code.</B></FONT><BR><PRE><FONT COLOR="#000080">1: Sub Form_Load ()2: ' Defines the justification of the cells3: ' as well as assigns cell titles to the4: ' fixed row and columns and assigns initial5: ' price values to the 40 cells.6: Call InitScrolls ' Initialize scroll bars7: Call CenterCells ' Center all cell alignments8: Call SizeCells ' Specify width of cells9: Call Titles ' Initialize column and row titles10: Call FillCells ' Fill cells with values11: End Sub</FONT></PRE><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE>6: Break long modules into shorter ones.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE><I>Definition: Structured programs</I> are programs broken into several small sections of code.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P><FONT COLOR="#FF8000"><B><I>Analysis: </I></B></FONT>The code in Listing P12.1 demonstrates a programming approach called <I>structured programming</I> that you should incorporate into your own programs. Form_Load() isn't one huge event procedure because the Call statements in lines 6 through 10 help break up the code into smaller and more manageable code routines. All of the called code executes when the Form_Load() event procedure runs (right before the user sees the form on the screen), but the five subroutines provide a way for you to break the program into sections that let you zero in later on parts that you need to modify.<BR><P>Suppose that you need to update the pricing information. If you study only the Form_Load() procedure, you can see that the FillCells subroutine fills the grid's cells with values. Therefore, you could go straight to that subroutine if you needed to change the prices.<BR><P>Listing P12.2 contains the code for the routines called by the Form_Load() procedure. Study the code to familiarize yourself with your own grid's initialization requirements.<BR><P><B>Warning: </B>Not all of the code for the FillCells and Titles subroutines is listed in Listing P12.2 because the code is lengthy and repetitive. The long string of assignment statements helps to show how loading and saving the pricing information from and to the disk would be helpful once the company added many more customers. You don't want to look through and modify long strings of assignments every time that a price needs updating, you get a new customer, or you lose a customer. Using the disk for pricing, however, would probably require at least one additional program that enables you to update the disk price information directly.<P><FONT COLOR="#000080"><B>Listing P12.2. The code that the </B><B>Form_Load()</B><B> procedure calls to initialize the grid and other controls.</B></FONT><BR><PRE><FONT COLOR="#000080">1: Sub InitScrolls ()2: ' Set both scroll bars to their maximum values3: hscIncrease.Value = 154: hscDecrease.Value = 155: End Sub6:7: Sub CenterCells ()8: ' Sets the justification of the grid's9: ' cells to center alignment10: Dim Apps As Integer ' 5 applications yearly11: ' Center all values in the grid12: For Apps = 0 To 5 ' 5 applications13: grdLawn.Col = Apps ' Locate next column14: ' Center the fixed cells in this column15: grdLawn.FixedAlignment(Apps) = 216: Next Apps17: For Apps = 1 To 5 ' 5 applications18: ' Center the non-fixed cells in this column19: grdLawn.ColAlignment(Apps) = 220: Next Apps21: End Sub22:23: Sub SizeCells ()24: ' Specify the width of each cell25: Dim Apps As Integer ' 5 Application26: For Apps = 0 To 527: grdLawn.ColWidth(Apps) = 1100 ' Twips28: Next Apps29: End Sub30:31: Sub Titles ()32: ' Fill in the column titles33: grdLawn.Row = 0 ' All titles are in row #034: grdLawn.Col = 135: grdLawn.Text = "Pre-Emerge"36: grdLawn.Col = 237: grdLawn.Text = "Deep"38: ' Rest of column titles are filled here39: ' Fill in the row titles40: grdLawn.Col = 0 ' Customer titles in first column41: grdLawn.Row = 142: grdLawn.Text = "Jones"43: grdLawn.Row = 244: grdLawn.Text = "Smith"45: ' Rest of row titles are filled here46: End Sub47:48: Sub FillCells ()49: ' Fill in all 40 cells one at a time.50: ' This is tedious!51: '52: ' Normally, you would fill these cells from53: ' a disk file or through calculations.54: grdLawn.Row = 155: grdLawn.Col = 156: grdLawn.Text = 43.1657: grdLawn.Col = 258: grdLawn.Text = 41.5659: grdLawn.Col = 360: grdLawn.Text = 34.5761: grdLawn.Col = 462: grdLawn.Text = 27.4963: grdLawn.Col = 564: grdLawn.Text = 41.3465: grdLawn.Row = 266: grdLawn.Col = 167: grdLawn.Text = 43.5668: ' Rest of cells filled here69: End Sub</FONT></PRE><BR><A NAME="E69E171"></A><H4 ALIGN=CENTER><CENTER><FONT SIZE=4 COLOR="#FF0000"><B>Descriptions</B></FONT></CENTER></H4><BR><P>1: The code for the subroutine procedure that sets the scroll bars to their initial maximum values.<BR><P>2: A remark that explains the procedure.<BR><P>3: Set the Increase scroll bar to its maximum value of 15, which represents a 15 percent increase when the user clicks the Increase command button.<BR><P>4: Set the Decrease scroll bar to its maximum value of 15, which represents a 15 percent decrease when the user clicks the Decrease command button.<BR><P>5: Terminate the subroutine.<BR><P>6: A blank line to separate the procedures.<BR><P>7: The code for the subroutine procedure that sets the alignment in all the grid's cells to centered justification.<BR><P>8: A remark explains the procedure.<BR><P>9: The remark continues.<BR><P>10: Define a variable used to loop through the number of applications.<BR><P>11: A remark explains the code.<BR><P>12: Step through all of the table's six columns (the first column, column 0, holds the row titles).<BR><P>13: Set the column number to the For loop's value.<BR><P>14: A remark explains the code.<BR><P>15: Center (the value of 2 sets the property to 2-Center) the alignment property of all the fixed rows in the selected column. Only the top row is fixed in the last five columns.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR><NOTE>15: Visual Basic right-justifies cell values if you don't change the justification.</NOTE><BR><HR ALIGN=CENTER></BLOCKQUOTE></BLOCKQUOTE><P>16: Continue the loop.<BR><P>17: Step through the table's last five columns (these contain the nonfixed columns) to prepare for centering.<BR><P>18: A remark explains the code.<BR><P>19: Center (the value of 2 sets the property to 2-Center) the alignment property of all the nonfixed rows in the selected column.<BR><P>20: Continue the loop.<BR><P>21: Terminate the subroutine.<BR><P>22: A blank line to separate the procedures.<BR><P>23: The code for the subroutine procedure that sets the width of the cells.<BR><P>24: A remark explains the procedure.<BR><P>25: Define a variable used to loop through the number of applications.<BR><P>26: Step through each column, including the first fixed column 0.<BR><P>27: Set every cell to 1,100 twips wide.<BR><BLOCKQUOTE><BLOCKQUOTE><HR ALIGN=CENTER><BR>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -