UNIT 1 INTRODUCTION TO VISUAL BASICS 6.0

Similar documents
Computer Science 110. NOTES: module 8

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

Using Visual Basic Studio 2008

NOTES: Variables & Constants (module 10)

PROGRAMMING LANGUAGE 2 (SPM3112) NOOR AZEAN ATAN MULTIMEDIA EDUCATIONAL DEPARTMENT UNIVERSITI TEKNOLOGI MALAYSIA

Visual Basic. The Integrated Development Environment. Menu Bar

Standardized Coding Practices

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

Clickteam Fusion 2.5 Creating a Debug System - Guide

VISUAL BASIC 6.0 OVERVIEW

Handout Objectives: a. b. c. d. 3. a. b. c. d. e a. b. 6. a. b. c. d. Overview:

CS130/230 Lecture 12 Advanced Forms and Visual Basic for Applications

SlickEdit Gadgets. SlickEdit Gadgets

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

Code::Blocks Student Manual

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

Introduction. Getting Started with Visual Basic Steps:-

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

Getting started 7. Setting properties 23

Introduction to Data Entry and Data Types

Access Intermediate

Let s begin by naming the first folder you create Pictures.

Visual Basic Program Coding STEP 2

Visual C# Program: Resistor Sizing Calculator

Creating Web Pages with SeaMonkey Composer

Getting Started with Visual Basic.NET

Programming Language 2 (PL2)

Contents. More Controls 51. Visual Basic 1. Introduction to. xiii. Modify the Project 30. Print the Project Documentation 35

Program and Graphical User Interface Design

Customizing Access Parameter Queries

Manual. Note: This software has only been tested with VF-1 firmware Compatibility with other firmware versions cannot be guaranteed!

The Stack, Free Store, and Global Namespace

Creating a Dynamo with VBA Scripts

BasicScript 2.25 User s Guide. May 29, 1996

Advanced Layout Tools

Using Microsoft Word. Text Editing

QTP Open Source Test Automation Framework Coding Standards for Developers

Introduction VBA for AutoCAD (Mini Guide)

17. Introduction to Visual Basic Programming

Overview About KBasic

Code::Blocks Student Manual

9.0 Help for End Users Release Notes Using Jive for Outlook...5

Using Jive for Outlook

The Fundamentals. Document Basics

VBA Excel 2013/2016. VBA Visual Basic for Applications. Learner Guide

COPYRIGHTED MATERIAL. Visual Basic: The Language. Part 1

Word - Basics. Course Description. Getting Started. Objectives. Editing a Document. Proofing a Document. Formatting Characters. Formatting Paragraphs

SchoolDesk University

Writer Guide. Chapter 15 Using Forms in Writer

GUJARAT TECHNOLOGICAL UNIVERSITY DIPLOMA IN INFORMATION TECHNOLOGY Semester: 4

Part I. Integrated Development Environment. Chapter 2: The Solution Explorer, Toolbox, and Properties. Chapter 3: Options and Customizations

A Quick Tour GETTING STARTED WHAT S IN THIS CHAPTER?

Drawing an Integrated Circuit Chip

Visual C# Program: Temperature Conversion Program

GUI Design and Event- Driven Programming

Using Microsoft Word. Paragraph Formatting. Displaying Hidden Characters

AutoCollage 2008 makes it easy to create an AutoCollage from a folder of Images. To create an AutoCollage:

Karlen Communications Track Changes and Comments in Word. Karen McCall, M.Ed.

TABLE OF CONTENTS TABLE OF CONTENTS... 1 INTRODUCTION... 2 USING WORD S MENUS... 3 USING WORD S TOOLBARS... 5 TASK PANE... 9

COPYRIGHTED MATERIAL. Part I: Getting Started. Chapter 1: IDE. Chapter 2: Controls in General. Chapter 3: Program and Module Structure

FreeCommander ~ Configuring Operating Elements

Lesson 3 Transcript: Part 1 of 2 - Tools & Scripting

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

Understanding Word Processing

CaseComplete Roadmap

Full file at Programming in Visual Basic 2010

2 USING VB.NET TO CREATE A FIRST SOLUTION

Writer Guide. Chapter 15 Using Forms in Writer

Corel Ventura 8 Introduction

Lecture 1 Introduction Phil Smith

Introduction. Creating a New Publication. Publisher 2010 Creating a New Publication. To Create a New Publication from a Template: Page 1

Excel 2010: Getting Started with Excel

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

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

The Filter Property Selecting a File The Save Menu The SaveFileDialog Control The Edit Menu The Copy Menu...

