Wyo VB Lecture Notes - Objects, Methods, & Properties

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

Full file at Programming in Visual Basic 2010

REVIEW OF CHAPTER 1 1

Chapter 2. Creating Applications with Visual Basic Pearson Addison-Wesley. All rights reserved. Addison Wesley is an imprint of

Visual C# Program: Temperature Conversion Program

Chapter 2 Visual Basic Interface

Computer Science 110. NOTES: module 8

LESSON B. The Toolbox Window

Class Test 3. Question 1

Program and Graphical User Interface Design

Using Visual Basic Studio 2008

Chapter 2 Visual Basic, Controls, and Events. 2.1 An Introduction to Visual Basic 2.2 Visual Basic Controls 2.3 Visual Basic Events

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

Access Forms Masterclass 5 Create Dynamic Titles for Your Forms

Using event-driven programming Installing Visual Basic 2015 Touring the Visual Basic 2015 integrated development environment

Visual C# Program: Simple Game 3

Topic Notes: Java and Objectdraw Basics

2 USING VB.NET TO CREATE A FIRST SOLUTION

Overview About KBasic

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

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

Decision Structures. Start. Do I have a test in morning? Study for test. Watch TV tonight. Stop

printf( Please enter another number: ); scanf( %d, &num2);

1Welcome to Visual Basic 2010

Departme and. Computer. CS Intro to. Science with. Objectives: The main. for a problem. of Programming. Syntax Set of rules Similar to.

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

Touring the Mac. S e s s i o n 3 : U S E A N APPLICATION

COPYRIGHTED MATERIAL. Welcome to Visual Basic 2005

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

Click File on the menu bar to view the individual menu items and their associated icons on the File menu.

Creating Web Pages with SeaMonkey Composer

STATS 507 Data Analysis in Python. Lecture 2: Functions, Conditionals, Recursion and Iteration

MICROSOFT WORD 2010 BASICS

Get comfortable using computers

Fruit Snake SECTION 1

PowerPoint Introduction. Video: Slide Basics. Understanding slides and slide layouts. Slide Basics

IT3101 -Rapid Application Development Second Year- First Semester. Practical 01. Visual Basic.NET Environment.

A Second Visual BASIC Application : Greetings

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

NOTES: Variables & Constants (module 10)

The Mathcad Workspace 7

COMSC-031 Web Site Development- Part 2

INTRODUCTION TO VISUAL BASIC 2010

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

VARIABLES. 1. STRINGS Data with letters and/or characters 2. INTEGERS Numbers without decimals 3. FLOATING POINT NUMBERS Numbers with decimals

Excel 2010: Basics Learning Guide

LAB 2 CREATING A COMBINED PROPOSAL

Barchard Introduction to SPSS Marks

Welcome to Visual Basic.NET

Drawing an Integrated Circuit Chip

MULTIMEDIA TRAINING KIT INTRODUCTION TO OPENOFFICE.ORG WRITER HANDOUT

Introduction to: Computers & Programming: Review prior to 1 st Midterm

Lecture (06) Java Forms

Barchard Introduction to SPSS Marks

Full file at

Step by step set of instructions to accomplish a task or solve a problem

PowerPoint 2007 Cheat Sheet

VISUAL GUIDE to. RX Scripting. for Roulette Xtreme - System Designer 2.0. L J Howell UX Software Ver. 1.0

PowerPoint Slide Basics. Introduction

FUNctions. Lecture 03 Spring 2018

a child-friendly word processor for children to write documents

MIDIPoet -- User's Manual Eugenio Tisselli

StudioPrompter Tutorials. Prepare before you start the Tutorials. Opening and importing text files. Using the Control Bar. Using Dual Monitors

Intro. Scheme Basics. scm> 5 5. scm>

Spira Mirabilis. Finding the Spiral tool. Your first spiral

Excel 2013 Part 2. 2) Creating Different Charts

Get to know Word 2007 I: Create your first document Quick Reference Card

Faculty Development Seminar Series Constructing Posters in PowerPoint 2003 Using a Template

