Introduction to Visual Basic.NET Chapter 2 Introduction to Controls, Events.

Slides:



Advertisements
Similar presentations
CS0004: Introduction to Programming Visual Studio 2010 and Controls.
Advertisements

Chapter 1: An Introduction to Visual Basic 2012
Chapter 2 –Visual Basic, Controls, and Events
Chapter 1: An Introduction to Visual Basic.NET Programming with Microsoft Visual Basic.NET, Second Edition.
Outline Review Visual Basic.NET Environment Review Visual Basic.NET Environment First Visual Basic Planning and Programming First Visual Basic Planning.
Chapter 31 Visual Basic Controls A Form is a windows-style screen displayed by Visual Basic programs. In a form, a programmer can create objects in a form.
Chapter 2: The Visual Studio.NET Development Environment Visual Basic.NET Programming: From Problem Analysis to Program Design.
1 An Introduction to Visual Basic Objectives Explain the history of programming languages Define the terminology used in object-oriented programming.
Chapter 2 –Visual Basic, Controls, and Events
CS0004: Introduction to Programming Events. Review  Event Procedure  A set of instructions to be executed when a certain event happens.  Many event-driven.
Microsoft Visual Basic 2010: Reloaded Fourth Edition Chapter One An Introduction to Visual Basic 2010.
1 INF110 Visual Basic Programming AUBG Spring semester 2011 Reference books: Schneider D., An Introduction to Programming Using Visual Basic, Prentice.
Microsoft Visual Basic 2012 CHAPTER TWO Program and Graphical User Interface Design.
Visual Basic Fundamental Concepts. Integrated Development Enviroment Generates startup form for new project on which to place controls. Features toolbox.
Visual Basic Chapter 1 Mr. Wangler.
Microsoft Visual Basic 2005: Reloaded Second Edition
Chapter 8: Writing Graphical User Interfaces
Tutorial 1: An Introduction to Visual Basic.NET1 Tutorial 1 An Introduction to Visual Basic.NET.
Clearly Visual Basic: Programming with Visual Basic 2008 Chapter 4 I Need a Tour Guide.
Chapter 3: Using GUI Objects and the Visual Studio IDE.
Chapter 3 - VB.NET by Schneider1 Chapter 3 – Fundamentals of Programming in VB.NET VB.NET Controls VB.NET Events Numbers Strings Input and Output.
Chapter 3 – Fundamentals of Programming in VB.NET VB.NET Controls VB.NET Events Numbers Strings Input and Output.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 2 The Visual Basic.NET Integrated Development Environment.
Chapter 8: Writing Graphical User Interfaces Visual Basic.NET Programming: From Problem Analysis to Program Design.
Copyright © 2001 by Wiley. All rights reserved. Chapter 2: Using Visual Basic to Create a First Project Getting Started with VB Development Environment.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 7 Using Menus, Common Dialogs, Procedures, Functions, and Arrays.
 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.
Chapter 2 –Visual Basic, Controls, and Events
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 - VB 2008 by Schneider1 Chapter 2 –Visual Basic, Controls, and Events 2.1 An Introduction to Visual Basic 2.2 Visual Basic Controls 2.3 Visual.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 8 Debugging, Creating Executable Files, and Distributing a Windows Application.
What is Visual Basic.NET? 110 B-1 e.g. a word processor doesn’t do anything until the user clicks on a button, types text... A programming language that.
3.2 VB.NET Events An Event Procedure Properties and Event Procedures of the Form Tab Order of Controls Exercises.
CSC 230 (Blum)1 Visual Basic 2005 Hello World Fall 2005 T. Blum.
Chapter 3 - VB.NET by Schneider1 Chapter 3 – Fundamentals of Programming in VB.NET Part I VB.NET Controls VB.NET Events.
 2002 Prentice Hall. All rights reserved. 1 Chapter 2 – Introduction to the Visual Studio.NET IDE Outline 2.1Introduction 2.2Visual Studio.NET Integrated.
