1 / 39

Introduction to Visual Basic

Introduction to Visual Basic. laura leventhal and lisa weihl. Dinosaurs?. The COBOL Programmer

Télécharger la présentation

Introduction to 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 laura leventhal and lisa weihl

  2. Dinosaurs? • The COBOL Programmer • There was once a COBOL programmer in the mid to late 1990s. For the sake of this story, we'll call him Jack. After years of being taken for granted and treated as a technological dinosaur by all the UNIX programmers and Client/Server programmers and web site developers, Jack was finally getting some respect. • He'd become a private consultant specializing in Year 2000 conversions. He was working short-term assignments for prestige companies, traveling all over the world on different assignments. He was working 70 and 80 and even 90 hour weeks, but it was worth it.

  3. More COBOL • Several years of this relentless, mind-numbing work had taken its toll on Jack. He had problems sleeping and began having anxiety dreams about the Year 2000. It had reached a point where even the thought of the year 2000 made him nearly violent. He must have suffered some sort of breakdown, because all he could think about was how he could avoid the year 2000 and all that came with it. • He was put into his cryogenic receptacle, the technicians set the revive date, he was given injections to slow his heartbeat to a bare minimum, and that was that. • The next thing that Jack saw was an enormous and very modern room filled with excited people. They were all shouting "I can't believe it!" and "It's a miracle" and "He's alive!". There were cameras (unlike any he'd ever seen) and equipment that looked like it came out of a science fiction movie.

  4. More COBOL • Someone who was obviously a spokesperson for the group stepped forward. Jack couldn't contain his enthusiasm. "It is over?" he asked. "Is 2000 already here? Are all the millennial parties and promotions and crises all over and done with?" • The spokesman explained that there had been a problem with the programming of the timer on Jack's cryogenic receptacle, it hadn't been year 2000 compliant. It was actually eight thousand years later, not the year 2000. But the spokesman told Jack that he shouldn't get excited; someone important wanted to speak to him. • Suddenly a wall-sized projection screen displayed the image of a man that looked very much like Bill Gates. This man was Prime Minister of Earth. He told Jack not to be upset. That this was a wonderful time to be alive.

  5. More COBOL • That there was world peace and no more starvation. That the space program had been reinstated and there were colonies on the moon and on Mars. That technology had advanced to such a degree that everyone had virtual reality interfaces which allowed them to contact anyone else on the planet, or to watch any entertainment, or to hear any music recorded anywhere. • "That sounds terrific," said Jack. "But I'm curious. Why is everybody so interested in me?" • "Well," said the Prime Minister. "The year 10000 is just around the corner, and it says in your files that you know COBOL".

  6. Main Points • Tools to build GUI’s • Parts of Visual Basic Programs/Projects • Parts of the VB environment • Basic Language

  7. Tools to Build GUI’s • Need tools that make it easier to design, evaluate and generate GUI’s • Contemporary examples: • Symantic café (generate Java) • RapidApp (generate C++) • Python/tk,Tcl/tk (scripting, freeware languages,available for many platforms, can produce applications for either desktops or web deliverable) • Java forte, Motif widget set

  8. Tools to build GUI’s • Event-driven vs. linear • High-level GUI objects as part of language • Support UI prototyping • Examples • Visual Basic • Hypercard/hypertalk

  9. Why VB? • Visual Basic is one of the world’s most popular programming languages • encapsulates the complexities of Windows Application Program Interface (API) into easily manipulated objects • VB is similar to the QBasic (the non-visual Basic language supplied with every version of MS-DOS 5.0 and above) • it contains important extensions that make it event-driven and more object oriented.

  10. Developing a UI in VB • Four general steps • Design UI (placement of buttons, labels...) • Create the UI in VB • place buttons and other control objects in a window • Set properties of objects • Write code to control how objects respond to user events

  11. Planning Event Procedures - General Steps for planning your code • Identify the controls around which the main events fo your program will take place. • Determine the specific events you expect to take place around these controls. • Write procedures that define your program’s action when any one of these events take place. • Note - visual basic has a powerful help facility to help you along.

  12. Visual Basic Environment • The VB environment is shown in Figure 1 of the tutorial. Find • Menubar • Toolbox window • Form window • Project window • Properties window • Explorer window

  13. VB Environment/Form window • forms • windows your application will use. If your application runs as a standalone application, users will see the forms as the application. • place control objects (buttons, text boxes...) on forms

  14. VB Environment/Properties • list of all of the properties that can be set for an object (eg. size, captions, color...) • objects with properties are any object on the form and the form itself.

  15. Properties - Example • Suppose that you write Shirt.color = “Blue” • In VB, this would mean that you have an object named Shirt. Shirt has a property, color. We changed the property to Blue. • Assumption is that “Blue” is valid value for the property. Environment will tell you if it is not.

  16. VBEnvironment/Toolbox • Toolbox • shown in Figure 2 of tutorial • palette of control objects. • choose objects from the toolbox for your form.

  17. Toolbox Controls - Text • Four controls for text: • Text box - for user input • Label - for displaying user output • List box - for listing choices • Combo box - pull down list box and text box

  18. Examples/Toolbox Controls • Demos • Text box: change name to txtBox, change text • Label: change caption to lblBox • List box and Combo box • Ways to add elements to list: VB code (additem), in properties window (property = list) • Usually initialize at LOAD with additem • Method: name.additem “value”

  19. Toolbox Controls - Containers • Containers are used to house or contain (like a bucket) a set of controls. There are two types: • Picture Box • Frame

  20. Toolbox Controls - Buttons • Buttons are controls that users click to turn them on or off. Clicking a button evokes an event. • Types • Option buttons • Check box • Command button • Image box

  21. VB Environment/Project Window • List of all files that are required for a VB application • If a program consists of multiple forms, they are listed in the project window. • Usage warning - do not simply copy project in file manager. Elements will be lost.

  22. Parts of a VB Project/Application • A project is made up of: • Forms • A window that contains visual elements. • Classes • Modules • special window with declarations and programming code. • Custom controls • visual objects like buttons that are add-ons.

  23. Example 3.4 • In this exercise, I will show a simple interface that has two command buttons and a text box to display output

  24. Methods • A method is a reserved keyword that acts on a particular object. • For example, Clearis a method to clear the contents of a control, such as a list box or combo box. • In VB, there are approximately 30 different methods.

  25. Methods - Example • Syntax object.method • Example - clear the contents of the list box named List1. • List1.Clear

  26. An Event • An event is an action recognized by an object. • remember the word action. Action might be a mouse click or pressing a keyboard key • While the value of a property and a method can be specified, an event typically requires a set of programmed instructions.

  27. A Statement • A statement is a reserved word that causes the VB program to do something • Examples • Kill (deletes a file from a disk) • Option Explicit (forces explicit declaration of all variables - a good idea!)

  28. VB Program Instructions • An instruction is a line of code in a VB application. • All instructions begin with a statement or a method. • Example • Form1.Move Left + (Width\10), Top + (Height\10) • Move is the method, Left, Width, Top and Height are properties and Form is the object

  29. Open the Code Window/Writing VB Instructions • To open the code window, double-click the active form. • This will open a window with a template for a subroutine. By definition, the name of the subroutine is the combination of the object and the specific event. • Use the names of the controls to see which events are defined for which controls (eg. Command buttons have “click”) • Example • Private Sub Form_Load • Private Sub Form_Click

  30. Coding Event Procedures for Controls • Adding a control to a from and setting the control properties set the scene for adding a procedure. • A control will do nothing in an event-oriented environment unless an event procedure is attached to it. • More than one event can be associated with a single control

  31. Control Naming Conventions • The property Name is typically the variable name of the control. • There is always a default name assigned to the form or control.. For example, the first command button added to a form is named Command1. • To change the name and follow convention: • Add a command button. Change the caption property (ie what the user sees to StartI. • Change the name property to • See table 3.1 for conventions

  32. A Few Coded Statements and Methods • See book and tutorial for more complete descriptions • Dim Statement • used to declare variables and allocate storage space. • eg. Dim variablename [As type] • Private • All control procedures have the Private keyword proceding them by default. • When defining classes in VB, just like in C++ it is important to determine which data members and member functions are public and which are private.

  33. Example 7.7

  34. Defining Variable and Procedure Scope • Variable scope is determined by • the placement of the variable declaration • keywords (Dim, Static, Private, Public) • Procedure scope determined by • keywords (Private, Public, Static)

  35. Variable Scope • Variables declared with Dim are local to the procedure. • Declaring variables with Public instead of Dim (same syntax) makes the variable global. • In order for variables/constants to be global, they must be declared in the Declarations section of a standard module. • Class data members should be private in general.

  36. Grand Finale In Class

  37. Project Management - File Menu • Open Project, Save Project, Save Project As • Use these to guarantee that your whole project is saved. • Add File - opens a dialog box that enables you to add .FRM (form), .BAS (coded module), and other files. Important when you use a file built for another project. • Remove File - removes the file from the current project that is active. • Save File • Save File As - Saves current file to disk. • Print • Print Setup • Make .EXE - creates an executable application.

  38. Other Stuff • Details about objects, properties and VB syntax are in the tutorial • In the lab, we will demo the maturity calculator

More Related