41
CIS 115 Lecture 2

CIS 115 Lecture 2. Visual Studio 2005 Professional Edition (Requires Windows XP Pro) MSDN Library for Visual Studio 2005 Available from MSDNAA

Embed Size (px)

Citation preview

Page 1: CIS 115 Lecture 2.  Visual Studio 2005 Professional Edition (Requires Windows XP Pro)  MSDN Library for Visual Studio 2005 Available from MSDNAA

CIS 115 Lecture 2

Page 2: CIS 115 Lecture 2.  Visual Studio 2005 Professional Edition (Requires Windows XP Pro)  MSDN Library for Visual Studio 2005 Available from MSDNAA

Visual Studio 2005 Professional Edition(Requires Windows XP Pro)

MSDN Library for Visual Studio 2005

Available from MSDNAA

Page 3: CIS 115 Lecture 2.  Visual Studio 2005 Professional Edition (Requires Windows XP Pro)  MSDN Library for Visual Studio 2005 Available from MSDNAA

A platform that allows the development and deployment of desktop and web applications

Allows user choice of many .NET languages May program in One of them May create different parts of application in

different languages▪ Visual Basic▪ C# (C Sharp)▪ C++▪ J++▪ Etc.

Page 4: CIS 115 Lecture 2.  Visual Studio 2005 Professional Edition (Requires Windows XP Pro)  MSDN Library for Visual Studio 2005 Available from MSDNAA

Integrated Development Environment – allows the automation of many of the common programming tasks in one environment Writing the code Checking for Syntax (Language) errors Compiling and Interpreting(Transferring to

computer language) Debugging (Fixing Run-time or Logic

Errors) Running the Application

Page 5: CIS 115 Lecture 2.  Visual Studio 2005 Professional Edition (Requires Windows XP Pro)  MSDN Library for Visual Studio 2005 Available from MSDNAA

4th Generation Programming Environment / Development Language

Based on BASIC language Beginners All-Purpose Symbolic Instructional

Code Most widely used tool for developing

Windows Applications Graphical User Interface (GUI) Menus, Buttons, Icons to help the user

Full Object-Oriented Programming Language

Page 6: CIS 115 Lecture 2.  Visual Studio 2005 Professional Edition (Requires Windows XP Pro)  MSDN Library for Visual Studio 2005 Available from MSDNAA

Solution

.NET FrameworkVisual Studio .NET

Project

CommonLanguageRuntime

IntegratedDevelopmentEnvironment

Source files

Visual Basiccompiler

1 2 3

Assembly

Intermediate Language (IL)

Class references

Page 7: CIS 115 Lecture 2.  Visual Studio 2005 Professional Edition (Requires Windows XP Pro)  MSDN Library for Visual Studio 2005 Available from MSDNAA

User creates a new project in Visual Studio A solution and a folder are created at the same time with the

same name as the project The project belongs to the solution Multiple projects can be included in a solution

Solution Contains several folders that define an application’s structure Solution files have a file suffix of .sln

Project: contains files for a part of the solution Project file is used to create an executable application A project file has a suffix of .vbproj Every project has a type (Console, Windows, etc.) Every project has an entry point: A Sub procedure named

Main or a Form

Page 8: CIS 115 Lecture 2.  Visual Studio 2005 Professional Edition (Requires Windows XP Pro)  MSDN Library for Visual Studio 2005 Available from MSDNAA

Solution folder Solution file (.sln) Project folder

▪ Project file (.vbproj)▪ Visual Basic source files (.vb)▪ My Project folder: contains configuration information

common to all projects▪ The file AssemblyInfo.vb contains assembly metadata▪ The References folder contains references to other

assemblies

▪ The bin folder contains the executable file produced as a result of compiling the application

Page 9: CIS 115 Lecture 2.  Visual Studio 2005 Professional Edition (Requires Windows XP Pro)  MSDN Library for Visual Studio 2005 Available from MSDNAA
Page 10: CIS 115 Lecture 2.  Visual Studio 2005 Professional Edition (Requires Windows XP Pro)  MSDN Library for Visual Studio 2005 Available from MSDNAA

Select the “Create Project” option from the “Recent Projects” box on the Start Page

Page 11: CIS 115 Lecture 2.  Visual Studio 2005 Professional Edition (Requires Windows XP Pro)  MSDN Library for Visual Studio 2005 Available from MSDNAA
Page 12: CIS 115 Lecture 2.  Visual Studio 2005 Professional Edition (Requires Windows XP Pro)  MSDN Library for Visual Studio 2005 Available from MSDNAA

This is a Visual BasicGUI object called a form

Forms are the windows and dialog boxes that display when a program runs.

A form is an object that contains other objects such as buttons, text boxes, and labels

Page 13: CIS 115 Lecture 2.  Visual Studio 2005 Professional Edition (Requires Windows XP Pro)  MSDN Library for Visual Studio 2005 Available from MSDNAA

Form elements are objects called controls

This form has: Two TextBox controls Four Label controls Two Button controls

The value displayed by a control is held in the text property of the control

