Start Visual Basic. Session 1. The User Interface Form (I/II) The Visual Basic Programming Environment. The Tool Box (I/II)

Similar documents
Computer Science 110. NOTES: module 8

Using Visual Basic Studio 2008

GUJARAT TECHNOLOGICAL UNIVERSITY DIPLOMA IN INFORMATION TECHNOLOGY Semester: 4

UNIT 1 INTRODUCTION TO VISUAL BASICS 6.0

Text box. Command button. 1. Click the tool for the control you choose to draw in this case, the text box.

Visual Basic. The Integrated Development Environment. Menu Bar

Visual Basic.NET. 1. Which language is not a true object-oriented programming language?

( ) 1.,, Visual Basic,

Programming with visual Basic:

VISUAL BASIC For Engineers & Scientists. Shahab D. Mohaghegh, Ph.D. Professor Petroleum & Natural Gas Engineering West Virginia University

Overview About KBasic

Las Vegas, Nevada, December 3 6, Kevin Vandecar. Speaker Name:

VBScript: Math Functions

Full file at Programming in Visual Basic 2010

Lecture 1 Introduction Phil Smith

variables programming statements

NiceForm User Guide. English Edition. Rev Euro Plus d.o.o. & Niceware International LLC All rights reserved.

EXCEL 2003 DISCLAIMER:

WEEK NO. 12 MICROSOFT EXCEL 2007

Excel Select a template category in the Office.com Templates section. 5. Click the Download button.

You will have mastered the material in this chapter when you can:

Outline. Midterm Review. Using Excel. Midterm Review: Excel Basics. Using VBA. Sample Exam Question. Midterm Review April 4, 2014

DATABASE AUTOMATION USING VBA (ADVANCED MICROSOFT ACCESS, X405.6)

Document Editor Basics

COPYRIGHTED MATERIAL. Visual Basic: The Language. Part 1

ECDL Module 4 REFERENCE MANUAL

Visual C# Program: Resistor Sizing Calculator

University of Technology Laser & Optoelectronics Engineering Department Visual basic Lab.

NOTES: Variables & Constants (module 10)

LESSON A. The Splash Screen Application

VARIABLES. Aim Understanding how computer programs store values, and how they are accessed and used in computer programs.

Microsoft Excel 2010 Tutorial

VISUAL BASIC 6.0 OVERVIEW

Status Bar: Right click on the Status Bar to add or remove features.

Visual C# Program: Simple Game 3

A Tutorial for Excel 2002 for Windows

Introduction to the Visual Studio.NET Integrated Development Environment IDE. CSC 211 Intermediate Programming

lab MS Excel 2010 active cell

Answer: C. 7. In window we can write code A. Immediate window B. Locals window C. Code editor window D. None of these. Answer: C

Program and Graphical User Interface Design

Introductionto the Visual Basic Express 2008 IDE

GUI Design and Event- Driven Programming

Inspiration 8 IE: tutorial

NATIONAL DIPLOMA IN COMPUTER TECHNOLOGY

Section 3. Editing a Web Page

Visual C# Program: Temperature Conversion Program

Designer Reference 1

Agenda. First Example 24/09/2009 INTRODUCTION TO VBA PROGRAMMING. First Example. The world s simplest calculator...

Gloucester County Library System. Excel 2010

Workbook Also called a spreadsheet, the Workbook is a unique file created by Excel. Title bar

Microsoft Access XP (2002) - Forms. Navigation Wizards Custom Forms Combo Boxes Calculations in Forms Pictures Multitable Input Summary Operations

Corel Ventura 8 Introduction

SUM - This says to add together cells F28 through F35. Notice that it will show your result is

Tutorials. Lesson 1 - Format a Schedule. In this lesson you will learn how to: Change the schedule s date range. Change the date headings.

d2vbaref.doc Page 1 of 22 05/11/02 14:21

Language Fundamentals

GETTING STARTED WITH VBA

LESSON B. The Toolbox Window

Architect. User s Manual

MODIFYING CIRCULATION WINDOW DISPLAYS

FIT 100. Lab 8: Writing and Running Your First Visual Basic Program Spring 2002

UNIT 1. Introduction to Microsoft.NET framework and Basics of VB.Net

