1 / 21

Introduction to Visual Basic: Writing Windows Applications with Visual Basic

Learn the basics of Visual Basic programming and how to create windows applications using the Windows graphical user interface (GUI). This program demonstrates the process of creating forms, entering data, displaying output, and writing procedures. Understand the object model, syntax, and versions of Visual Basic, and learn how to write your first project.

lshaw
Download Presentation

Introduction to Visual Basic: Writing Windows Applications with Visual Basic

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to Visual Basic Writing Windows applications with Visual Basic Figure 1.1 This program works as follows: (These operations can be performed in any order). 1. Type your name in the top text box. 2. Type a message in the lower text box. 3. Click on one of the Option buttons under Color. 4. Click on one or more of the Check buttons under Style. 4. Click on Display Your name and message will appear in the smaller box next to the image, in the selected color and style.)

  2. The Windows graphical user interface (GUI) In a Visual Basic program, the programmer creates: - forms (windows) containing controls (buttons, boxes) - data is entered - output appears - procedures (chunks of code) for parts of the form Example Fig. 1.1 Display has a procedure to display the name and message. Green has a procedure to change the name and message to green. Programming languages - procedural, object-oriented, and event-driven Older languages (Pascal, FORTRAN, etc.) are procedural languages. For example, input was entered in a fixed order line by line according to prompts (“Enter name”, “Enter style”, etc.)

  3. In Visual Basic, the user can enter data in any desired order. Example Click on the Color or Style buttons at any time. Visual Basic programs are event-driven; they respond to user actions (events). Some events are internal. Example Event - the user clicks on a button. The object model An object consists of - data (which has various properties) - procedures acting on the data (methods) Example Fig. 1.1 Button Italic has - property checked (True or False) - a method which italicizes the letters in some box

  4. Syntax (grammar) for property is name of object . property Example optGreen.checked Syntax for method is name of object . method Example Printer.Print (sends output to the Printer) Versions of Visual Basic We use Version 5.00. Writing Visual Basic projects The three-step process 1. Define the user interface (define the objects). 2. Set the properties (describe the objects). 3. Write the Basic code. PLAN BEFORE CODING !!!!!!!!!!!!!!!!!!!!

  5. The Visual Basic Environment TheVisual Basic window (Fig. 1.2) contains several windows - form window (center of the screen) - project window (on the right) - properties window (lower right) - toolbox (left) (Fig. 1.3) Note: the toolbox on your screen may not contain all the icons shown on the text. - toolbar (top) (Fig. 1.4) Form location and size information are located at the top right of the Visual Basic window. Help is on the right of the toolbar.

  6. Miscellaneous - design time is the time spent writing the program - run time is the time when the program runs - break time is the time when the program stops because - an error has occurred, or - the user has suspended execution Writing your first Visual Basic project Create the following form (Fig. 1.5) Hello World by J. C. Bradley Hello World Push Me Exit

  7. When you click on “Push Me”, the “Hello World” appears. Set up your Visual Basic workspace A. Create a folder within which you will save your project. (Refer to the handout.) Note: You must create this folder beforeyou save your project. B. Launch Visual Basic (refer to the handout) C. Set up your workspace (refer to the handout) Design the user interface (Fig. 1.9) A. Design the form in complete detail on paper! B. Set up the form in Visual Basic (set size and position)

  8. C. Place controls on the form (i) Place the “Hello World” label on the form - from the toolbox select the label icon (the cursor becomes a cross) (Fig. 1.10) - click where you want the label (Fig. 1.11) - drag the cursor to draw a box (Fig. 1.12) (Note: these operations reserve space for the “Hello World” space. You will put the words “Hello World” on the form later. If you wish, you can resize and move the control.) (ii) Place the “Push Me” and “Exit” buttons on the form (Fig. 1.13 - 1.16) - from the toolbox select Command button - draw a command button as you drew a label, or double-click on the Command button

  9. Note: double-clicking gives buttons of the same size. - move command buttons to the desired positions - lock the controls (to keep from moving them) - click on Format (top of page) - select Lock controls - to unlock controls, do the same (iii) Set the name and caption properties for the label (Fig. 1.17 - 1.21) - click onceon the label - click on the title bar of the Properties window - scroll down to the Name property and type the word lblMessage The word “lblMessage” now appears at the top of the Properties window.

  10. - scroll down to the Caption property and delete the word Label1 This deletes the word “Label1” from the form. When the program runs, the message “Hello World” will appear here. (iv) Set the Name and Caption properties for the first command button (Fig. 1.22) - click on the Command1 button - change the Name property to cmdPush - change the Caption property to Push Me (v) Set the Name and Caption properties for the second Command button - change the Name property to cmdExit - change the Caption property to Exit

  11. (vi) Set the Caption property for the form to the phrase Hello World by your name (Fig. 1.23) Visual Basic code statements Remark statements, comments (MOST IMPORTANT!!!) - three most important features of a program - correctness - READABILITY!!!! - for others - FOR YOURSELF!!! - efficiency (usually not important except for graphics - comments describe what the code does - general description at the beginning - purpose of the module (chunk of code) - name of programmer (important for a team of programmers)

  12. - date written and/or modified - other statements if necessary to describe how the code works - comments begin with a single quote - comments can appear on the same line as other statements - Example Book p. 20 Assignment statements - assignments place a value into a variable - general form is object . property = value -Example Book p. 20 - lblMessage.Caption = “Hello” - lblTitle.FontSize = 12 - literals (messages, names, etc) are enclosed in “ “ - numbers and True or False are not

  13. The End statement - this statement stops a program - usually End is placed in the Exit module Code the event procedures for Hello World A. Code the click event for “Push Me” (Fig. 1.25) - double-click on the “Push Me” button You will see a new window with part of the code for the appropriate procedure - hit the tab key and type ‘Display the Hello World message - hit the Enter key; the comment turns green - hit the Enter key, hit the Tab key, and type lblMessage.Caption = “Hello World” - close the code window, if you wish

  14. B. Code the click event for the “Exit” button (Fig. 1.26) - add the comment ‘Exit the program - skip one line - type End Run the project (Fig. 1.27, 1.28) A. Open Run and select Start (There are also other ways to run programs.) B. Click on “Push Me”. C. Click on “Exit”. Save the project (Fig. 1.29) A. Under File select Save. (Fig. 1.30- 1.32) B. Move to the correct folder. C. Type in a file name. D. Hit Enter or click Save.

  15. Open the project (Note: this screen is different from Fig. 1.32) We assume that Visual Basic is not open. Under File choose Open Select the tab that says Existing Click on the arrow next to the folder name near the top of the window Click on Floppy A Open the folder Open the project If the form does not show up on the screen - in the Properties window open the Form folder - click on the name of the form

  16. Modify the project 1. Change the size and alignment of the message - make sure the controls are not locked - enlarge the label - click on the label - widen the label on both ends - change the font - select Font property in the Properties box (Fig. 1.33) - click on the dialogue box - select 12 point or larger -select Alignment and select 2-Center 2. Add a new label for your name - create a new label on the form - change the Caption to read “by <your name>“ 3. Change the Push Me button - select Push Me - change the Caption to English

  17. 4. Add a Spanish button - create a new command button - change the Name to cmdSpanish -change the Caption to Spanish 5. Add a Print button 6. Lock the controls 7. Add code for the Spanish button - double-click on the Spanish button - type in ‘ Display the Hola Mundo Message lblMessage.Caption = “Hola Mundo” 8. Add code for the Print button - double-click on the Print button - type in ‘Print the form PrintForm 9. Save and run the project 10. Add general remarks in the general declarations section name of project, name of programmer, date, purpose

  18. Explore the code window (Fig. 1.37, 1.38) - go to the general section - go to the cmdSpanish section, etc. Print the project - under File select Print - click the appropriate boxes (Fig, 1.39) - form image prints a picture of the form (Fig 1.40) - form as text prints a verbal description of the form - code prints the code Sample printout P. 32-35 Finding and fixing errors Compile errors Before a program runs, it must be translated into machine code (0’s and 1’s) which is all the machine can directly handle.

  19. Definition A compiler is a program that translates your program into machine language. The process of translating a program into machine language is called compiling. Errors that prevent the program from compiling are called compile errors, or compile-time errors. Example If you run the program and get a message such as “Method not available”, then this is a compile-time error. Run-time errors Definition If the program runs and then stops unexpectedly, then the error which causes it to stop is called a run-time error. Example At some point, you compute the value A/B, and you set B = 0.

  20. Logic errors Definition A logic error is an error which does not prevent the program from running but which causes an incorrect value to be returned. Example You compute weekly wage as wage * 400 instead of wage * 40. Visual Basic highlights compile-time and run-time errors as the program is running. You can fix them and continue running the program by selecting Restart (under Run). Project debugging (error correcting) DefinitionTesting a program is finding errors. NOTE: If you run a program and find no errors, then your test is UNSUCCESSFUL!!!

  21. Naming conventions - names for controls - Names for labels begin with lbl - Names for command buttons begin with cmd - Names for forms begin with frm - names must not be Command1, Label1, etc. - names must not be numbers Visual Basic files When you save your project, you get - a project file (name . VBP) - a code file (name . BAS) - a form file (name . FRM) - a custom control file (name . VBX) Read: Summary, Key terms, Review questions

More Related