Tracking changes in Word 2007 Table of Contents

Using Windows 7 Explorer By Len Nasman, Bristol Village Computer Club

HOW TO USE CODE::BLOCKS IDE FOR COMPUTER PROGRAMMING LABORATORY SESSIONS

Mouse Basics. Dayton Metro Library

In this chapter, I m going to show you how to create a working

Copyrighted Material. Copyrighted. Material. Copyrighted

Get JAVA. I will just tell you what I did (on January 10, 2017). I went to:

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

Getting started 7. Setting properties 23

Creating an with Constant Contact. A step-by-step guide

Excel VBA Variables, Data Types & Constant

COP Programming Assignment #7

TLMC SHORT CLASS: THESIS FORMATTING

San Pedro Junior College. WORD PROCESSING (Microsoft Word 2016) Week 4-7

Lab 7 Macros, Modules, Data Access Pages and Internet Summary Macros: How to Create and Run Modules vs. Macros 1. Jumping to Internet

VBA Foundations, Part 7

Using Eclipse and Karel

A PROGRAM IS A SEQUENCE of instructions that a computer can execute to

Civil Engineering Computation

variables programming statements

In further discussion, the books make other kinds of distinction between high level languages:

Copyright. Trademarks Attachmate Corporation. All rights reserved. USA Patents Pending. WRQ ReflectionVisual Basic User Guide

HOUR 4 Understanding Events

Instructions for Using the Databases

Chapter 1. Introduction to Programming and Visual Basic Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of

Transcription:

UNIT 1 INTRODUCTION TO VISUAL BASICS 6.0 The VB6 IDE (Integrated Development Environment) is a very simple and fully featured IDE. If you start out programming in VB6 you may end up being too spoiled to ever appreciate a more complicated and less functional IDE like most C++ IDEs. One feature which sets VB6 apart from various IDEs is the simplicity of its approach to GUI (Graphical User Interface) design. As a general rule: Play with it. You're very unlikely to break anything that matters, so just explore and experiment with the IDE, and you'll learn more. Creating A Project 1. Start VB6 2. When presented with a "New Project" dialog you will usually want to pick "Standard EXE" and press "OK" 3. You now see the VB6 IDE, which contains an empty form called "Form1". That is your program. Description of the IDE In the middle of the program is a window which contains another window called "Form1". This is called the GUI designer window. That window inside of there with the title "Form1" is your program. On the left is the toolbox, which stores the pre-made objects you can place in your program. This includes visual items you often see in programs like textboxes, command buttons, checkboxes. It also has the potential to include invisible items like the winsock control, which allows easy access to network communication. On the right is the Object properties pane, which contains all of the properties of the item currently selected in the GUI designer. On the top there is the menu bar. On the top, below the menu bar is the toolbar. Setting up the IDE Don't allow sloppiness 1. Click on Tools->Options 2. Check "Require Variable Declaration" 3. Click on the "Environment" tab 4. Select the option button which says "Prompt to save changes" 5. Press "OK" Get rid of silliness

On the bottom-right of the IDE, close the "Form Layout" pane because its pretty much useless. Access to handy functions. 1. Right-click somewhere on the toolbar and click on "Customize..." 2. Click on the "Commands" tab 3. Under "Categories", select "Edit" 4. Scroll down to about the middle of the "Commands" box until you can see "Indent", "Outdent", "Comment Block", and "Uncomment Block". Drag all of those items to your toolbar, putting them in order, right next to the "Start", "Pause", and "Stop" buttons. 5. Press "Close" Changing Properties of Objects If you click on an object on your form, or even the form itself, you gain access to the properties of that item and change them in the properties pane on the right side of the screen. Using controls Placing controls on a form All of the items on the left-hand of the screen, in the toolbox pane, are able to be added to a form like this: 1. Click on the box representing the control you want in your program 2. Click the left mouse down on a space on a form, then drag to another spot and release the left mouse button. This sometimes makes the control you selected the same size as you directed, and sometimes objects are a fixed size which you can't modify Importing extra controls If you want to use a control which is not included in VB6's "Standard EXE" toolbox of 21 controls, you'll need to import them like this: 1. Right-click somewhere in the toolbox, and select "Components" 2. In that window you can select an OCX file which contains controls you can add to your form If the control you're looking for isn't on the list then you can try to find it by clicking "Browse..." and finding the OCX file manually Sometimes OCX files are given the extension DLL when the file is a DLL which includes OCX data inside it Underlined letters