Spreadsheet View and Basic Statistics Concepts

Microsoft Excel for Beginners

Review for Programming Exam and Final May 4-9, Ribbon with icons for commands Quick access toolbar (more at lecture end)

1 THE PNP BASIC COMPUTER ESSENTIALS e-learning (MS Excel 2007)

Setup Examples. RTPView Project Program

EXCEL TUTORIAL.

TSM Report Designer. Even Microsoft Excel s Data Import add-in can be used to extract TSM information into an Excel spread sheet for reporting.

Standardized Coding Practices

Budget Exercise for Intermediate Excel

INTRODUCTION TO CHEMDRAW ULTRA 12.0

RL6 - WORKING WITH REPORTS

Introduction to Microsoft Word

Center for Faculty Development and Support Making Documents Accessible

Lesson 8: Presentation Enhancements Microsoft PowerPoint 2016

Creating Web Applications Using ASP.NET 2.0

Introduction to Data Entry and Data Types

The Mathcad Workspace 7

Introduction to Computer Use II

Visual C# Instructor s Manual Table of Contents

Visual Programming 1. What is Visual Basic? 2. What are different Editions available in VB? 3. List the various features of VB

v Annotation Tools GMS 10.4 Tutorial Use scale bars, North arrows, floating images, text boxes, lines, arrows, circles/ovals, and rectangles.

EXCEL BASICS: MICROSOFT OFFICE 2010

Microsoft How to Series

EXCEL 2007 TIP SHEET. Dialog Box Launcher these allow you to access additional features associated with a specific Group of buttons within a Ribbon.

Microsoft Excel Level 1

Microsoft Office Excel

Dive Into Visual C# 2010 Express

2 USING VB.NET TO CREATE A FIRST SOLUTION

Access Review. 4. Save the table by clicking the Save icon in the Quick Access Toolbar or by pulling

ENGG1811 Computing for Engineers Week 9 Dialogues and Forms Numerical Integration

EXCEL BASICS: MICROSOFT OFFICE 2007

POS Designer Utility

Band Editor User Guide Version 1.3 Last Updated 9/19/07

Tutorials. Lesson 3 Work with Text

Creating a Spreadsheet by Using Excel

VB FUNCTIONS AND OPERATORS

HELP - VB TIPS. ANIMATE AN IMAGE BOX Insert a module. In this module, create a global variable: Global x

Angel International School - Manipay 1 st Term Examination November, 2015

Transcription:

Session 1 Start Visual Basic Use the Visual Basic programming environment Understand Essential Visual Basic menu commands and programming procedure Change Property setting Use Online Help and Exit Visual Basic In Microsoft Windows, click Start button, point to programmes and Microsoft Visual Basic Click Visual Basic 6.0 Programme Icon Click Open to accept the default project, a standard 32-bit Visual Basic application References: Michael Halvorson, Microsoft Visual Basic 6.0 Professional Step by Step Microsoft Press, 1998 Any Other VB Books will be useful 1 2 The Visual Basic Programming Environment The User Interface Form (I/II) A form is a window for creating the user interface of a programme Form Window Project Window Properties window Form Programme User Interface Project Container Window Form Layout window A result screen after press Display Tool bar Menu bar 3 4 The User Interface Form (II/II) The Tool Box (I/II) A form can contain Menus Buttons List Boxes Scroll Bars. The default form: Form1 A standard grid to line up elements of programmes user interface Adjust the size of form using the mouse Add additional forms using Add Form command on the Project menu The run-time position of the user interface is controlled by the Form Layout window The elements of a programme s user interface is added to a form by using the tools or controls in the tool box ToolBox contains controls you can use to add to a user interface Artwork, Labels, Buttons, List boxes, Scroll bars, menus, and geometric shapes. Each control added to a form becomes an Object, or programmable user interface element. These elements will be visible to the user of the program when the programme runs and will operate like the standard objects in any Windows-based application 5 6 1

