|
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
To access the contents, click the chapter and section titles.
Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6!
Other WindowsVisual Basic has a number of other windows you will use during program development. They are not shown in Figure 2.2 because Visual Basic displays these windows only when you need them. For the most part, well deal with these other windows as the need arises. One type of window, however, is so central to Visual Basic programming that Ill take a moment to introduce it now. In a code window, you enter and edit Basic code. It works pretty much like any other Windows text editor: You can type text, delete it, move and copy it from place to place, and so on. If you want to see what a code window looks like, click on the View Code button in the Project Explorer window. An empty code window will open.
A Visual Basic OverviewNow that youve had a look at the most important parts of Visual Basic, you may be wondering how they all fit together. With Visual Basic, more than with other programming tools, understanding how all the parts fit together is essential if you are going to realize its full potential. We went over the most important points in the previous chapter, but lets go over them again briefly before beginning your first Visual Basic project.
Your First Visual Basic ProgramAll right, enough talk. Its time to dive in and get your fingers dirty. Were going to create a real, live Visual Basic programand not just some silly demonstration program, but a real program capable of doing something useful. Well write a mortgage calculator that will display the monthly payment on a mortgage or other loan. It wont be the worlds fanciest program (not even close), but it is a good start to learning Visual Basic. You will create this program in four steps. These are the same steps you would use for any Visual Basic project. Step 1: Planning AheadProgramming projects always benefit from a bit of planning, and Visual Basic is no different. What will a mortgage calculator require? Put on your thinking cap, and get out your paper and pencil. (Yes, even in this computerized age, paper and pencil are still the ideal tools for some tasks.) A few moments of thought yields the conclusion that the program will need to do three things: Gather input information from the user, perform the calculations, and display the answer. Lets start with the input. We need three pieces of information to perform the calculations: the amount, or principal, of the loan; the interest rate being charged; and the duration, or term, of the loan. Right away, we know the program will need places for the user to enter these three items. One of Visual Basics controls, the Text Box, is intended for entering and displaying information of this sort. At the completion of this first planning step, we know the project will need three Text Box controls for input. As for output, our program will generate only one piece of informationthe monthly loan payment. Again, a Text Box control is ideal for this purpose, so well add one output Text Box to the three input Text Boxes, for a total of four. Note that these are all the same type of Text Box control; designating them as input and output reflects only the way the program will use them. We also need some way to identify the Text Box controls to determine which one is for the interest rate, which is for the loan term, and which is for the amount. The Label control is ideal for this purpose, and well need one for each input Text Box. Finally, we need some way for users to quit the program. Yes, we could let them quit by using the default window controls and clicking the X button in the upper right corner. For a little more elegance, lets provide a Quit button instead. Visual Basics Command Button control is suited for such a task, and well need only one. Our planning is now complete. Well, almost. We also need to know how to calculate the mortgage payment when given the amount, term, and rate. Fortunately, I happen to have a Handbook of Financial Formulas on my shelf. If we define the following: rate = interest rate per period amount = loan amount nper = term of loan in periods then: monthly payment = (amount * rate) / (1 - (1 + rate) ^ -nper) Because the formula is written in Basic, it may not be clear to you. Youll learn all about Basic-speak in later chapters. For now, all you need to know is that * means multiply, / means divide, and ^ means raised to the power of. In this formula, all items must be expressed in the same units. In other words, if you want to calculate monthly payments, you have to enter the monthly interest rate and the loan term in months. Okay, now the planning is really complete. Of course, your plan is not engraved in stone. The freedom to make changes later is one of the beauties of Visual Basic. But now, its time to get to work. Step 2: Designing The InterfaceThe next step in creating a Visual Basic program is usually the design of the visual interface. This means we are going to start with a blank form and place the needed controls on it. Because we were wise enough to plan ahead, we already know what controls will be on the form. All we need to do now is decide on the visual layoutwhere the controls are located, what size they are, and so on.
|
![]() |
Pro |