Creating Breakout - Part 2

What is VMware View. IMPORTANT: Connecting from Off-Campus. Connecting to View Desktops. Downloading the Client

Visual Basic Course Pack

In Depth: Writer. The word processor is arguably the most popular element within any office suite. That. Formatting Text CHAPTER 23

Microsoft Word 2016 by Prapaporn Techa-angkoon adapted into English by Dr. Prakarn Unachak

Guide to Completing Your Senior English and Government Portfolios

EVENT-DRIVEN PROGRAMMING

Visual Web Development

Excel Tutorial 1

INFORMATICS LABORATORY WORK #4

CROMWELLSTUDIOS. Content Management System Instruction Manual V1. Content Management System. V1

LESSON A. The Splash Screen Application

Learning to use the drawing tools

Modern Programming Languages Lecture An Introduction to SNOBOL Programming Language

Introduction to MS Word XP 2002: An Overview

Adobe Flash CS5. Creating a web banner. Garvin Ling Juan Santa Cruz Bruno Venegas

Basic Intro to ETO Results

Tutorial 03 understanding controls : buttons, text boxes

HOUR 4 Understanding Events

USER GUIDE. MADCAP FLARE 2017 r3. QR Codes

Microsoft Word 2007 Tutorial CIS*1000*DE

Visual Basic 2008 The programming part

1. Create your First VB.Net Program Hello World

GETTING STARTED WITH MICROSOFT WORD 2016

Contents. Foreword. Examples of GeoGebra Applet Construction 1 A Straight Line Graph... 1 A Quadratic Graph... 6 The Scalar Product...

Lesson 4 - Basic Text Formatting

Python allows variables to hold string values, just like any other type (Boolean, int, float). So, the following assignment statements are valid:

Alice. Coverage. Mathematical Expressions, Conditional Statements, Control Structures. Arithmetic Expressions Built-in Functions Conditional Execution

EXCEL BASICS: MICROSOFT OFFICE 2010

SKILL AREA 210: USE A WORD PROCESSING SOFTWARE. Lesson 1: Getting Familiar with Microsoft Word 2007 for Windows...5

CST242 Windows Forms with C# Page 1

Format your assignment

Transcription:

Wyo VB Lecture Notes - Objects, Methods, & Properties Objective #1: Use forms appropriately. A form is a basic building block of a Visual Basic project. Eventually, you'll be creating projects that consist of many forms. You'll also learn how forms can be reused to save programming time and to give more consistency to a large project. A form has number of useful properties including the Text property. The form's Text property appears in the blue title bar at the top of the form. The default value of a form's Text property is Form1. You should change that entry to something like the name of the project. The default Name property of a form is Form1. I do not recommend that you rename a form even though I do recommend that you immediately rename other objects such as labels and buttons. You can think of a form as having two basic parts, its interface and its code. The interface is what the user will see when he/she executes the program. The code is the behind-the-scenes part of the form that contains the crucial logic and code that makes the form work that way the programmer intends it to work. A form and the objects on a form are measured in pixels. The size of the pixels on your monitor depend on your computer's screen resolution settings. Popular screen resolution settings are 640 pixels by 480 pixels, 800 pixels by 600 pixels, or 1024 pixels by 724 pixels where the first number is the width of the screen and the second measurement is the height of the screen. Larger numbers of pixels in the resolution means the screen looks sharper with more detail. For now in this course, it is not recommended that you resize a form to make it fill up more of the computer screen. The Size property of a form is used to determine its size. Actually, it is the two subproperties, Height and Width, of the Size property that determine the size of the form. The Location property of a form is used with its two subproperties, X and Y, to determine the location of the upper-left corner of the form on the screen. The Load method of a form appears in the code window when you double-click anywhere on the interface of the form. Any code typed into the form's Load method executes as soon as the form is loaded. The default form of a project loads as soon as the user executes a project. Objective #2: Identify and use objects. In English class, you learn about grammar and the rules for writing sentences and paragraphs. In computer programming, we refer to this as syntax. Syntax is the set of symbols and operators that are used in Visual Basic including the period symbol, the equals symbol, double quotes, parentheses, etc. along with the rules that you must follow in order to use them correctly. Keywords are commands that are defined in Visual Basic. Examples of keywords are Public, Class, Private, Sub, and End. These words do not represent objects, properties, or methods. Rather they are defined keywords. An error will occur of course if you mispell or overlook a necessary keyword. Objects (also known as controls) are the things that make up a program. Examples of objects include forms, textboxes, buttons, labels and MessageBoxes. Objective #3: Know how to place objects such as textboxes, labels, and buttons on a form and give them proper names. Textboxes, labels, and buttons are three very useful kinds of objects to use in a VB program. There are several ways to place objects on a form. You can double-click a Toolbox icon in order to place that object on the form. You may have to drag objects around the form in order to be able to see newly created ones.