The Tool Box (II/II) The tool box also contains controls that can be used to create objects that perform special behind the scenes operations in a VB programme. The Properties Window The properties window lets you change the characteristics, or property settings, of the user interface elements on a form These objects are not visible to the user when the programme is running, these include objects for Manipulating information in databases Working with Windows-based applications... 7 8 The Project Window Getting Help A VB programme is made up of several files that are assembled together or compiled when a programme is complete. The project window lists all the files used in the programming process and provide access to them via two special buttons: View Code and View Object The project file that maintains the list of all the supporting files in a programming project is called the VB project (.vbp) Project window displays the components of the project Exiting Visual Basic Save any changes you have made On the File menu, click the Exit command 9 10 Session 2 Task, Specification, Algorithm Create the user interface for a new programme Set the properties for each object Write programme code Save and run the programme Build an executable file Programming Step DCreate the user interface Lucky Seven: The programme should perform the following actions Provide a user interface that has : Spin and End buttons 3 spinner windows, a descriptive label, a winner display window Pick three random number and display them when the user click Spin DSet the properties Display a stack of coins and beep if the number 7 appears in one of the spinner windows DWrite the programme code Terminate when the End button is clicked 11 12 2

Creating the User Interface (I/IV) Setting the Properties (II/IV) 1 On the file menu, click the New Project command 2 Enlarge the Form window until the scroll bar appear in the Project Window as shown on the right 3 Click the CommandButton control in the toolbox, then place the mouse pointer over the form 4 Move the mouse pointer close to the upper-left corner of the form, hold down the left mouse button, then drag down and to the right. Stop dragging and release the mouse button until you have a button similar to the one shown here on the right 5 Add a second CommandButton 6 Add the number Labels 7 Add an image Command button Label Control Image Control Setting the CommandButton Properties 1 Click the first command button on the form 2 Double click the Properties window title bar (the Properties window is enlarged to full size) 3 Double click the Caption property in the left column, and change the current Caption Command1 to Spin 4 Open the object drop-down list box at the top of the Properties window, a list of interface objects appears 5 Click Command2 and change its Caption to End 13 14 Setting the Properties (III/IV) Setting the Properties (IV/IV) Setting the Number Labels Properties Setting the Descriptive Labels Properties 1 Click the first number label and then holding down the Shift key click the second and third number labels. 1 Click the fourth label object on the form 2 Click the Alignment property, then the drop-down list box to align the buttons to Centre 2 Change the caption to Lucky Seven 3 Change the Fond property 3 Click the BorderStyle property and change it to FixedSingle 4 Double-click the Font property and change the Font and Style (e.g., Times New Roman, Bold, and 24 point size) 4 Double-click the ForeColor property and change the colour of your object The System tab shows the current colours used for the user interface elements in your system, the palette shows all the available colours 5 Delete the captions in the 1st, 2nd and 3rd number labels 15 16 Setting the Properties Setting the Image Box Properties Use the Code Window 1 Double click the End command button on the form, the code window appears 1 Click the image box object on the form 2 Click the Stretch property and set it to True 3 Double-click the Picture property and load a picture using dialog box 4 Click the Visible property, select False to make the picture invisible when the programme starts A body of a procedure always fits between these two lines and is executed when a user activates the interface element associated with the procedure VB subroutine, or event procedure associated with a particular object in the interface The event is a mouse click 17 18 3