When you press the Alt button in many programs you're shown little lines underneath certain letters of menu items. If you then press on your keyboard a letter which corresponds to one of the underlined letters then that menu item is selected. In VB6, to get that functionality all you have to do is place an apersand (&) the letter before the letter you want to be underlined and functional in this way. This functionality exists in: Primary Menu items Secondary menu items Command buttons More..? Running A Program Take a look in the menu item "Run". You will find that: Pressing "Start" in that menu, pressing the "Play" button, or pressing F5 on your keyboard will run your program Pressing "Break" in that menu, pressing the "Pause" button, or pressing Ctrl+Break on your keyboard will pause your program o Allows you to edit your program while you're running it (AWESOME! Spoilage factor here versus every other programming IDE) Pressing "End" in that menu, or pressing the "Stop" button will stop your program Stopping A Program When you are done testing your application, you will want to close it. There are several ways to accomplish this. If at all possible you should close your application by pressing the "X" button on the topright of the window of an application, or using the keystroke Alt+F4. This is a "clean" termination of the program. The following options should be used only in the case that your program has stalled and you cannot close it by pressing Alt+F4 or the "X" button on the program's window. In all of these options there is a chance of causing a memory leak, which renders some space in your computer's memory unusable until the next time you reboot the computer. If the VB6 IDE is still responsive: Press the "Stop" button located beside the "Play" and "Pause" buttons in the VB6 IDE. If the VB6 IDE is no longer responsive: Press Ctrl+Break to Pause the program's execution.

If Ctrl+Break doesn't work: Try to end VB6.exe via the windows task manager. If none of that works: Time to restart the computer and go fix that horrible bug in your program! Compiling A Program You must be using a full, retail copy of Visual BASIC. The Learning Edition will not compile to EXE In the VB6 IDE, press File->Make ProjectName.exe, select a location and filename you would like your program to have and VB6 will create that EXE for you simply and very easily. The process in which source code is converted into EXE is called "compiling", and is far more simplified in VB6 than a language like C++. In order for your program to run on any given computer, that computer must have all of your program's "dependencies". All programs written in VB6 will require the Visual Basic 6 Runtime Library (msvbvm6.dll). Happily: Windows XP had the VB6 runtime library ever since first release, so if you make a VB6 program with no extra dependencies, it will work on Windows XP. Prior versions of windows, however, did not come with the VB6 runtime preloaded, and will require it to be installed if it hasn't yet been installed. If your program requires any extra DLL or OCX files in order to work, those are now dependencies of your program which you will need to supply to anyone you want to send your program to. For maximum portability it is a good idea to rely on functionality you implement in your own program. If you're lucky, you can opt for implementing the CTL source code file from an OCX into your program if the OCX is open-source and was written in VB6. Getting into the source code Right-click anywhere on the form called "Form1", and select "View code". Another way to accomplish the same thing is to double-click anywhere on the form. You should now see the following: 1. Option Explicit 2. 3. Private Sub Form_Load() 4. End Sub This is the current source code behind this form. Your cursor is currently between "Private Sub Form_Load()" and "End Sub" because the object you double-clicked on was a "Form" and the

default subroutine for the IDE to bring you to if there is nothing yet coded for that object is "Load()". You can change which subroutine associated with "Form" you want to look at by clicking somewhere inside the "Form_Load" routine, then selecting one of the options inside the combobox on the top right of the window you're in. This function will get you spoiled, quick. Setting project properties In the top menu click on Project» Project1 Properties Search through this dialog. Learn its options. Set the values inside. This gives you a good understanding of the basics of the VB IDE. As you program and learn new things these tips will prove very helpful. If you have any other tips or questions please post them by clicking the Add Comment button below. Beginner Tutorial - Hello World Level: Rank: (11 votes) Visual Basic is a great starter programming language. Not only is it easy to learn, but many business applications use it extensively for their applications. If you are just starting to learn to develop applications - this is a great language to start with. This tutorial and the many other Visual Basic tutorials at this site will give you a solid foundation in your Visual Basic knowledge. Lets jump right in with a simple application. When you are done with this Visual Basic tutorial, you should have a complete working application that when you click on the button it will say "Hello, World" Start Microsoft Visual Basic 6.0 (VB6) The New Project dialog box will appear. If it doesn't go up to the menu bar and select File -> New Project

In the New Project dialog select Standard EXE, and click the Open Button. This will bring up your new Project 1 application with Form1 visible. Already Visual Basic has done a lot for us. As you can see this tutorial isn't very long but already you have a full working application. You can see your new program in action by going up to the menu bar and selecting Run -> Start (Or simply press the F5 key). You should see the Form1 window appear:

This is a fully functional application. You can move it around, minimize and maximize it, and close it down. For you to do this same thing in C++ - the original language most of Windows was written in you would have written hundreds of lines of code. You area already getting to see some of the extreme power VB gives you. Now lets continue with the tutorial. Lets make this program say hello!

On the left side of the screen you can see the toolbox (if this doesn't show up go to the top menu bar and select View -> Toolbox). In this toolbox you will see a picture of a button. Double click the button icon and it will create a Command1 CommandButton in the center of your form.

If you run the program now (Press F5) you will see your window now has a button labeled Command1 in the center of it, but if you click the button it doesn't do anything. So lets wire things up so our program will say "Hello, World" when you click the button. Close out of your running program so you are back to the main design environment (pictured above) Visual Basic allows you to do event driven programming. This is a concept that is very powerful and easy to use. Event driven programming works as follows: Visual Basic has many different events defined that occur when a specified thing happens. We as the programmer can link into these events and write our custom code to do what we want. One very useful event is the Click event. This event occurs any time the user clicks on the specified object. I'm sure the wheels are already turning in your head, if we want to say hello world when someone clicks the button on our form than we should do something in the Click event for the Command1 button. That is exactly what we are going to do. To write the click event code double click on the Command1 button. This will bring up the code editor and will automatically write the beginning code to handle the click event.

Now any code you put between the Private Sub... and the End Sub statements will be executed when the user clicks the command button. To demonstrate this we will have a message box appear saying hello world. So add this line of code: 1. Private Sub Command1_Click() 2. MsgBox "Hello, World!" 3. End Sub MsgBox is a built in Visual Basic function that causes a message box to be displayed to the user. The first parameter this function takes is the text string you wish to have displayed. We choose the text string "Hello, World!". MsgBox also takes other parameters to specify things such as what buttons to display and what caption to use, but these will be discussed later. For now lets see how this works. Run your newly created Visual Basic hello world program (Press F5). Once the program is running click the Command1 button, you should see a message box saying Hello, World! appear.

Congragulations! You have written a complete working Visual Basic program using this Hello, World tutorial. Read some of our other VB6 tutorials to learn how powerful this language really is. Object Types and Naming Scheme This tutorial explains all the different aspects of Visual Basic data types and constructs. It also gives suggestions as to how to name your different VB6 items. These are very important things to remember in order to make your source code more easily intelligible. File Types You will use various file types while using VB6. These are some the most common ones. File type: What people call it Prefix: What people prefix the filename with Extension: What file extension is used Description: What its used for most often File Type Prefix Extension Description Project vbp General project options Form frm frm GUI information and private code BAS Module mod bas Project-wide accessible functions Class Module cls cls Project-wide accessible subroutines User Control uc ctl Control object (Like an OCX with source code) Property page pag pag Property information OLE Control ocx Compiled control object Dynamic Link Lib dll Subs and functions accessible by other programs Examples of common file names: OddCalc.vbp frmmain.frm frmabout.frm frmprintinvoice.frm modmain.bas

modsettings.bas moddeclares.bas modwinsock.bas clswinsock.cls uccustombutton.ctl uctreeview.ctl ucwinsock.ctl Variables A variable is a word or letter used to reference data used in a program. At the most basic level: All variables used in a program (Even if its interpreted as text) are held on the computer as a sequence of 1s and 0s (Binary) which represent numbers, which may or may not in turn represent letters or any given ASCII character. In a sane programmer's code the variable names are easy to understand because they clearly state what the variable is used for inside of the variable name. The information to be conveyed in a variable name is: 1. Variable data type 2. Functional use in program This is accomplished by coming up with a unique word between about 3 and 10 letters which explains the functional use of the variable as well as a prefix of usually 3 letters which explains the variable data type. A few examples of this: intresult -- An integer which is the result of an operation strfirstname -- A string which is used to store the first name of a person dtmworkdayend -- A Date variable which is used to store the time of the end of the work day Computer Data Storage All data stored on a computer is based upon binary values associated with them somewhere between 0 and 255. This is the range of values possible with an 8 bit binary value (8 ones and zeroes). Decimal Binary Hexadecimal ASCII 48 0b00110000 0x30 0 49 0b00110001 0x31 1 50 0b00110010 0x32 2

------- --------------- --------- ----- 55 0b00110111 0x37 7 56 0b00111000 0x38 8 57 0b00111001 0x39 9 ------- --------------- --------- ----- 65 0b01000001 0x41 A 66 0b01000010 0x42 B 67 0b01000011 0x43 C ------- --------------- --------- ----- 88 0b01011000 0x58 X 89 0b01011001 0x59 Y 90 0b01011010 0x5A Z ------- --------------- --------- ----- 97 0b01100001 0x61 a 98 0b01100010 0x62 b 99 0b01100011 0x63 c ------- --------------- --------- ----- 120 0b01111000 0x78 x 121 0b01111001 0x79 y 122 0b01111010 0x7A z A text character's "ASCII value" is the decimal value of the binary value used to represent that character on the computer. In the case of the uppercase letters A, the ASCII value is 65, which is 01000001 in binary. Uppercase Z has an ASCII value of 90, which is 1011010 in binary. The values for uppercase letters A through Z are between the values 65 and 90. Anybody notice how similar the uppercase and lower case values are in binary and hex? In binary you toggle the 32's place in order to change case, and in hex you add/subtract 2 from the 16's place. Handy. Associated example to play with: 1. Option Explicit

2. Private Sub Form_Load() 3. Dim strchar As String ' Declares a variable 4. 5. ' Shows an input box and puts the result in a variable called strchar 6. strchar = InputBox("What would you like the ASCII value of?", "HUH!? PUNK!?", "A") 7. 8. ' Shows a message box containing the ASCII value of the previously input letter 9. ' plus a random ASCII uppercase letter 10. MsgBox "The ASCII value of " & strchar & " is " & Asc(strChar) & vbnewline & _ 11. "And your random, uppercase ASCII character is: " & _ 12. Chr$(RandomNumInRange(65, 90)) 13. ' Unloads the form (Which cleanly ends the program if no other forms are loaded) 14. Unload Me 15. End Sub 16. Public Function RandomNumInRange(ByVal Low As Long, ByVal High As Long) As Long 17. Randomize ' Randomizes Rnd() (Surprisingly good random number generator) 18. 19. ' Generates a random number between "High" and "Low" and returns it 20. RandomNumInRange = Int((High - Low + 1) * Rnd) + Low 21. End Function Variable Names The following are the requirements when naming the variables in Visual Basic: It must be less than 255 characters No spacing is allowed It must not begin with a number Period is not permitted For the sake of making sure other people can look at your code and know what you were thinking: Suffix your variable name with the appropriate suffix for your variable's data type Make sure the body of your variable name makes it easy to tell what its used for Don't use an ambiguous name like "intuhhhh" or "strx" unless its use is within a very small scope of the program Numeric Data Types Type Size Range of Values Prefix Example Variable Name Byte 1 byte 0 to 255 byt bytfirstchar

Integer Long 2 bytes 4 bytes -32,768 to 32,767 int intcount -2,147,483,648 to 2,147,483,648 lng lnghwnd Single 4 bytes Negative values: -3.402823E+38 to -1.401298E- 45 Positive values: 1.401298E-45 to 3.402823E+38 sng sngpi Double 8 bytes Negative values: -1.79769313486232e+308 to - 4.94065645841247E-324 Positive values: 4.94065645841247E-324 to 1.79769313486232e+308 dbl dblangle Currency 8 bytes -922,337,203,685,477.5808 to 922,337,203,685,477.5807 cur curtotalcost Non-numeric Data Types Type Size Range of Values Prefix Example Variable Name String(fixed length) String(variable length) Length of string Length + 10 bytes 1 to 65,400 characters str strname 0 to 2 billion characters str strhtml Date 8 bytes January 1, 100 to December 31, 9999 dtm dtmbirth Boolean 2 bytes True or False bln blntoggle Object 4 bytes Any embedded object obj objcurrent Variant(numeric) 16 bytes Any value as large as Double vnt vntnumber Variant(text) Length+22 bytes Same as variable-length string vnt vntname Control Types Control Type Prefix TextBox txt

PictureBox pic Label lbl Frame fra CommandButton cmd CheckBox chk RadioButton rad ComboBox cbo ListBox lst Scroll Bar sbr (no orientation needed) Timer tmr DriveListBox drv DirListBox dir FileListBox fil Shape shp Image img Data dat OLE ole ListView lvw TreeView tvw Examples of common object names: txtname txtaddress cboyear cmdok cmdcancel Hopefully this Visual Basic tutorial gave you good idea of all the different objects and data types in VB6. By following these naming standards and using the appropriate types in your code you will produce much better VB code. Understanding variable