You can drag a Toolbox icon onto a form to place an object there. You can single-click the Toolbox icon to select it and then click and drag on the form itself to place and size an object. You should always rename objects such as buttons and labels AS SOON AS you place them on a form by typing something descriptive and appropriate in the object's Name property. For example, the default name of a button is Button1. You must not double-click the button and begin to type code in its Click method before you have renamed the button with a proper name such as btnclick. The Name property of an object should begin with a proper prefix. Label's should start with lbl, button's should start with btn, and textboxes should begin with txt. After the prefix, you must use a descriptive name for the object. If the descriptive name consists of two or more words, each word should begin with an uppercase letter so it is easier for other programmers to quickly read the name of the object. There should be no spaces or symbols in the name of an object. For example, if the purpose of a button is to clear some labels than a good name would be btnclear. If the purpose of a label is to display the directions to a game then a good name would be lbldirections or even lblgamedirections. Notice the use of an uppercase G and uppercase D. This method of naming variables is called Hungarian Notation. Good names for a button would be: btnclick btncomputeprice btnstep1 Bad names for a button would be: X because it doesn't explain the purpose of the variable ClickMe because it doesn't have a prefix btnclick because the c is not uppercase btnclickme because the m is not uppercase btncomputeprice because it is difficult to read when uppercase letters are not used btn compute price because the name includes spaces btn-compute-price because the name includes the hyphen symbol btn_compute_price because the name includes the underscore symbol btnshowmethe$ because the name includes the $ symbol A partial list of object prefixes can be found at support.microsoft.com/default.aspx?scid=kb;en-us;q110264 even though it doesn't contain some objects such as buttons that were added to recent versions of VB. Another list is found at http://www.rwc.uc.edu/thomas/intro_oop_text/misc/vb_prefixes.html Objective #4: Use Click events to make a program interact with the user when he/she clicks the mouse. Visual Basic makes it easy to create event-driven programs. An event-driven program is one which responds to users' actions such as mouse clicks and mouse movement. Before the 1990's, programming languages were considered to be procedural. The programmer simply wrote lines of code that executed with a sequential flow of control. Those programs were not as interactive as modern Visual Basic event-driven ones. However, sometimes user interactivity is a hindrance to effective program execution. A useful object that is found in practically every VB project is a button. A button allows the user to interact with the VB project since it can be clicked by the user. The Text property of a button is used to present the word or short phrase that the user will be able to read on the button. When a user clicks a button a click event causes the button's Click method to execute. For example, the following method named btnexit_click causes the program to end when the user clicks this button:

Private Sub btnexit_click() Application.Exit() For now, until we learn about menus, you should include an Exit button in the lower-right corner of every form. To have VB create an empty button Click method, you should double-click on a button. Then you can type statements in the body of the method. Those statements will execute when the user clicks the button. A form has a Click method as well that executes when the user clicks anywhere on the form. The following method named Form1_Click causes a message box to appear but only when the form is clicked Private Sub Form1_Click() MessageBox.Show("Hi") To type the MessageBox statement into the form's Click method, it is easiest to open the code window and change the ClassName listbox at the top-left of the code window to "Form1 events" and then change the MethodName listbox at the top-right of the code window to "Click". VB will create an empty Form1_Click method that you can fill in with statements. Textboxes and labels also have Click methods that can be created similarly. Objective #5: Write assignment statements that assign values to properties. Each kind of object has its own set of properties. An object's properties affect the way it is displayed and the way that it interacts with the user and other objects. Examples of properties include Name, Text, Font, BackColor, & ForeColor. Referring to properties is tricky. You must first include the name of the object, followed by a dot operator (. ), and then the name of the property. In the statement lblhello.text = "Hello World" Text is the name of a property. It is a property of the label named lblhello. The value of the Text property is being set to the phrase "Hello World" in this statement. Setting the label's Text property to "Hello World" is called assignment. The whole statement is called an assignment statement. The equals symbol is called the assignment operator because the phrase "Hello World" is being "assigned to" the label lblhello. The phrase "Hello World" is called a string. In the statement, lblstep1.visible = True lblstep1 is the name of a label object. Visible is an example of a property. The line of code causes the label to appear on the screen by setting its Visible property to True. lblstep1.visible = False

would make the label go invisible since it sets the Visible property to False. lblstep1.text = "Design the interface" causes the phrase "Design the interface" to be stored in the Text property of a label named lblstep1. txtmessage.text = "Plan the code" causes the phrase "Plan the code" to be stored in the Text property of a textbox named txtmessage. lbluser.text = lblname.text causes whatever is stored in the Text property of lblname to also be displayed in the Text property of lbluser. In this case the assignment statement works from right to left with whatever is stored on the right of the equals symbol to be copied to the whatever is on the left of the equals symbol. The following statement clears a label named lblphrase lblphrase.text = "" In the statement above, using two double quotes next to each other puts nothingness into the textbox. Two double quotes next to each other is called the empty string (also known as the null string). Do not put a space between the double quotes since a space is not considered to be nothingness. When typing an online password typing extra spaces before or after the password could prevent you from logging in so spaces do count for something according to a computer. Do not confuse the Text property with the Name property of an object. The Text property usually Name property is used to refer to the object in the source code. Name properties can never have a space. Also, Name properties should begin with an appropriate prefix (e.g. lbl, btn, or txt). In the statement, lblstep1.visible = True lblstep1 is the name of a label object. Visible is an example of a property. The line of code causes the label to appear on the screen by setting its Visible property to True. lblstep1.visible = False would make the label go invisible since it sets the Visible property to False.

lbloutput.text = "My Name is Earl" causes the phrase "My Name is Earl" to be stored in the Text property of a label named lbloutput. Notice that you normally find the object being named followed by the dot operator and then the property followed by an equals symbol and then the value that is being stored in the object's property as in lblstep1.visible = True An error occur if you type it backwards like this True = lblstep1.visible Sample exercises in which you have to write an assignment statement Make a label named lblstep2 become invisible. Answer: lblstep2.visible = False Set a label named lblstep3's Text property to the phrase "Write the code" Answer: lblstep3.text = "Write the code" Make the phrase "Design the interface" appear in a label named lblstep1 Answer: lblstep1.text = "Design the interface" Clear a label named lblstep1 so that nothing appears in its Text property. Answer: lblstep1.text = "" Clear a label named lblstep2 by setting its property to the empty string. Answer: lblstep2.text = "" Clear a label named lblstep3. Answer: lblstep3.text = "" Set lblmyname equal to whatever is currently displayed in lblyourname. Answer: lblmyname.text = lblyourname.text Display whatever is currently found in lblyourname in a label named lblmyname. Answer: lblmyname.text = lblyourname.text Objective #6: Make use of the concatenation operator. The + symbol is sometimes used as an addition operator to add two numbers like it is in mathematics. In this example lblanswer.text = 10 + 5 The number 15 will appear in the label named lblanswer. However in other situations the + symbol is used as the concatenation operator. Concatenation occurs when you