2 Type End, and press the Down arrow key. The End statement stops the execution of a programme and remove it from the screen Write Code for the Spin button 1 Open the object drop down list box in the Code window 2 Click Command1 in the list box and write the following code End is a keyword recognised by Visual Basic Compiler. VB has several hundred unique keywords 3 Indent the End statement by placing the cursor to the beginning of the line with End state in it, and press the spacebar 4 times (the indenting scheme is one of programming conventions to keep your programme clear and readable, the set of conventions regarding how programme code is organised is often referred to as programme style 19 20 A Look at the Command1_Click Procedure The Command1_Click procedure is executed when the user click the Spin button on the form It performs 3 tasks: hides the coin stack, creates random numbers for the label windows and displays the coin stack when the number seven appears. Image1.Visible = False hide coins A Look at the Command1_Click Procedure The Rnd function creates a random number between 0 and 1 Int (Rnd*10) to create numbers between 0 and 9 and round them to integer number The numbers are then assigned to the Caption properties of the first three labels, and the assignment causes the numbers to be displayed in the label windows Programme statement Comment explanatory notes included in the programme following a single quotation mark( ). These notes are not processed by VB when the programme runs, they exist only to document what the programme does. The last group of statements checks whether any of the random number is 7, if one or more of them is, the stack of coin is made visible and beep 21 22 Saving the Programme Hungarian Convention 1 On the File menu, click the Save Project As command 2 Select your programme file folder 3 Type the file name (e.g. MyLucky) and press enter Run the Programme 1 Click the Start button on the toolbar 2 Click the Spin button 3 Click the Spin button 10 to 20 times, watching the results 4 Click the End button to finish To help identify objects from their names, the Hungarian convention is used. Then when you see a list of subroutine names, for example, you can identify the object being referred to. It is good practice to change the default names of objects to ones that you want, as soon as you create the object. The convention suggests using the following prefixes: Check box chk Combo box cbo Command button cmd Data dat Directory list box dir Drive list box drv File list box fil Form frm Frame fra Horizontal scroll bar hsb Image img Label lbl Line lin List box lst Menu mnu Picture box pic Radio button opt Shape shp Text box txt Vertical scroll bar vsb 23 24 4

Session 3 The Anatomy of a VB Programme Statement A programme statement is a valid instruction for the Visual Basic Compiler `Use variables to store data in your program `Get input by using InputBox function `Display messages by using the MsgBox function `Use Mathematical operators and functions in formulas Programme Statement Keywords properties functions operators symbols Visual Basic Compiler Statement Syntax: is the rules for constructing programme statement Example: Label1.Caption = Time Object name Property name assignment operator VB Function 25 26 Using Variable to Store Information Working with Specific Data Types VB variables follow the usual rules. There are many types of variables including Byte, Boolean Integer, Long, Single, Double, Currency, Date, String and Objects. Variants and sub-types The programmer has the option of declaring variables with explicit types or as type variant. The variant type provides dynamic typing: at run time the value has an associated tag to record the value currently stored. Declarations Dim A A as variant Dim B As Integer Dim C As String variable length string Dim D As String*10 fixed length string Const pi = 3.14.59 pi as constant Data type Size Sample usage Integer 2 bytes Dim Birds% Birds%=37 Long 4 bytes Dim Loan& integer Loan&=32,000 Single precision 4 bytes Dim Price! floating point Price!=899.98 Double precision 8 bytes Dim Pi# floating point Pi#=3.1415926535 Currency 8 bytes Dim Credit@ Credit = 567899.90 String 1 byte per Dim Dog$ character Dog$= Pointer Boolean 2 bytes Dim Flag as Boolean Flag = True Date 8 bytes Dim Birthday as Date Birthday = #3-1-63# 27 28 User Defined Data Types Working with Arrays of Variables (I/III) Example: Using Type statement to define a new data type An array is a collection of values stored under a single name Type Employee Name As String DateOfBirth As Date HireDate As Date End Type Creating an Array: Before you can use an array, you must declare it. Declaring a Fixed-Sized Array: Syntax Public ArrayName (Dim1Elements, Dim2Elements,...) As DataType Use the new data type Dim ProductManager As Employee ProjectManager.Name = Erick Cody ProjectManager.HireData = #3-9-89# Public is the keyword that creates a global array ArrayName is the variable name of the array Dim1Elements is the number of elements in the first dimension of the array Dim2Elements is the number of elements in the second dimension of the array As DataType is a keyword corresponding to the type of data that will be include in the array To declare arrays locally in an event procedure, replace the public keyword with Static keyword and place the declaration inside and event procedure. Local array can only be used inside the procedure. 29 30 5

Working with Arrays of Variables (II/III) Working with Arrays of Variables (III/III) Creating Dynamic Array To create a dynamic array, omitting the number of elements in the array. e.g. Public Temperature ( ) As Variant Actual size must be set before use by using the keyword ReDim 31 32 Working with Visual Basic Operators Visual Basic Mathematical Functions Operator Mathematical Operation + Addition - Subtraction * Multiplication / Division \ Integer division Mod Remainder division ^ Exponentiation (Rising to a power) Abs (n) Atn (n) Cos (n) Exp (n) Rnd (n) Sgn (n) Sin (n) Sqr (n) Str (n) Tan (n) Val (n) & String concatenation (Combination) 33 34 Operator Precedence Using Variable to Store Input Operator Order of Precedence Get Input by using InputBox ( ) Values within parentheses are always evaluated first ^ Exponentiation is second - Nagetive is third */ Multiplication and division is fourth \ Integer division is fifth Mod Remainder division is sixth +- Addition and subtraction is last Exercise: Create a user interface as shown on the right and type in the code and run the program. Use the online help to find out more about InputBox function. 35 36 6

Functions Using a Variable for Output InputBox is a special Visual Basic keyword known as function. A function is a statement that performs meaningful work, such as prompt user for information, or calculating an equation, and then return a result to the program The value returned by a function can be assigned to a variable, or a property, or another statement or function When a function uses more than one argument, the arguments are separated by commas, the whole group of arguments is enclosed in parentheses, e.g FullName = InputBox$(Prompt, Title) The MsgBox function uses text strings to display output in a dialog box, the syntax for the MsgBox function is ButtonClicked = MsgBox(Message, NumberOfButtons, Title) Message: is the text to be displayed on the screen NumberOfButtons: is button style number (1-5) Title: is the text displayed in the message box title bar The variable ButtonClicked is assigned the result returned by the function, which indicates which button the user clicked in the dialog box. If you are just displaying a message in MsgBox, the assignment operator (=), the ButtonClicked variable, and the NumberOfButton argument are optional. Find out more about MsgBox in the VB online Help. 37 38 Week 1 Lab Exercises (I/IV) Week 1 Lab Exercises (II/IV) 1. Practice the Lucky Seven program described in pages 12-23 of this note. But make sure that you use the Hungarian convention described in page 24 to name all the objects, e.g., the Spin button is a command button, you can name it as cmdspin. Obviously, cmdspin is more meaning than Command1. 2. Following this example, can you create a Lucky Eight program? 3. Try and change the colour and other appearances of the objects and user interface. Note: There is a coins.jpg image in the module s document directory which you can copy to your own directory and use in the program. The Hello World Program 1 Create a user interface using TextBox and CommandButton Control 2 Set the following properties for TextBox and Command Control Property Setting Text1 Text (Empty) Command1 Caption OK 3 Double-click the OK command button and type the following programme statement between Private Sub and End sub statement Text1.Text = Hello, World! TextBox 4 User the Form Layout window to set the position of the form when the programmes runs Can you change the appearance, font, text size, colour etc of Hello World!? 5. Make sure to use the Hungarian convention 39 40 Week 1 Lab Exercises (III/IV) Week 1 Lab Exercises (IV/IV) Using Timer Object A timer object is an invisible stopwatch that gives you access to the system clock from your programs. A timer object is accurate to 1 millisecond (1/1000 second) Creating a digital clock by using timer object 1 Open a new project and resize the form to a small window 2 Click the timer control in the toolbox 3 Create a small timer object on the left side of the form 4 Click the Label control in the tool box, and create a label in the centre of the form that fills most of the form 5 Open the properties window, and set the following properties Label1 Caption Empty Font Times New Roman, Bold, 24 point Alignment 2 - Center Timer1 Interval 1000 Enabled True Form1 Caption Digital Clock 6 Double click the timer object and type the following code in the Timer1_Timer event procedure Label1.Caption = Time 7 Can you display date on your digital clock as well? Use File system objects 1 Click the DriveListBox control in the toolbox 2 Click the DirListBox control in toolbox and then add a directory list box to the form below the drive list box 3 Add FilelistBox 4 Add Image control 5 Set object properties as follows Object Property Setting File1 Pattern *.bmp; *wmf;*.ico; *.jpg Image1 Stretch True 6 Type the following code Private Sub Dir1_Change() File1.Path = Dir1.Path End Sub Private Sub Drive1_Change() Dir1.Path = Drive1.Drive End Sub Try and browse images in your system. I have put some in the module s Private Sub File1_Click() document directory. Try and SelectedFile = File1.Path & "\" & File1.FileName Image1.Picture = LoadPicture(SelectedFile) copy them into your local directory End Sub and have a look at those images. 41 42 7