PaintPictureBoxDemo Refers to the PaintPictureBoxDemo Visual Basic Program Included With The Lecture.

Slides:



Advertisements
Similar presentations
Chapter 9 Color, Sound and Graphics
Advertisements

Fall 2006AE6382 Design Computing1 Object Oriented Programming in Matlab Intro to Object- Oriented Programming (OOP) An example that creates a ASSET class.
Automating Tasks With Macros
Constructors. You got plenty of experience using constructors in the Marching Band program. A constructor is the subroutine which creates objects from.
Compunet Corporation Programming with Visual Studio.NET GUI Week 13 Tariq Aziz and Kevin Jones.
Inheritance, Shared. Projectiles Program Demonstrates – Inheritance – MustInherit – Shared vs. Non-shared methods A variation on the Multiball example.
Automating Tasks With Macros. 2 Design a switchboard and dialog box for a graphical user interface Database developers interact directly with Access.
Graphics Procedure (IDrawable) 1. Basic Procedure For Drawing ThingsToDraw Create Objects From Classes Add Them to PictureBox Use the Paint Event to draw.
Code Regions and XML Comments. Code Regions The code editor automatically puts little minus signs next to the header line for each Sub or Function. You.
1 An Introduction to Visual Basic Objectives Explain the history of programming languages Define the terminology used in object-oriented programming.
Graphics Object. Group Activity! In each group, pull out one piece of blank paper and one pen or pencil Start with the piece of paper on the left side.
Programming Paradigms Imperative programming Functional programming Logic programming Event-driven programming Object-oriented programming A programming.
CS0004: Introduction to Programming Events. Review  Event Procedure  A set of instructions to be executed when a certain event happens.  Many event-driven.
Chapter 3 Introduction to Event Handling and Windows Forms Applications.
Chapter 9 Introduction to ActionScript 3.0. Chapter 9 Lessons 1.Understand ActionScript Work with instances of movie clip symbols 3.Use code snippets.
Microsoft Visual Basic 2005 CHAPTER 8 Using Procedures and Exception Handling.
Saving and Loading Simple Text Files A sequential file stores characters one after the other like songs on a cassette tape. New characters are added to.
Classes and Class Libraries Examples and Hints November 9,
Hands-on Introduction to Visual Basic.NET Programming Right from the Start with Visual Basic.NET 1/e 6.
Overview of Previous Lesson(s) Over View  OOP  A class is a data type that you define to suit customized application requirements.  A class can be.
Microsoft Visual Basic 2012 Using Procedures and Exception Handling CHAPTER SEVEN.
Microsoft Visual Basic 2005: Reloaded Second Edition
An Object-Oriented Approach to Programming Logic and Design
XP New Perspectives on Microsoft Office Access 2003 Tutorial 11 1 Microsoft Office Access 2003 Tutorial 11 – Using and Writing Visual Basic for Applications.
Introduction to Visual Basic. Quick Links Windows Application Programming Event-Driven Application Becoming familiar with VB Control Objects Saving and.
Chapter 8: Writing Graphical User Interfaces
IE 411/511: Visual Programming for Industrial Applications
Multiple Forms, Container Controls, AddHandler This presentation is based on the Forms and ContainerControls VB Projects 1.
Enhancing JavaScript Using Application Programming Interfaces (APIs)
BIM211 – Visual Programming Objects, Collections, and Events 1.
Introduction to Arrays. definitions and things to consider… This presentation is designed to give a simple demonstration of array and object visualizations.
Visual Basic .NET BASICS
Chapter 8: Writing Graphical User Interfaces Visual Basic.NET Programming: From Problem Analysis to Program Design.
Basic Controls & Properties Chapter 2. Overview u VB-IDE u Basic Controls  Command Button  Label  Text Box  Picture Box u Program Editor  Setting.
Chapter One An Introduction to Visual Basic 2010 Programming with Microsoft Visual Basic th Edition.
Chapter 7 Objects and Classes 1 Fall 2012 CS2302: Programming Principles.
Lecture Set 11 Creating and Using Classes Part B – Class Features – Constructors, Methods, Fields, Properties, Shared Data.
Visual Basic.NET BASICS Lesson 3 Events and Code.
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 Eleven Classes and Objects Programming with Microsoft Visual Basic th Edition.
Variables and Inheritance Part 1
1 Creating Windows GUIs with Visual Studio. 2 Creating the Project New Project Visual C++ Projects Windows Forms Application Give the Project a Name and.
CSCI 3327 Visual Basic Chapter 3: Classes and Objects UTPA – Fall 2011.
Grade Book Database Presentation Jeanne Winstead CINS 137.
Visual Basic CDA College Limassol Campus Lecture:Pelekanou Olga Semester C Week - 1.
Preset and custom animation
Microsoft Visual Basic 2008 CHAPTER ELEVEN Multiple Classes and Inheritance.
Programming with Microsoft Visual Basic 2012 Chapter 11: Classes and Objects.
Class Builder Tutorial Presented By- Amit Singh & Sylendra Prasad.
Visual Basic for Application - Microsoft Access 2003 Programming applications using Objects.
Tutorial for Arrays and Lists. Description This presentation will cover the basics of using Arrays and Lists in an Alice world It uses a set of chickens.
PowerPoint: Animation Randy Graff HSC IT Center Training
Microsoft Visual Basic 2005 BASICS Lesson 3 Events and Code.
Copyright (c) 2003 by Prentice Hall Provided By: Qasim Al-ajmi Chapter 2 Introduction to Visual Basic Programming Visual Basic.NET.
Copyright © Texas Education Agency, All rights reserved.1 Web Technologies Motion Graphics & Animation.
Object-Oriented Programming: Inheritance and Polymorphism.
IE 411/511: Visual Programming for Industrial Applications Lecture Notes #2 Introduction to the Visual Basic Express 2010 Integrated Development Environment.
Procedural programming Procedural programming is where you specify the steps required. You do this by making the program in steps. Procedural programming.
Programming with Visual Basic.NET. Quick Links Program Code The Code Window The Event Procedure Assignment Statements Using AutoList Radio Buttons Buttons.
Variables and Inheritance Part 1 Alice. Review: Properties A class defines properties for its own kind of object. When an object is created (instantiated),
Lecture 8: Collections, Comparisons and Conversions. Svetla Boytcheva AUBG, Spring COS 240 Object-Oriented Languages.
Object-Orientated Programming
Chapter 1: An Introduction to Visual Basic 2015
Microsoft Visual Basic 2005: Reloaded Second Edition
Hands-on Introduction to Visual Basic .NET
Visual programming Chapter 2: Events and Event Handling
Object-Oriented Programming: Inheritance and Polymorphism
CIS16 Application Development and Programming using Visual Basic.net
Chapter 8 - Functions and Functionality
Presentation transcript:

PaintPictureBoxDemo Refers to the PaintPictureBoxDemo Visual Basic Program Included With The Lecture

Topics Demonstrated Inheritance from Existing Classes Events and Event Handling Adding Controls at Runtime Overloaded Constructors Encapsulation

The PaintPictureBoxDemo Program The program consists of: – A custom class, PaintPictureBox, which inherits from the built-in.NET Framework class PictureBox. – A form, frmPPBDemo, which adds an instance of PaintPictureBox at runtime, and has controls to add simple shapes to the PaintPictureBox and change the colors.

PaintPictureBox code Here is the collapsed view of the PaintPictureBox class’s code: The four sections by the plus signs are “regions”. The VB editor allows you to organize your code into regions using the #Region syntax. Regions can be expanded to show all their code by clicking on the plus sign, or collapsed back to a single line by clicking the minus sign. Like comments, regions do not affect how your code runs; they just help you to organize it, and to hide sections of code you are done working on.

Inherits As you will recall, inheritance is one of the four key elements of OOP (along with abstraction, encapsulation, and polymorphism). Inheriting from the built-in.NET classes, especially controls, is one of the most useful ways to use inheritance in your programs. By inheriting from PictureBox, the custom PaintPictureBox has all of the properties, procedures, and other elements of the PictureBox control.

Inherits In fact, if all I did was inherit from PictureBox, I would have a class EXACTLY like PictureBox, just with a different name. Not much point in that, however. We inherit from an existing class so that we can take advantage of its built-in elements, and then add to them. And that’s what the rest of the PaintPictureBox code is: adding new elements to PictureBox to increase its functionality.

Properties and Variables Most of the code in this region should be familiar. The variable “Private mLastMouseLocation As Point” is used with the MouseMove event to draw a line from the last place the mouse was to where it is now (see Sub pbGraphics_MouseMove)

