BIM211 – Visual Programming Objects, Collections, and Events 1.

Slides:



Advertisements
Similar presentations
Microsoft® Small Basic
Advertisements

Chapter 9 Color, Sound and Graphics
The Web Warrior Guide to Web Design Technologies
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 21 - “Cat and Mouse” Painter Application.
© by Pearson Education, Inc. All Rights Reserved.
Visual Basic 2010 How to Program. © by Pearson Education, Inc. All Rights Reserved.2.
Visual Basic 2010 How to Program Reference: Instructor: Maysoon Bin Duwais slides Visual Basic 2010 how to program by Deitel © by Pearson Education,
IS 1181 IS 118 Introduction to Development Tools VB Chapter 03.
CA 121 Intro to Programming Tariq Aziz and Kevin Jones GUI Programming in Visual Studio.NET Chapter 1 Tariq Aziz and Kevin Jones.
Compunet Corporation Programming with Visual Studio.NET GUI Week 13 Tariq Aziz and Kevin Jones.
Compunet Corporation Programming with Visual Basic.NET GUI Week # 11 Tariq Ibn Aziz.
Copyright © 2012 Pearson Education, Inc. Chapter 2 Introduction to Visual C#
1 Introduction to the Visual Studio.NET IDE Powerpoint slides modified from Deitel & Deitel.
Fundamentals of Programming in Visual Basic 3.1 Visual basic Objects Visual Basic programs display a Windows style screen (called a form) with boxes into.
BİL528 – Bilgisayar Programlama II Advanced Controls, Menus, Toolbars, and Status Bars 1.
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.
Chapter 3 Introduction to Event Handling and Windows Forms Applications.
Getting Started Example ICS2O curriculum
Slide 1 Chapter 2 Visual Basic Interface. Slide 2 Chapter 2 Windows GUI  A GUI is a graphical user interface.  The interface is what appears on the.
Microsoft Visual Basic 2005 CHAPTER 8 Using Procedures and Exception Handling.
A First Program Using C#
Visual Basic Chapter 1 Mr. Wangler.
Hands-on Introduction to Visual Basic.NET Programming Right from the Start with Visual Basic.NET 1/e 6.
BIL528 – Bilgisayar Programlama II Introduction 1.
05/09/ Introducing Visual Basic Sequence Programming.
BIM313 – Advanced Programming Simple Controls 1. Contents Traditional Controls – Labels, Text Boxes, Buttons, Check Boxes, List Boxes, Combo Boxes Advanced.
Microsoft Visual Basic 2012 Using Procedures and Exception Handling CHAPTER SEVEN.
XP New Perspectives on Microsoft Office Access 2003 Tutorial 11 1 Microsoft Office Access 2003 Tutorial 11 – Using and Writing Visual Basic for Applications.
Microsoft Visual Basic 2008 CHAPTER 8 Using Procedures and Exception Handling.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 26 – CheckWriter Application Introducing Graphics.
Java Programming, 3e Concepts and Techniques Chapter 3 Section 65 – Manipulating Data Using Methods – Java Applet.
© 2006 Lawrenceville Press Slide 1 Chapter 3 Visual Basic Interface.
Chapter 3: Using GUI Objects and the Visual Studio IDE.
Visual Basic .NET BASICS
Chapter 8: Writing Graphical User Interfaces Visual Basic.NET Programming: From Problem Analysis to Program Design.
Starting Out with Visual Basic.NET 2 nd Edition Chapter 2 Creating Applications With Visual Basic.NET.
Chapter One An Introduction to Visual Basic 2010 Programming with Microsoft Visual Basic th Edition.
Program Design and Coding
Microsoft Visual Basic 2012 CHAPTER THREE Program Design and Coding.
Microsoft Visual Basic 2010 CHAPTER THREE Program Design and Coding.
Introduction 1. CIS 400 Programming for the Windows Environment 2.
BIL528 – Bilgisayar Programlama II Introduction 1.
Visual Basic.NET BASICS Lesson 3 Events and Code.
Laboratory Exercise # 9 – Inserting Graphics to Documents Office Productivity Tools 1 Laboratory Exercise # 9 Inserting Graphics to Documents Objectives:
Visual Basic Programming Chapter Six Notes Repetition and the Do Statement ADDING ICONS TO YOUR FORM –It is possible to add an ______________ to your title.
Chapter Two Creating a First Project in Visual Basic.
1 Chapter 12: Form Builder Objects and Flexible Code.
Chapter 2 – Introduction to the Visual Studio .NET IDE
Visual C# 2012 How to Program © by Pearson Education, Inc. All Rights Reserved.
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 21 - “Cat and Mouse” Painter Application.
Visual Basic Programming Introduction VB is one of the High level language VB has evolved from the BASIC language. BASIC stands for Beginners All-purpose.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Programming Logic & Design Second Edition by Tony Gaddis.
Visual Basic for Application - Microsoft Access 2003 Programming applications using Objects.
Microsoft Visual Basic 2005 BASICS Lesson 3 Events and Code.
Copyright (c) 2003 by Prentice Hall Provided By: Qasim Al- ajmi Chapter 3 Some Visual Basic Controls and Events Visual Basic. NET.
Microsoft Visual Basic 2008: Reloaded Third Edition Chapter One An Introduction to Visual Basic 2008.
Programming Logic and Design Fourth Edition, Comprehensive Chapter 14 Event-Driven Programming with Graphical User Interfaces.
Addison Wesley is an imprint of © 2011 Pearson Addison-Wesley. All rights reserved. Chapter 2 Creating Applications with Visual Basic.
PaintPictureBoxDemo Refers to the PaintPictureBoxDemo Visual Basic Program Included With The Lecture.
 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 4.  Variables – named memory location that stores a value.  Variables allows the use of meaningful names which makes the code easier to read.