Microsoft Visual Basic 2008: Reloaded Third Edition Chapter One An Introduction to Visual Basic 2008.
Copyright (c) 2003 by Prentice Hall Provided By: Qasim Al-ajmi Chapter 2 Introduction to Visual Basic Programming Visual Basic.NET.
Object-Oriented Application Development Using VB.NET 1 Chapter 2 The Visual Studio.NET Development Environment.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 3 Building an Application in the Visual Basic.NET Environment.
Chapter 31 Fundamentals of Programming in Visual Basic (VB) Visual Basic Events Simple Statement.
CSC 230 (Blum)1 Visual Basic 2005 Hello World Fall 2005 T. Blum.
Visual Basic.NET Programming for the Rest of Us Keith Mulbery Utah Valley State College.
Chapter 31 Fundamentals of Programming in Visual Basic (VB) Visual Basic Events Simple Statement.
IE 411/511: Visual Programming for Industrial Applications Lecture Notes #2 Introduction to the Visual Basic Express 2010 Integrated Development Environment.
 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.
Microsoft Visual Basic 2012: Reloaded Fifth Edition Chapter One An Introduction to Visual Basic 2012.
Chapter 2 - VB 2008 by Schneider1 Chapter 2 –Visual Basic, Controls, and Events 2.1 An Introduction to Visual Basic 2.2 Visual Basic Controls 2.3 Visual.
Chapter 2: The Visual Studio.NET Development Environment Visual Basic.NET Programming: From Problem Analysis to Program Design.
ALMAJMA'AH UNIVERSITY College of Science and Humanitarians Studies in Alghat Information Technology Section (211Tal course) 1.
Chapter 1: An Introduction to Visual Basic .NET
Chapter 2: The Visual Studio .NET Development Environment
Chapter 1: An Introduction to Visual Basic 2015
Chapter 8: Writing Graphical User Interfaces
Do Now: What are the three steps to writing a Visual Basic Program?
Chapter 3 Fundamentals of Programming in Visual Basic 3
Chapter 2 – Introduction to the Visual Studio .NET IDE
CIS16 Application Development Programming with Visual Basic
Building an Application in the Visual Basic .NET Environment
Fundamentals of Programming in VB.Net
Introduction to Programming
Presentation transcript:

Introduction to Visual Basic.NET Chapter 2 Introduction to Controls, Events

VB.NET Controls Invoking VB.NET A Text Box Walkthrough A Button Walkthrough A Label Walkthrough A List Box Walkthrough The Name Property A Help Walkthrough Fonts / Auto Hide

A Text Box Walkthrough Drag Text Box from ToolBox Sizing Delete Properties –Text, Color, Font, Size, Location, Visible, Enabled

A Button Walkthrough Add the button Change the Text property

Add an "access key"

A Label Walkthrough Add the Label Change the Text property Resize the control

A List Box Walkthrough Add the List Box Add data Resize the control

The Name Property How the programmer refers to a control in code Name must begin with a letter Must be less than 215 characters long May include numbers and the underscore Naming convention: use appropriate 3 character naming prefix –First three letters identifies the type of control –Remaining letters identifies the purpose –E.g. a text box to store a social security number would be called txtSocialSecurity

Common Control Name Prefixes

Fonts Proportional width fonts take up less space for "I" than for "W" – like Microsoft Sans Serif Fixed-width fonts take up the same amount of space for each character – like Courier New Fixed-width fonts are good for tables

Auto Hide Hides tool windows when not in use Vertical push pin icon indicates auto hide is disabled Click the push pin to make it horizontal and enable auto hide

Viewing the Code The GUI Forms Designer generates textual code –Prior to VB programmers wrote everything in textual code Click on the “Form1.VB” tab to see the code (not the design tab)

An Event Procedure Walkthrough An event is an action, such as: –The user clicks on a button –A form is minimized –The mouse enters or exits a control –The form is re-drawn Usually, nothing happens until an event occurs

The three steps in creating a VB.NET program: 1.Create the interface; that is, generate, position, and size the objects. 2.Set properties; that is, configure the appearance of the objects. 3.Write the code that executes when events occur.

Changing Properties Properties are changed in code with the following: controlName.property = setting This is an assignment statement Examples: txtBox.ForeColor = Color.Red txtName.Text = "Hello There" txtName.Visible = False txtName.Location.X = 100

Adding Code to an Event To add code for an event: –In the VB Code Window select the control on the left side menu and the event of interest on the right side menu –Or double-click the control in the designer to bring up the most common event for that control Other methods for opening the Code window: –If the Code window is visible, click on it –Double-click anywhere on the Form window –Select the Code option from the View menu –Press the F7 method key anywhere on the design form –Select the View Code icon from the Project Window

Program Region