Left button text property is Calculate Gross Pay

Buttons have methods attached to events

Page 14: CIS 115 Lecture 2.  Visual Studio 2005 Professional Edition (Requires Windows XP Pro)  MSDN Library for Visual Studio 2005 Available from MSDNAA

Design WindowT

oolbox

SolutionExplorer

PropertiesWindow

Page 15: CIS 115 Lecture 2.  Visual Studio 2005 Professional Edition (Requires Windows XP Pro)  MSDN Library for Visual Studio 2005 Available from MSDNAA

Step 1: Add a Control to the Form – Button Look in the Toolbox for the Button Control Select the Button with the Mouse Draw a Rectangle Region in the Design

Window by holding the mouse button down Release the mouse button to see your

button (Can also be added by double clicking on

the button in the Toolbox)

Page 16: CIS 115 Lecture 2.  Visual Studio 2005 Professional Edition (Requires Windows XP Pro)  MSDN Library for Visual Studio 2005 Available from MSDNAA
Page 17: CIS 115 Lecture 2.  Visual Studio 2005 Professional Edition (Requires Windows XP Pro)  MSDN Library for Visual Studio 2005 Available from MSDNAA

Add a Second Button to the FormPut it in the lower right corner

The project now contains a form with 2 button controls

Page 18: CIS 115 Lecture 2.  Visual Studio 2005 Professional Edition (Requires Windows XP Pro)  MSDN Library for Visual Studio 2005 Available from MSDNAA

Properties All controls have properties Each property has a value (or values) Determine the Look and Feel (and

sometimes behavior) of a Control Set initially through the Properties Window

Properties Set for this Application Name Text

Page 19: CIS 115 Lecture 2.  Visual Studio 2005 Professional Edition (Requires Windows XP Pro)  MSDN Library for Visual Studio 2005 Available from MSDNAA

The name property establishes a means for the program to refer to that control

Controls are assigned relatively meaningless names when created

Change these names to something more meaningful

Control names must start with a letter Remaining characters may be letters,

digits, or underscore

Page 20: CIS 115 Lecture 2.  Visual Studio 2005 Professional Edition (Requires Windows XP Pro)  MSDN Library for Visual Studio 2005 Available from MSDNAA

btnCalcGrossPay btnClose

txtHoursWorked

txtPayRate

lblGrossPay

Label1

Label2

Label3

The label controls use the default names (Label1, etc.)

Text boxes, buttons, and the Gross Pay label play an active role in the program and have been changed

Page 21: CIS 115 Lecture 2.  Visual Studio 2005 Professional Edition (Requires Windows XP Pro)  MSDN Library for Visual Studio 2005 Available from MSDNAA

Should be meaningful 1st 3 lowercase letters indicate the type of

control txt… for Text Boxes lbl… for Labels btn… for Buttons

After that, capitalize the first letter of each word txtHoursWorked is clearer than txthoursworked Change the name property

Set the name of button1 to btnWelcome Set the name of button2 to btnExit

Page 22: CIS 115 Lecture 2.  Visual Studio 2005 Professional Edition (Requires Windows XP Pro)  MSDN Library for Visual Studio 2005 Available from MSDNAA

Click on the Control in the Design Window

Select the appropriate property in the Properties Window

Page 23: CIS 115 Lecture 2.  Visual Studio 2005 Professional Edition (Requires Windows XP Pro)  MSDN Library for Visual Studio 2005 Available from MSDNAA

Determines the visible text on the control

Change the text property bntWelcome set to “Say Welcome” btnExit set to “Exit”

Do not need to include the “ “ in your text field

Notice how the buttons now display the new text

Page 24: CIS 115 Lecture 2.  Visual Studio 2005 Professional Edition (Requires Windows XP Pro)  MSDN Library for Visual Studio 2005 Available from MSDNAA

The GUI environment is event-drivenAn event is an action that takes place

within a program Clicking a button (a Click event) Keying in a TextBox (a TextChanged event)

Visual Basic controls are capable of detecting many, many events

A program can respond to an event if the programmer writes an event procedure

Page 25: CIS 115 Lecture 2.  Visual Studio 2005 Professional Edition (Requires Windows XP Pro)  MSDN Library for Visual Studio 2005 Available from MSDNAA

An Event Procedure is a block of code that executes only when particular event occurs

Writing an Event Procedure Create the event procedure stub

▪ Double click on control from Design Window – for default event for that control

OR▪ Open the Code Editor (F7 or View Menu/Code option)▪ Select Control & Select Event from drop down windows

in Code Editor Add the event code to the event procedure stub

Page 26: CIS 115 Lecture 2.  Visual Studio 2005 Professional Edition (Requires Windows XP Pro)  MSDN Library for Visual Studio 2005 Available from MSDNAA
Page 27: CIS 115 Lecture 2.  Visual Studio 2005 Professional Edition (Requires Windows XP Pro)  MSDN Library for Visual Studio 2005 Available from MSDNAA
Page 28: CIS 115 Lecture 2.  Visual Studio 2005 Professional Edition (Requires Windows XP Pro)  MSDN Library for Visual Studio 2005 Available from MSDNAA