Visual Basic.NET BASICS Lesson 9 Nested If Statements and Radio Buttons.
Chapter 1: An Introduction to Visual Basic 2015
Chapter 2 – Introduction to the Visual Studio .NET IDE
Visual studio 2010 SENG 403, Tutorial 2 SENG Winter 2011.
Microsoft Access Illustrated
Using Procedures and Exception Handling
Presentation transcript:

BIM211 – Visual Programming Objects, Collections, and Events 1

Contents Objects and Classes Properties Methods Events 2

Objects 3

C# is a true object-oriented programming language. Everything you use in Visual C# is an object – Forms and the controls you put on a form are all objects All objects have attributes (called properties), most have methods, and many have events. Whether creating simple applications or building large-scale enterprise solutions, you must understand what an object is and how it works to be successful. 4

Objects (continued) An object is a programming structure that encapsulates data and functionality as a single unit and for which the only public access is through the programming structure’s interfaces (properties, methods, and events). Whether creating simple applications or building large-scale enterprise solutions, you must understand what an object is and how it works to be successful. 5

“Controls” We used a PictureBox and two Button objects in the last week’s class Both PictureBox and Button objects are Control s They are different objects but they share some properties The shared properties come from the class Control. 6

Properties The attributes of an object which specify and return the state of the object are called properties. You used some properties last week. – What you see in Properties window are all properties of the selected object on the form – Text of a Button, Width and Height of a Form, and Image of a PictureBox are all properties 7

Changing Properties by Code By using the Properties window, you can change the properties of an object in design time. At runtime, you don’t have Properties window When referencing properties in code, you specify the name of the object first, followed by a period (.), and then the property name as in the following syntax: {ObjectName}.{Property} 8

Changing Properties (continued) If you had a button object named btnClickMe, for example, you would reference button’s Text property this way: btnClickMe.Text This line of code would return whatever value was contained in the Text property of the button object btnClickMe. To set a property to some value, you use an equal sign (=). To change the Button object’s Left property, for example, you’d use a line of code such as the following: btnClickMe.Left = 90; 9

Storing a Property in a Temporary Variable The following line of code places the value of the Text property of the button object called btnClickMe into a temporary variable. This statement retrieves the value of the Text property because the Text property is referenced on the right side of the equal sign. strText = btnClickMe.Text; 10

Read-Only Properties Just as in real life, some properties can be read but not changed. Those properties are called read-only properties. One example of a read-only property is the Height property of the combo box control. Although you can view the value of the Height property in the Properties window, you can’t change the value—no matter how hard you try. If you attempt to use Visual C# code to change the Height property, Visual C# simply changes the value back to the default. 11

Exercise Last week, we changed the Size of the Form in the Properties window. Today, we are going to change the Width and Height properties of the Form with Visual C# code. We are going to add two buttons to the Picture Viewer project. One button enlarges the form when clicked, whereas the other shrinks the form. 12

Add the Buttons PropertyValue NamebtnEnlarge Location338, 261 Size21, 23 Text^ 13 PropertyValue NamebtnShrink Location365, 261 Size21, 23 Textv

Enlarging Code Double-click the btnEnlarge button and write the following code: this.Width = this.Width + 20; this.Height = this.Height + 20; 14

Shrinking Code Double-click the btnShrink button and write the following code: this.Width = this.Width - 20; this.Height = this.Height - 20; 15

