
Transcription
introductionLesson1 :Introduction1.1 A brief description of Visual BasicVISUAL BASIC is a high level programming language evolved from the earlier DOS version called BASIC.BASIC means Beginners' Allpurpose Symbolic Instruction Code. It is a fairly easy programming language tolearn. The codes look a bit like English Language. Different software companies produced different version ofBASIC, such as Microsoft QBASIC, QUICKBASIC, GWBASIC ,IBM BASICA and so on.VISUAL BASIC is a VISUAL and events driven Programming Language.These are the main divergence fromthe old BASIC. In BASIC, programming is done in a text-only environment and the prgram is executedsequentially. In VISUAL BASIC, programming is done in a graphical environment. Because users may click ona certain object randomly, so each object has to be programmed indepently to be able to response to thoseactions(events).Therefore, a VISUAL BASIC Program is made up of many subprograms, each has its ownprogram codes, and each can be excecuted indepently and at the same time each can be linked together in oneway or another.1.2 The Visual Basic EnvironmentOn start up, Visual Basic 6.0 will display the following dialog box as shown in figure 1.1.You can choose to start a new project, open an existing project or select a list of recently opened programs. Aproject is a collection of files that make up your application. There are various types of applications we couldcreate, however, we shall concentrate on creating Standard EXE programs(EXE means executable program).Now, click on the Standard EXE icon to go into the actual VB programming environment.Figure 1.1 The Visual Basic Start-up Dialog Boxhttp://www.vbtutor.net/lesson1.html (1 of 3) [2/23/2003 1:56:16 PM]
introductionIn figure 1.2, the Visual Basic Enviroment consists of the The Blank Form window which you can design your application's interface.The Project window displays the files that are created in your application.The Properties window which displays the properties of various controls and objects that are created inyour applications.It also includes a Toolbox that consists of all the controls essential for developing a VB Application. Controls aretools such as boxes, buttons, labels and other objects draw on a form to get input or display output. They alsoadd visual appeal.Figure 1.2: The Visual Basic Enviromenthttp://www.vbtutor.net/lesson1.html (2 of 3) [2/23/2003 1:56:16 PM]
introduction[Back to contents page]http://www.vbtutor.net/lesson1.html (3 of 3) [2/23/2003 1:56:16 PM]
VB lesson2Lesson 2: Building a Visual Basic Application2.1 Creating Your First ApplicationIn this section, we are not going into the technical aspect of VB programming, just have a feel of it. Now,you can try out the examples below:Example 2.1.1 is a simple program . First of all, you have to launch Microsoft Visual Basic. Normally, adefault form Form1 will be available for you to start your new project. Now, double click on form1, thesource code window for form1 will appear. Don't worry about the begining and the end statements(i.ePrivate Sub Form Load.End Sub.); Just key in the lines in between the above two statementsexactly as are shown here.When you run the program, you will be surprise that nothing shown up.Inorder to display the output of the program, you have to add the Form1.show statement like in Example21.2 and Example 2.1.3. Try them out.Example 2.1.1Example 2.1.2Example 2.1.3Private Sub Form LoadPrivate Sub Form LoadPrivate Sub Form LoadFor i 1 to 5print "Hello"next iForm1.showFor i 1 to 5print "Hello"next iForm1.showFor i 1 to10print inext iEnd SubEnd SubEnd Sub2.2 Steps in Building a Visual Basic ApplicationStep 1 Draw the interfaceStep 2 Set PropertiesStep 3 Write the events codeExample 2.1This program is a simple program that calculate the volume of a cylinder. Let design the interface:http://www.vbtutor.net/lesson2.html (1 of 4) [2/23/2003 1:56:24 PM]
VB lesson2First of all, go to the properties window and change the form caption to Volume Of Cylinder. Then drawthree label boxes and change their captions to Base Radius, height andvolume respectively. After that,draw three Text Boxes and clear its text contents so that you get three empty boxes. Named the textboxes asradius ,hght(we cannot use height as it is the built-in control name of VB)and volumerespectively. Lastly, insert a command button and change its caption toO.K. and its name to OK. Nowsave the project as cylinder.vbp and the form as cylinder.vbp as well. We shall leave out the codes at themoment which you shall learn it in lesson3.Example 2.2Designing an attractive and user friendly interface should be the first step in constructing a VB program.To illustrate, let's look at the calculator program.http://www.vbtutor.net/lesson2.html (2 of 4) [2/23/2003 1:56:24 PM]
VB lesson2 Now, please follow the following steps to design the calculator interface. Resize the form until you get the size you are satisfed with.Go to the properties window and change the default caption to the caption you want , such as 32Calculator-----Designed by Vkliew.Change other properties of the form, such as background color, foreground color , border style.Irecommend you set the following properties for Form1 for this calculator program:BorderStyleMaxButtonminButtonFixed SingleFalseTrueThese properties will ensure that the users cannot resize or maximize your calculatorwindow, but able to minimize the window. Draw the Display Panel by clicking on the Label button and and place your mouse on the form.Start drawing by pressing down your mouse button and drag it along.Click on the panel and the corresponding properties window will appear. Clear the default label sohttp://www.vbtutor.net/lesson2.html (3 of 4) [2/23/2003 1:56:24 PM]
VB lesson2 that the caption is blank(because the display panel is supposed to show the number as we click onthe number button). It is good to set the background color to a bright color while the foregroundcolor should be something like black.(for easy viewing). Change the name to display as I amgoing to use it later to write codes for the calculator.Now draw the command buttons that are necessary to operate a calculator. I suggest you followexactly what is shown in the image above.Test run the project by pressing F5. If you are satisfied with the appearance, go ahead to save theproject. At the same time, you should also save the file that contain your form.Now, I know you are very keen to know how to write the code so that the calculator is working.Please refer to my sample VB programs for the source codes.[Back to contents page]http://www.vbtutor.net/lesson2.html (4 of 4) [2/23/2003 1:56:24 PM]
VB lesson3Lesson 3 : Writing the CodesNow we shall attempt to write the codes for the cylinder program.Now, doubleclick on the O.K button and enter the codes between Private Sub OK Click( ) and End SubPrivate Sub OK Click( )r Val(radius.Text)h Val(hght.Text)pi 22 / 7v pi * (r 2) * hvolume.Text Str (v)End Subwhen you run the program , you should be able to see the interface as shown above. if you enter a valueeach in the radius box and the height box, then click OK, the value of of the Volume will be displayed inthe volume box.I shall attempt to explain the above source program to newcomers in Visual Basic( If you are a veteran,you can skip this part) . Let me describe the steps using pseudocodes as follows:Procedure for clicking the OK button to calculate the volume of cylinderget the value of r from the radius text boxget the value of h from the height text boxassign a constant value 22/7 to pihttp://www.vbtutor.net/lesson3.html (1 of 2) [2/23/2003 1:56:29 PM]
VB lesson3calculate the volume using formulaoutput the results to the Volume text boxEnd of ProcedureThe syntax radius.Text consists of two parts, radius is the name of text box while Text is the textualcontents of the text box. Generally, the syntax is: Object.PropertyIn our example, the objects are radius, hght and volume, each having text as their property.Object andproperty is separated by a period(or dot).The contents of a text box can only be displayed in textual form,or in programming term,as string. To convert the contents of a text box to a numeric value so thatmathematical operations can be performed , you have to use the function Val. Finally, In order to displaythe results in a text box, we have to perform the reverse procedure, that is, to convert the numeric valueback to the textual form, using the function Str .I shall also explain the syntax that defines the sub procedure Private Sub OK click. Private Sub heremeans that the parameters , values and formulas that are used here belong only to the OKsubprocedure(an object by itself).They cannot be used by other sub procedures or modules. OK Clickdefines what kind of action the subprocedure OK will response .Here, the action is mouse click. Thereare other kind of actions like keypress, keyup, keydown and etc that I am going to due with in otherlessons.[Back to contents page]http://www.vbtutor.net/lesson3.html (2 of 2) [2/23/2003 1:56:29 PM]
Visual Basic Tutorial Lesson 4Lesson 4-Working With ControlsBefore writing an event procedure for a control to response to a user's action, you have to set certainproperties for the control to determine its appearance and how it will work with the event procedure. Youcan set the properties of the controls in the properties windows. I am not going into the details on how toset the properties. However, I would like to stress a few important points about setting up the properties. You should set the Caption Property of a control clearly so that a user know what to do withthat command. For example, in the calculator program, all the captions of the command buttonssuch as , - , MC ,MR are commonly found in an ordinary calculator, a user should have noproblem in manipulating the buttons.You should set a meaningful name for the Name Property because it is easier for you to writeand read the event procedure and easier to debug your program later.Another property that is important is whether you want your control to be visible or not at startup.This property can only set to be true or false.One more important property is whether the control is enabled or not.[Back to contents page]http://www.vbtutor.net/lesson4.html [2/23/2003 1:56:35 PM]
lesson5Lesson 5 : Managing Visual Basic DataThere are many types of data we come across in our daily life. For example, we need to handle data suchas names, adresses, money, date, stock quotes, statistics and etc everyday. Similarly In Visual Basic, weare also going to deal with these kinds of data. However, to be more systematic, VB divides data intodifferent types.5.1 Types of Visual Basic Data5.1.1 Numeric DataNumeric data are data that consists of numbers, which can be computed mathematically with variousstandard operators such as add, minus, multiply, divide and so on. In Visual Basic, the numeric data aredivided into 7 types, they are summarised in Table 6.1Table 5.1: Numeric Data TypesTypeByteIntegerLongStorage1 byte2 bytes4 bytesRange of Values0 to 255-32,768 to 32,767-2,147,483,648 to 2,147,483,648-3.402823E 38 to -1.401298E-45 for negative valuesSingle4 bytes1.401298E-45 to 3.402823E 38 for positive values.-1.79769313486232e 308 to -4.94065645841247E-324 for negative valuesDouble 8 bytes4.94065645841247E-324 to 1.79769313486232e 308 for positive values.Currency 8 bytes -922,337,203,685,477.5808 to 922,337,203,685,477.5807 /- 79,228,162,514,264,337,593,543,950,335 if no decimal is useDecimal 12 bytes /- 7.9228162514264337593543950335 (28 decimal places).5.1.2 Non-numeric Data Typesthe nonnumeric data types are summarised in Table 5.2Table 5.2: Nonnumeric Data Typeshttp://www.vbtutor.net/lesson5.html (1 of 4) [2/23/2003 1:56:40 PM]
lesson5Data TypeString(fixed length)String(variable xt)StorageLength of stringLength 10 bytes8 bytes2 bytes4 bytes16 bytesLength 22 bytesRange1 to 65,400 characters0 to 2 billion charactersJanuary 1, 100 to December 31, 9999True or FalseAny embedded objectAny value as large as DoubleSame as variable-length string5.1.3 Suffixes for LiteralsLiterals are values that you assign to a data. In some cases, we need to add a suffix behind a literal so thatVB can handle the calculation more accurately. For example, we can use num 1.3089# for a Double typedata. Some of the suffixes are displayed in Table 5.3.Table 5.3Suffix&!#@Data TypeLongSingleDoubleCurrencyIn additon, we need to enclose string literals within two quotations and date and time literals within two #sign. Strings can contain any characters, including numbers. The following are few examples:memberName "Turban, John."TelNumber "1800-900-888-777"LastDay #31-Dec-00#ExpTime #12:00 am#5.2 Managing VariablesVariables are like mail boxes in the post office. The contents of the variables changes every now andthen, just like the mail boxes. In term of VB, variables are areas allocated by the computer memory tohold data. Like the mail boxes, each variable must be given a name. To name a variable in Visual Basic,http://www.vbtutor.net/lesson5.html (2 of 4) [2/23/2003 1:56:40 PM]
lesson5you have to follow a set of rules.5.2.1 Variable NamesThe following are the rules when naming the variables in Visual Basic It must be less than 255 charactersNo spacing is allowedIt must not begin with a numberPeriod is not permittedExamples of valid and invalid variable names are displayed in Table 5.4Table 5.4Valid NameMy CarThisYearLong Name Can beUSEInvalid NameMy.Car1NewBoyHe&HisFather*& is not acceptable5.2.2 Declaring VariablesIn Visual Basic, one needs to declare the variables before using them by assigning names and data types.They are normally declared in the genaral section of the codes' windows using the Dim statement.The format is as follows:Dim variableNmae as DataTypeExample 5.1Dim password A
BASIC, such as Microsoft QBASIC, QUICKBASIC, GWBASIC ,IBM BASICA and so on. VISUAL BASIC is a VISUAL and events driven Programming Language.These are the main divergence from the old BASIC. In BASIC, programming is done in a text-only environment and the prgram is executed sequentially. In VISUAL BASIC, programming is done in a graphical environment. Because users