join two pieces of text (also known as strings) together as in lblname.text = "Wyomissing" + "Spartans" in which case the text "WyomissingSpartans" will appear in the label named lblname. If lblname.text currently stores the name "Earl" then the statement lbloutput.text = "My Name is " + lblname.text causes the phrase "My Name is Earl" to be stored in lbloutput. However, if a number such as 5 is typed into lblnumber and the following statement executes lblanswer.text = 10 + lblnumber.text the number 15 will appear in lblanswer since the mathematical sum of 10 plus 5 is computed and stored in lblanswer. In this case, VB treats the plus symbol as an addition operator since it knows that it is surrounded by two numbers rather than pieces of text. In this example MessageBox.Show("My name is " + lblname.text) the phrase "My name is Earl" will appear in a message box if the word "Earl" is stored in lblname. In this example lbloutput.text = "Mr. " + txtname.text the phrase "Mr. Minich" will appear in the label named lbloutput if the textbox txtname stores the word "Minich". In this example MessageBox.Show("Penn" + "sylvania") the word "Pennsylvania" will appear in a message box. Study these sample exercises in which you have to write statements that use concatenation: Use the + concatenation operator to concatenate what is stored in lblschool to the end of the phrase "My school is " and display the whole thing in a message box. Answer: MessageBox.Show("My school is " + lblschool.text) Use the + concatenation operator to concatenate "Dr. " to the front of what is stored in a textbox named txtdoctor and display the whole thing in lblfullname. Answer: lblfullname.text = "Dr." + txtdoctor.text Write a line of code that concatenates the words (also known as strings), "Philadelphia" and "Eagles", and

stores the whole thing in a message box. Answer: MessageBox.Show("Philadelphia" + "Eagles") Objective #7: Identify and use methods. Methods are actions that a programmer can use with objects. Examples of methods include Load, Show, Click, Exit, & Clear. In the statement Application.Exit() Exit is the name of a method. This statement can be used to end the program. Notice that methods are always followed by a set of parentheses. In the statement MessageBox.Show("Hello World") Show is a method. In the statement txtuserinput.clear() Clear is the name of a method. It is a method that works with textbox objects. This statement causes the Text property of a textbox named txtuserinput to be emptied. You should be able to write out a whole method. A method always begins with a line that starts with Private Sub... and a method ends with the line of code. One or more statements are typed inside the body of a method. The body statements are indented for good style. For example, if you are asked to write a method that displays "Hello Jupiter" in a message box when a button named btnclickhere is clicked, here is the answer you should give Private Sub btnclickhere_click() MessageBox.Show("Hello Jupiter") In this class, you are not expected to write the complicated code that goes in the parentheses on the first line of the method. The name of the method is btnclickhere_click however the name of the method includes the name of the object that is being clicked (btnclickhere) followed by an underscore ( _ ) which is followed by the event (i.e. user's interaction with the computer.) If you are asked to write a method that causes the program to end when a button named btnexit is clicked, your answer would be Private Sub btnexit_click() Application.Exit() If you are asked to write a method that causes a label named lblstep1 to disappear when a button named btnclear is clicked, your answer would be Private Sub btnclear_click() lblstep1.visible = False If you are asked to write a method that causes the phrase "I attend Wyomissing Area High School" to appear in a label named lbloutput when a button named btnschool is clicked, your answer would be

Private Sub btnschool_click() lbloutput.text = "I attend Wyomissing Area High School" If you are asked to write a method that causes the phrase "Hello World" to show up in a message box as soon as the program is executed, your answer would be Private Sub Form1_Load() MessageBox.Show("Hello World") Notice that the event is Load since it automatically executes first when a program is started. If you are asked to write a method that displays the total cost of purchasing five items at $50 each in a label named lbltotalcost when the user clicks a button named btncompute, your answer would be Private Sub btncompute_click() lbltotalcost.text = 5 * 50 If you are asked to write a method that displays the total cost of purchasing the number of items typed into a textbox named txtinput where items at priced at $50 each and to show that total cost in a label named lbltotalcost when the user clicks a button named btncompute, your answer would be Private Sub btncompute_click() lbltotalcost.text = txtinput.text * 50 This assumes that the user types a number such as 5 into the textbox.