16

this this refers to the object to which the code belongs (in this case, the form). this is a reserved word; it’s a word that you can’t use to name objects or variables because Visual C# has a specific meaning for it. 17

Intellisense When you type the period, or dot, as it’s called, a small drop-down list containing all properties and methods that you can use appears. Visual Studio is smart enough to realize that this represents the current form (more on this in a moment), and to aid you in writing code for the object, it gives you a drop-down list containing all the properties and methods of the form. This feature is called IntelliSense. This prevents you from misspelling a member name, thereby reducing compile errors. 18

Intellisense 19

Methods 20

Understanding Methods In addition to properties, most objects have methods. Methods are actions the object can perform, in contrast to attributes that describe the object. You can call methods like properties by using dot: {ObjectName}.{MethodName}({Parameters}); 21

Parentheses Method calls in Visual C# must always have parentheses. Sometimes they’ll be empty, but at other times they’ll contain data to pass to the method. Example: MessageBox.Show("Hello World!"); 22

Exercise We are going to modify our PictureViewer project to include a button that, when clicked, draws a colored border around the picture. 23

Creating the Interface Add a new button to the form and set its properties and location (as in pp.69). Write the following code into Click event: Graphics objGraphics = this.CreateGraphics(); objGraphics.Clear(SystemColors.Control); objGraphics.DrawRectangle(Pens.Blue, picShowPicture.Left - 1, picShowPicture.Top - 1, picShowPicture.Width + 1, picShowPicture.Height + 1); objGraphics.Dispose(); Run your program. 24

Graphics Object A Graphics object can be used to draw some geometrical shapes or to write some texts. CreateGraphics() methods of a form returns an object which represents the client area of the form. Anything drawn onto the Graphics object of the form appears on the form. Notice that the values returned by a property don’t have to be traditional values, such as numbers or texts; they could also be objects. 25

The Methods Used in the Code Clear() method clears the background of the form using the color specified. DrawRectangle() method draws a rectangle by using a Pen object on the specified coordinates. Dispose() method cleans up the resources used by the object (like free() in C). 26

Running the Project 27

Collections 28

Understanding Collections A collection is just what its name implies: a collection of objects. Collections make it easy to work with large numbers of similar objects by enabling you to create code that performs iterative processing on items within the collection. Iterative processing is an operation that uses a loop to perform actions on multiple objects, rather than writing the operative code for each object. In addition to containing an indexed set of objects, collections also have properties and might have methods. 29

Collections have their own properties and methods All collections have a Count property. All collections might have an Add() and a Delete() method. 30

Exercise Create a small form that tells the names of all control it contains: – Add some controls to the form and name them uniquely. – Write the code on the following slide to the Click method of the button which is labeled as Show Control Names. 31

The Necessary Code for (int i = 0; i < this.Controls.Count; i++) { MessageBox.Show("Control #" + i.ToString() + " has the name " + this.Controls[i].Name); } 32

Another Version int i = 1; foreach (Control cnt in this.Controls) { MessageBox.Show("Control #" + i.ToString() + " has the name " + cnt.Name); i++; } 33

Events 34

Understanding Events In addition to designing an interface, you have to empower your program to perform actions in response to both how a user interacts with the program and how Windows interacts with the program. You do this by using events. 35

Event-Driven Programming With traditional programming languages, the first line of code in the program executes, and the code continues to execute in a completely predetermined path. Event-driven programs have logical sections of code placed within events. There’s no predetermined order in which events occur; the user often has complete control over what code is executed in an event-driven program by interactively triggering specific events, such as by clicking a button. 36

Event An event is a special kind of method used by an object to signal state changes that might be useful to clients (code using the object). Events can be called in special ways: – Users can trigger the event (e.g. clicking a button) – Objects can trigger their own event (e.g. timers) – The operating system can trigger events – You can trigger an event just by calling its method 37

Exercise: Display Mouse Coordinates Open the PictureViewer project Add two labels for X and Y coordinates Select the picture box and create the new MouseMove event using the Properties window Enter the following code: – lblX.Text = "X: " + e.X.ToString(); – lblY.Text = "Y: " + e.Y.ToString(); 38

Running the Project When you move the mouse out of the picture box, the labels display the latest coordinates! Solution: Handle the MouseLeave event! – lblX.Text = ""; – lblY.Text = ""; 39

Notes The event methods has two parameters: Sender and EventArgs. The mouse event methods have a parameter of type MouseEventArgs instead of EventArgs. Other event methods may have different type of parameter, but still inherited from EventArgs class. The EventArgs object gives useful information related to the event. 40