Introduction to Programming Using Visual Basic 6.0

Slides:



Advertisements
Similar presentations
Working with Intrinsic Controls and ActiveX Controls
Advertisements

VISUAL BASIC Visual Basic is derived from the Basic language (Beginner’s All-Purpose Symbolic Instruction Code) Visual Basic uses an event-driven programming.
Chapter 1: An Introduction to Visual Basic 2012
Presented by: Benefits Systems Support. Getting Started ê Open Powerpoint, create a blank presentation. ê Select a style for your first slide from the.
Office 2003 Post-Advanced Concepts and Techniques M i c r o s o f t Excel Project 7 Using Macros and Visual Basic for Applications (VBA) with Excel.
Using Macros and Visual Basic for Applications (VBA) with Excel
Understanding Microsoft Excel
Customizing Reports. Custom Reports A report is a formatted hardcopy of the contents of one or more tables from a database. Although you can format and.
® Microsoft Office 2010 Word Tutorial 1 Creating a Document.
Text Box controls are used when users are required to type some input (during program execution), or output is displayed on the form (known as the user-
1 Introduction to the Visual Studio.NET IDE Powerpoint slides modified from Deitel & Deitel.
To type the VB code behind the command button (named cmdPush), Double-Click on the Push Me (caption) command button As a result the Visual Basic Code Window.
Fundamentals of Programming in Visual Basic 3.1 Visual basic Objects Visual Basic programs display a Windows style screen (called a form) with boxes into.
Office 2003 Post-Advanced Concepts and Techniques M i c r o s o f t Word Project 8 Working with Macros and Visual Basic for Applications (VBA)
COMPREHENSIVE Excel Tutorial 8 Developing an Excel Application.
Visual Basic Chapter 1 Mr. Wangler.
Lesson 1 – Microsoft Excel The goal of this lesson is for students to successfully explore and describe the Excel window and to create a new worksheet.
05/09/ Introducing Visual Basic Sequence Programming.
Hello World In C++ and Microsoft Visual C++. Directions to begin a project 1. Go to All Programs 2. Open Visual Studio C++ 3. Click on New Project 4.
Project 9 Using Visual Basic for Applications (VBA) to Customize and Automate Excel Jason C. H. Chen, Ph.D. Professor of Management Information Systems.
Chapter 1 P. 1 Writing Windows applications with Visual Basic Figure 1.1 The first program works as follows: (These operations can be performed in any.
Ch 11: Userforms CP212 Winter Topics Designing User Forms o Controls Setting Properties o Tab Order o Testing Writing Event Handlers o Userform_Initialize.
PMS /134/182 HEX 0886B6 PMS /39/80 HEX 5E2750 PMS /168/180 HEX 00A8B4 PMS /190/40 HEX 66CC33 By Adrian Gardener Date 9 July 2012.
Introduction to Visual Basic. Quick Links Windows Application Programming Event-Driven Application Becoming familiar with VB Control Objects Saving and.
IE 411/511: Visual Programming for Industrial Applications
© 2006 Lawrenceville Press Slide 1 Chapter 3 Visual Basic Interface.
Chapter 3 Introducing Visual Basic.NET. 3.1 Visual Basic.NET Windows Programming -Used to create Windows, Web, and Console applications -Uses predefined.
CHAPTER TWO Creating Simple Visual Basic.NET Windows Applications.
XP 1 Microsoft Word 2002 Tutorial 1 – Creating a Document.
Basic Controls & Properties Chapter 2. Overview u VB-IDE u Basic Controls  Command Button  Label  Text Box  Picture Box u Program Editor  Setting.
Copyright © 2001 by Wiley. All rights reserved. Chapter 2: Using Visual Basic to Create a First Project Getting Started with VB Development Environment.
 Application – another name for a program.  Interface – is what appears on the screen when the application is running.  Program Code – is instructions.
Chapter One An Introduction to Visual Basic 2010 Programming with Microsoft Visual Basic th Edition.
Introduction to Visual Basic.NET Your First Visual Basic.NET Application.
Program Design and Coding
Microsoft Visual Basic 2012 CHAPTER THREE Program Design and Coding.
Microsoft Visual Basic 2010 CHAPTER THREE Program Design and Coding.
Working with the VB IDE. Running a Program u Clicking the”start” tool begins the program u The “break” tool pauses a program in mid-execution u The “end”
Chapter Two Creating a First Project in Visual Basic.
Chapter 2 P. 1 Introducing more controls (on the Toolbox) (Fig. 2.1) - Text box - Frame - Option button - Check box - Image Example P. 44 Figure 2.2 Message.
Chapter 2 – Introduction to the Visual Studio .NET IDE
1 Creating Windows GUIs with Visual Studio. 2 Creating the Project New Project Visual C++ Projects Windows Forms Application Give the Project a Name and.
Lesson 1 – Microsoft Excel * The goal of this lesson is for students to successfully explore and describe the Excel window and to create a new worksheet.
Visual Basic Programming Introduction VB is one of the High level language VB has evolved from the BASIC language. BASIC stands for Beginners All-purpose.
McGraw-Hill/Irwin Programming in Visual Basic 6.0 © 2002 The McGraw-Hill Companies, Inc. All rights reserved. Update Edition Chapter 2 More Controls.
Visual Basic for Application - Microsoft Access 2003 Programming applications using Objects.
1 Visual Basic Part I - A tool for customizing your program Principles of GIS
COMPUTER PROGRAMMING I 3.02 Apply Properties Associated with the Controls.
COMPUTER PROGRAMMING I 3.01 Apply Controls Associated With Visual Studio Form.
Introduction to Programming Using Visual Basic 6.0 Cheryl Klimack
IE 411/511: Visual Programming for Industrial Applications Lecture Notes #2 Introduction to the Visual Basic Express 2010 Integrated Development Environment.
Word and the Writing Process. To create a document 1.On the Start menu, point to Programs, and then click Microsoft Word. A new document opens in Normal.
COMPUTER PROGRAMMING I 3.01 Apply Controls Associated With Visual Studio Form.
 2002 Prentice Hall. All rights reserved. 1 Introduction to the Visual Studio.NET IDE Outline Introduction Visual Studio.NET Integrated Development Environment.
Microsoft Visual C# 2010 Fourth Edition Chapter 3 Using GUI Objects and the Visual Studio IDE.
MS WORD INFORMATION TECHNOLOGY MANAGEMENT SERVICE Training & Research Division.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 6 Looping and Multiple Forms.
Chapter 10 Using Macros, Controls and Visual Basic for Applications (VBA) with Excel Microsoft Excel 2013.
Excel Tutorial 8 Developing an Excel Application
Visual Basic.NET Windows Programming
Chapter 1: An Introduction to Visual Basic 2015
3.01 Apply Controls Associated With Visual Studio Form
3.01 Apply Controls Associated With Visual Studio Form
An Introduction to Computers and Visual Basic
CIS16 Application Development Programming with Visual Basic
Visual Basic Programming Chapter Four Notes Working with Variables, Constants, Data Types, and Expressions GROUPBOX CONTROL The _____________________________________.
Items, Group Boxes, Check Boxes & Radio Buttons
An Introduction to Computers and Visual Basic
Introduction to Programming
Presentation transcript:

Introduction to Programming Using Visual Basic 6.0 Cheryl Klimack 729-3900 klimack.cheryl@brandonsd.mb.ca

Why VB? Rationale… Easy for beginners to produce a working program Capable of high end programs Graphical OOP (for the most part) Affordable Many free resources available 2

The VB IDE View Object View Code Menu bar Project Explorer Window Tool bar Tool box PropertiesWindow Project Window Immediate Window 3

View Menu As in all Windows applications, simply choose View from the menu to view your different toolboxes, windows, etc. or to remove them from the IDE. 4

Objects Every VB app has at least one object – the form. Other objects are added at design time by selecting the object from the toolbox and drawing it on the form. A second way is to simply double-click the object and it will appear on the form. 5

Object Properties All objects should be named. Each object has a prefix that is standard in the VB coding world. For example, the prefix for a form is frm. See the VB help for a complete list. Objects have many properties that can be accessed at design time or at run time. VB uses dot notation to access properties…for example: lblMessage.Caption = “Welcome!” Property Object Property Value 6

Events An event procedure is a block of code that executes in response to an event. A user event is an action performed by the user such as a mouse click. 7

The Inevitable Hello World Program We will work to create the program shown at the side. Open VB and choose a New, Standard Exe program. 8

Hello World Name the form frmHelloWorld 3. Type in the Caption as shown. Note it change in the title bar. 9

Save the project. Create a new folder and save your form in. Hello World Save the project. Create a new folder and save your form in. 5. A second dialog box will pop to ask for the Project name…change it to Hello World as shown to the right 10

Label Object Add a label object and resize it accordingly. Change the name to lblMessage. Change the Caption to Welcome! 11

Font Property 9. Choose the Font property and make your font larger and of a different type. 12

Alignment Property 10. Choose the Alignment property and Center the label 13

Command Button Add a command button by double clicking that object in the toolbox. Move it as shown. Name the button cmdDisplay and change the caption to Display. 14

Done Button Add a button called cmdDone and change the caption to Done. Note: If you choose to Copy and Paste a control, you will get a message box re: Control Array. Answer “NO” to this. 15

Time to Code We will begin coding with the cmdDone_Click event procedure. 14. Double click the Done button on the object. This will open up the code editor window as shown. Type in Unload Me. 16

Unload Me Adding the code Unload Me to the cmdDone_Click event tells the computer that when the control named cmdDone is clicked, the form (me) will unload. To run your program, choose Run from the menu, press F5, or click the Run button on the toolbar. Clicking the Done button should unload your form. 17

Display Button The code for the Display button will make the Caption in the label lblMessage change to “Hello, World”. 15. Double click the Display button and type in the following code: lblMessage.Caption=“Hello, World” Run your program and click the Done button. 18

Details – Default Buttons It would be good if the user could simply hit the <Enter> key to have the message change. 16. Choose the Display button, find the Default property and set it to True. Run the program. Note the dark outline around the button indicating that it is Default. 19

Details – Keyboard Access Keys Another standard Windows feature is to be able to access a key using Alt-?. To create this ability in your program, change the Caption property to include an &. Caption for Display - &Display Caption for Done – D&one. (You can’t have them both be a D for the hotkey. 20

Moving Your Program and Creating an exe. Your program at this point will consist of three files… *.frm *.vbw *.vbp You would need to transport all 3 of these to have it work properly at a different location. 17. Create an exe by choosing File, Make Helloworld.exe. Choose your path. This one file, of course, will allow others to run your program, but not to edit it. 21

Colors We can alter the look of your form with colors and graphics. Select lblMessage. Choose the Backcolor property, then Palette. Select a color of your own choice. Do the same for the Forecolor. 22

Colors on Buttons You change the color of a button’s background, but first you must set it’s Style property to Graphical. Choose the Display button, then change the Backcolor and the Style property. Note that there is no Forecolor…the font stays black. 23

Graphics We can also add graphics to some objects. Select the Picture property of the Form, then choose an image. This one is at g:\klimack\images\bonnell. Change the file type to All Files if nothing is showing up. 22. Change your Done button picture property remembering to change the Style to Graphical. 24

Message Boxes In your code for the Done button, add this line: MsgBox "Have a good day!", vbOKOnly, "Bye Bye" 25

Run your program. We are done! Comments Adding comments in VB is done by prefixing your statement with an apostrophe. For example: ‘this is a comment and will be ignored by the computer Comments turn green when typed correctly. At the top of your program, add comments with your name, date, and project info as such: ‘Cheryl Klimack ‘Hello World project ‘Oct 24 2003 Run your program. We are done! 26

Break Time! cons 27

Pizza Project We will create the project shown to the left with checkboxes, option buttons, textboxes, message boxes, command buttons and labels. 28

Add a label and format it as you desire. Create a new project by first removing the current project. Save as Pizza. Add a label and format it as you desire. Add an image control and name it imgPizza. The image seen here is found at G:\klimack\images. Note: to resize an image on your form, you must set the Stretch property to True. 29

Add and name labels and textboxes to correspond to the figure at the right. Don’t worry about lining them up perfectly yet. Choose the Text property and delete it’s contents to remove Text1 from the text boxes. txtName lblName txtPhone lblPhone 30

Aligning You can line up objects on your form evenly by selecting them (holding down the shift key allows multiple selections) and then choosing Format, Align, etc. Notice the other options under the Format menu. Line up your labels and text boxes. Choose both labels and text boxes at the same time, then select Font and make them all 10 or 12 point. 31

Frames Frames are used as containers for other controls. Usually groups of option buttons are placed in frames. 7. Add a frame named fraSize. 8. Make the Caption Size. 32

Option Buttons in Frame These are used when only one button of a group should be selected. For example, a pizza size choice cannot be both large and medium at the same time. 9. Select (but do not double click) the Option button control. Draw 3 buttons inside of the frame. Name them optSmall, optMedium, and optLarge. 10. Line them up and make the same size. Make their spacing equal as well. 33

Options on Form Options on a form but not in a frame function as a group. 11. Add options for Pickup and Delivery as shown. Set the Value for Pickup to True Note: Run the program and experiment with the buttons. If you can select 2 in one group, one of them doesn’t belong. Delete it and recreate it. 34

Multiline Textboxes with Scroll Bars 12. Add a label (lblAddress) for Enter Address and a textbox (txtAddress) for the contents of the address. Make the Multiline property True, and set Scrollbars to Both. 35

Checkboxes Checkboxes allow the user to select (or deselect) an option. Any number of checkboxes in a group may be selected. If selected, it’s value is vbChecked, otherwise it is vbUnchecked. Add a frame for Toppings (fraToppings). Add the checkboxes indicated (chkPepperoni, etc.) 36

Order and Cancel Buttons 15. Add an Order and Cancel button as shown in the figure at the right. Make the Order button Default. Add hotkey access. 37

Time to Code! The specs for this program state that the order can be cancelled at any time. After order placement, a message box will appear asking the user to verify the order as indicated. If it is verified another message box will indicate this. 38

Enabling and Disabling It is a good idea to not let the user make inappropriate choices if at all possible. Disable any options that are not applicable at the current time, and enable them when appropriate. 16. Select all the checkboxes at once and set the Enabled property to False. Run the program. 39

Enabling Controls It would be appropriate for these checkboxes to be enabled after choosing a pizza size. Double click optSmall, and add this code to the procedure. chkPepperoni.enabled=true Run the program. Click on Small and Pepperoni should become enabled. 18. Enable each of the checkboxes in the same manner. 40

Choose the procedures here. Copy and Pasting Code 19. The code for Medium and Large will be the same as it is for Small, so you can copy and paste it into those procedures. Now choosing any of the sizes will enable the checkboxes. Choose the procedures here. 41

The Address Box We don’t need the address box if the order is for Pickup, so it should not be visible until the Delivery button is selected. 20. Choose lblAddress and txtAddress and make Visible = false in the properties window. 42

Delivery Option Button If you choose the Delivery option, these two controls (lblAddress and txtAddress) should become visible and the cursor should move to the txtAddress box, waiting for input. 21. Add the following to the optDelivery_click txtAddress.Visible = True lblAddress.Visible = True txtAddress.SetFocus 43

Pickup Option Button Just as Delivery should make the Address info visible, Pickup should make them invisible. Add the following to the optPickup_click procedure: txtAddress.Visible = false lblAddress.Visible = False 44

Cancel Button If the user clicks the Cancel button, all the info currently entered should disappear and the form reset to the initial screen. Get to the cmdCancel_click procedure and enter the following lines of code: 'clear the text boxes txtName.Text = "" txtPhone.Text = "" txtAddress.Text = "" 45

Canceling, continued 'clear the size options optSmall.Value = False optMedium.Value = False optLarge.Value = False 'set the pickup option optPickup.Value = True 'clear the checkboxes chkPepperoni.Value = vbUnchecked chkSausage.Value = vbUnchecked chkMushrooms.Value = vbUnchecked chkGreenPeppers.Value = vbUnchecked chkExtraCheese.Value = vbUnchecked chkOnions.Value = vbUnchecked 46

Canceling, continued Last of all, disable the checkboxes again and set the focus to txtName. 'disable the checkboxes chkPepperoni.Enabled = False chkSausage.Enabled = False chkMushrooms.Enabled = False chkGreenPeppers.Enabled = False chkExtraCheese.Enabled = False chkOnions.Enabled = False ‘set focus txtName.SetFocus 47

Break Time! 48

Global Variables We are going to use some Global variables for this program. 24. Add the lines shown to your program. These variables can be used by all the procedures. 49

Constants Every time the user selects or de-selects a topping, the topping price must change. In this case, toppings are 75 cents each. 25. Constants should be declared before variables, and these are global as well, so add them BEFORE the 2 global variables you just added. 50

Topping Price We need to ADD to the curToppings every time a new topping is selected, and SUBTRACT every time one is de-selected. In each chk procedure, add code similar to the following, changing the Pepperoni to the appropriate topping. If chkPepperoni.value=vbChecked then curToppings=curToppings + curToppingPrice Else curToppings=curToppings –curToppingPrice End if 51

Debug.print “Toppings =“ ;curToppings The Immediate Window For testing purposes, you can View the Immediate window and see the results of the debug.print item statement that can be included in the code. Add this line to each chk procedure. Debug.print “Toppings =“ ;curToppings When you run your program, select and de-select different checkboxes and watch the results in the immediate window. 52

Debug.Print statements Results of Debug.print 53

Size Price The size price depends upon whichever option is selected. Each option procedure needs to have code similar to the following: curSize = curSmall ‘add to optSmall curSize=curMedium ‘ add to optMedium curSize=curLarge ‘add to optLarge 54

Private curTotal as currency Total Price We need to calculate the total due as an addition of curSize and curToppings. Declare curTotal as a global variable of type currency. Private curTotal as currency in the General section. 55

Calculating the Total To calculate curTotal, we need to add a line to every procedure that affects the price. curTotal=curToppings + curSize Add this line to all the options for size and the checkboxes. (Thank goodness for copy and paste!) If you change your Debug.Print statement as shown, you can watch the totals as you run your program. Use Edit, Replace to do this.Also, copy and paste it to your options after the lines that calculate these totals. Debug.Print "Toppings ="; curToppings; "Total is"; curTotal 56

Private strSize as string Verifying the Order Now that we have the totals working properly, we need to manipulate some strings. We want a message box similar to the figure to appear when the Order button is clicked. Declare a global variable to contain a string representing the size of the Pizza. Private strSize as string 57

strSize In each of the procedures for the size options, add a line to assign a string to string size that corresponds with the size. For example, in optSmall add: strSize=“Small” In optMedium, add strSize=“Medium” & in optLarge add strSize=“Large”. 58

Check for Required Input Dim blnValidData As Boolean 'error checking blnValidData = False If txtName.Text = "" Then MsgBox "Invalid name" txtName.SetFocus ElseIf txtPhone.Text = "" Then MsgBox "Invalid phone" txtPhone.SetFocus ‘to be continued 32.Add the following code to verify that no required options or textboxes are left blank. We do not have time to validate the input today. 59

Run your project and click the order button with some text boxes left blank. ElseIf strSize = "" Then MsgBox "Invalid size" optSmall.SetFocus ElseIf optDelivery.Value = True And txtAddress = "" Then MsgBox "Invalid address" txtAddress.SetFocus Else blnValidData=True End If 60

vbCrLf – carriage return, Create a Toppings List Once we have validated entry in each required control, we can create a toppings list. Note the indenting. vbCrLf – carriage return, line feed 'list toppings in a string If blnValidData = True Then If chkPepperoni.Value = vbChecked Then strToppings = strToppings & vbCrLf & "Pepperoni" End If If chkSausage.Value = vbChecked Then strToppings = strToppings & vbCrLf & "Sausage" & concatenate 61

Toppings, continued Copy and paste the if …then statement for Pepperoni 5 times to create appropriate if statements for all of the toppings, as shown for Sausage. A message box can be a quick test to see if this works: MsgBox strToppings 'test toppings string Delete this line once testing is completed. 62

Delivery Method The following code can be used to determine the appropriate string for the method of delivery: 34. Declare a variable strDeliveryMethod as string in the Order procedure. Then add the following to the bottom of the procedure. 'check for delivery or pickup If optDelivery.Value = True Then strDeliveryMethod = "Delivery" Else strDeliveryMethod = "Pickup" End If 63

Message Boxes Revisited There are different parameters you can use with Message Boxes. See the Help for a complete list. We are going to ask the user to click Yes or No to confirm the order. We want their response to be stored so we can act upon it. If it is yes, the form should print and clear. If no, the message box should simply disappear and allow the user to make the required changes. 64

Message Box String The following code will output a message box with the appropriate strings and the Yes/No buttons. 35. First, declare a variable in the declaration section of the procedure. Dim intOrderCorrect as Integer ‘holds yes/no response from user 65

Msgbox() intOrderCorrect = MsgBox("Order for " & txtName & vbCrLf _ & strSize & "Pizza for " & strDeliveryMethod & " with" _ & vbCrLf & strToppings & vbCrLf & vbCrLf & _ "Total = " & Format(curTotal, "currency") & _ vbCrLf & vbCrLf & _ "Is this order correct?", vbYesNo, "Verify Order") _ at the end of a line means that _ the code is continued _ on the next line even though _ the statement is not finished. 66

vbYes, vbNo If the order is correct, print the form and clear it (the same as Canceling). If intOrderCorrect = vbYes Then Me.PrintForm Call cmdCancel_Click End if If the order is not correct, do nothing to the program. 67

Program Complete! 68

Conclusion Thank you for attending this workshop. Available on-line in zipped format is: This presentation Hello World program source Pizza source Source for the simple programs included in your package Visit www.lvp.com to order a copy of an excellent S2,S3 textbook. Visit http://www.edu.gov.mb.ca/ks4/cur/cs/index.html for the new Computer Science Framework 69