Select the btnWelcome control from the Form Controls List Box

Page 29: CIS 115 Lecture 2.  Visual Studio 2005 Professional Edition (Requires Windows XP Pro)  MSDN Library for Visual Studio 2005 Available from MSDNAA

Select the Click event from the list of many available events

Buttons have 57 possible events they can respond to

Page 30: CIS 115 Lecture 2.  Visual Studio 2005 Professional Edition (Requires Windows XP Pro)  MSDN Library for Visual Studio 2005 Available from MSDNAA

Beginning of Procedure is created for you If you create stub by double clicking on

control it will create a stub for the most commonly used event for that control

Page 31: CIS 115 Lecture 2.  Visual Studio 2005 Professional Edition (Requires Windows XP Pro)  MSDN Library for Visual Studio 2005 Available from MSDNAA

Write the code that you want executed when the user clicks on the btnWelcome button Type: MsgBox (“Welcome to Visual Basic”)

Must be contained within the Event Procedure Stub

Page 32: CIS 115 Lecture 2.  Visual Studio 2005 Professional Edition (Requires Windows XP Pro)  MSDN Library for Visual Studio 2005 Available from MSDNAA

Not Case Sensitive Visual Basic will “correct” case issues for

youKeywords are in Blue

Special reserved wordsComments in GreenProblems with Syntax (Language)

will be underlined in blue

Page 33: CIS 115 Lecture 2.  Visual Studio 2005 Professional Edition (Requires Windows XP Pro)  MSDN Library for Visual Studio 2005 Available from MSDNAA

Rules Use spaces to separate the words and

operators Indentation and capitalization have no effect

Recommendations Use indentation and extra spaces for alignment Use blank lines before and after groups of

related statements Code all variable declarations at the start of the

procedure Group related declarations

Page 34: CIS 115 Lecture 2.  Visual Studio 2005 Professional Edition (Requires Windows XP Pro)  MSDN Library for Visual Studio 2005 Available from MSDNAA

Usage Type an apostrophe ( ' ) followed by the comment The compiler ignores everything on the line after ‘ Used for documentation/readability and to disable

chosen statements during testing Recommendations

Follow apostrophe with a star for readability ( ‘* ) Use at beginning of program to indicate author,

purpose, date, etc. Use for groups of related statements and portions of

code that are difficult to understand

Page 35: CIS 115 Lecture 2.  Visual Studio 2005 Professional Edition (Requires Windows XP Pro)  MSDN Library for Visual Studio 2005 Available from MSDNAA

'* ======================================'* Class: CIS 115-101'* Author: Paul Overstreet'* Purpose: Homework 1 – VB Application'* Date: 11/30/01'* ======================================

Public Class Form1

Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventAr…

'*Variable declarations Dim dOrderTotal As Decimal Dim dDiscountAmount As Decimal

'*Get total from textbox dOrderTotal = txtOrderTotal.Text

'*Calculate the proper discount dDiscountAmount = dOrderTotal * 0.25' dDiscountAmount = dOrderTotal * 0.25

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)…

‘*Code goes here

End Sub

End Class

Page 36: CIS 115 Lecture 2.  Visual Studio 2005 Professional Edition (Requires Windows XP Pro)  MSDN Library for Visual Studio 2005 Available from MSDNAA

Create an Event Procedure for when the btnExit button is clicked

Have it display “Goodbye” in a MsgBox

Then “End” – this will terminate the program

Page 37: CIS 115 Lecture 2.  Visual Studio 2005 Professional Edition (Requires Windows XP Pro)  MSDN Library for Visual Studio 2005 Available from MSDNAA

You can switch between the Design Window and the Code Window (once opened) by clicking on the tabs at the top of the

Design and Code Windows Form1.vb(Design) is the design window Form1.vb is the Code Window

Page 38: CIS 115 Lecture 2.  Visual Studio 2005 Professional Edition (Requires Windows XP Pro)  MSDN Library for Visual Studio 2005 Available from MSDNAA

Click the Run Icon on the Standard Toolbar

Or Press F5

This will begin the programDisplay the Form/WindowNothing will happen

Waiting on an Event

Page 39: CIS 115 Lecture 2.  Visual Studio 2005 Professional Edition (Requires Windows XP Pro)  MSDN Library for Visual Studio 2005 Available from MSDNAA

Click on the “Say Welcome” button The message box should display

Click on the “Exit” button The message box should display The application should terminate

Page 40: CIS 115 Lecture 2.  Visual Studio 2005 Professional Edition (Requires Windows XP Pro)  MSDN Library for Visual Studio 2005 Available from MSDNAA

Make sure to save your work SAVE ALL (not Save Form) Visual Basic applications are made of several files - Often even several forms

Page 41: CIS 115 Lecture 2.  Visual Studio 2005 Professional Edition (Requires Windows XP Pro)  MSDN Library for Visual Studio 2005 Available from MSDNAA

Lab Handout Intro to VB Controls and Properties See handout for details and due date Questions?