Event Procedures Private Sub objectName_event(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles objectName.event For now you can ignore most of this, aside from knowing the name of the subroutine: Private Sub objectName_event(…) Handles objectName.event

Structure of an Event Procedure Private Sub objectName_event(...) Handles objectName.event statements ‘ Your code goes here End Sub

IntelliSense Automatically pops up to give the programmer help.

Code for Walkthrough Private Sub txtFirst_TextChanged(...) Handles txtFirst.TextChanged txtFirst.ForeColor = Color.Blue End Sub Private Sub btnRed_Click(...) Handles btnRed.Click txtFirst.ForeColor = Color.Red End Sub Private Sub txtFirst_Leave(...) Handles txtFirst.Leave txtFirst.ForeColor = Color.Black End Sub

Assigning properties in code The following won't work: Form1.Text = "Demonstration" The current form is referred to by the keyword Me. Me.Text = "Demonstration"

The Declaration Statement of an Event Procedure A declaration statement for an event procedure: Private Sub btnOne_Click(...) Handles btnOne.Click The name can be changed at will. For example Private Sub ButtonPushed(...) Handles btnOne.Click Handling more than one event: Private Sub ButtonPushed(...) Handles btnOne.Click, btnTwo.Click

The MessageBox.Show Method The MessageBox.Show method is used to display a box with a message for the user The message box also contains a title and an icon General forms of the MessageBox.Show method –MessageBox.Show(text) –MessageBox.Show(text, caption) –MessageBox.Show(text, caption, buttons) –MessageBox.Show(text, caption, buttons, icon) –MessageBox.Show(text, caption, buttons, icon, defaultbutton) To do: Add a MessageBox.Show to the button click event

Console.WriteLine Another handy way to output information is to the Console: –Console.WriteLine("Hello there") Outputs the message in double quotes and adds a newline –Console.Write("Hello again. ") Outputs the message in double quotes without a newline Useful for debugging, don’t have to push the OK button and clutter up the screen with message boxes

Adding Additional Event Procedures Comments –Explanatory remarks made within a program –Indicated by an apostrophe or the keyword Rem Statement categories –An executable statement causes some specific action to be performed by the compiler or interpreter –A nonexecutable statement is a statement that describes some feature of either the program or its data but does not cause the computer to perform any action

Focus on Program Design and Implementation: Creating a Menu of Forms A main menu form can be displayed as an application’s opening window to provide the user with a summary of what the application can do The main menu form is created as either: –A set of buttons –A menu bar

Figure 2-55: A Sample Main Menu Screen Focus on Program Design and Implementation: Creating a Main Menu (Continued)

Adding a Second Form From the P)roject menu, select Add New Item, and then Windows Form –Default name is “Form2.vb” To display a form: Dim varName As New FormName varName.Show() –E.g.: Dim secondForm as New Form2 secondForm.Show() To make the current form invisible but retain its variables and components: Me.Hide()(could display again with Show()) To destroy a form and unload its variables and components: Me.Close() (need to create a new one to display again)

Todo Implement program that can quit (End statement) and has a sub-form: Main Menu [Show A] [Exit] Form A [ Show B] [Main Menu]

Knowing About: The Help Facility Visual Basic’s Help Facility can be accessed by selecting either the Contents, Search, or Index options from the Help menu The Contents tab displays a Table of Contents for the documentation The Index tab provides both a general index of topics and a text box for user entry of a specific topic The Search tab provides a means of entering a search word or phrase

Knowing About: The Help Facility (Continued) Dynamic Help –The Dynamic Help window displays a list of help topics that changes as you perform operations –To open the Dynamic Help window, click Help on the menu bar and then click Dynamic Help Context-sensitive Help –Context-sensitive Help immediately displays a relevant article for a topic –To use this facility, select an object and press F1

Common Programming Errors and Problems A common problem is not being able to locate all of the elements needed to create an application –Can usually get these windows back from the V)iew menu A common error is forgetting to save a project at periodic intervals at design time

Summary Event-based programs execute program code depending on what events occur, which depends on what the user does GUIs are graphical user interfaces that provide the user with objects that recognize events The term “design time” refers to the period of time when a Visual Basic application is being developed under control of Visual Basic The term “run time” refers to the period of time when an application is executing

Summary A Visual Basic program consists of a visual part and a language part Basic steps for developing a Visual Basic program: –Create the GUI –Set the properties of each object on the interface –Write procedural code A form is used during design time to create a graphical user interface for a Visual Basic application The most commonly placed objects on a form are buttons, labels, and text boxes