Events and Handlers The MouseDown event provides more detailed information than does the Click event. With Click, a control just says “The user clicked the mouse on me somewhere” With MouseDown, the control identifies which button was clicked (e.Button) and where (e.Location) In this class, I want a left-click to start a line drawing operation, and a right-click to change the values in the Left and Top numeric updowns (where the next shape will be drawn).

Declaring an Event This simple declaration adds an event to the PaintPictureBox class. The event can be raised by other code in the class; in this case, it will be raised in the MouseDown procedure. It includes a parameter, Loc, that will be passed to any routine written to handle this event (in a class/form that uses objects of the PaintPictureBox class; in this case, frmPPBDemo).

Raising the Event This means that whenever this last line of code runs, a flag goes up telling any object using this object (PaintPictureBox) that the right button has been clicked in a certain spot. The calling code can then “handle” the event if it chooses to.

Handling the Event Here’s how the form in this project handles this event: We create a Sub (ChangeStartLocation) that takes the same parameter(s) as the event. We use AddHandler to have that Sub handle the desired event.

Other Uses for Events StartLocationChanged is a somewhat artificial event; I could have accomplished the same thing by handling pbGraphics’ MouseDown event directly in the form code. However, adding events is a very OOP way to add intelligence to your classes. Consider some sort of vehicle class (car, truck, airplane) where the range of the vehicle is limited by the size of the fuel tank. Suppose you have lots of vehicle objects in your program whose fuel tanks are in varying states of emptiness. Your calling code could loop through all of the vehicles each time the timer ticks to see if they are out of gas; or There could be an OutOfFuel event in the Vehicle class which is raised whenever the vehicle’s fuel amount reaches zero. This second choice is the preferred OOP way.

Vehicle Example Obviously, this isn’t a very useful class. It needs additional properties and procedures to be useful. Actual fuel consumption wouldn’t always be 1; it would depend on speed, mpg, and other factors. But as a start, you can see that every time the calling code tells the vehicle object to drive, the fuel remaining will be reduced, and when it runs out of fuel, the vehicle will raise the OutOfFuel event for the calling code to handle (if it wants to).

Overloaded Constructors The PaintPictureBox class has two constructors: – A detailed one allowing the calling code to specify four parameters, and – A simpler one which only takes two parameters. – Note that the simpler one simply calls the detailed one, using pre-selected colors. – This is a very common approach to overloading constructors.

Adding Controls at Runtime The PaintPictureBox control is not added at design time; In fact, it doesn’t even exist in the Toolbox! So how do we get our custom control, inherited from PictureBox, onto the form?

Adding Controls at Runtime Here’s how: 1.Declare a class-level variable. This is how we will reference the object once it is created. 2.Create the object using the constructor. 3.Add the new object to the form’s Controls collection. 4.Add any required/desired event handlers. Here’s the code: A form’s Controls collection contains all controls on the form. (duh) To put a control on a form in code, you must add it to the Controls collection. (not so duh)

Custom Controls in the Toolbox? It is possible to create your own controls, either inherited from existing controls or created from scratch, and use them at design time. That is, they can appear in the toolbox, be placed on the form, and have their properties edited in the properties window. If you will use the control frequently, this is a good idea. However, it is much more work than was involved in creating the PaintPictureBox class. We will not cover this topic this semester. Chapter 22 in the Stevens book describes how it is done. For a custom control that will only be used in a single program, the procedure outlined in this lecture is the simplest and best way to go.

Adding Ordinary Controls at Runtime Regular controls can be added at runtime as well. However, they only have default constructors, so any non-default properties you want to set will require extra lines of code. Example: This can be especially useful when you want to let the user determine the number of controls to add, or when doing animation using controls (next topic!).

PaintPictureBox: Summary The PaintPictureBoxDemo program demonstrates two of the four key elements of OOP: – Inheritance: The PaintPictureBox class inherits from the PictureBox class, which means that it starts life as a fully-functional control capable of displaying pictures and creating graphics. Additional features are then added. – Encapsulation: This is a fairly powerful drawing program. But note that the code in the form consists of just six very short subroutines. Most of the code which makes this program work is encapsulated in the PaintPictureBox class (including, of course, the unseen code inherited from the PictureBox class).