VB – Event driven Programming Timers & Motion, Sub & Function Arif Zaman.

Slides:



Advertisements
Similar presentations
Working with Intrinsic Controls and ActiveX Controls
Advertisements

VISUAL BASIC Visual Basic is derived from the Basic language (Beginner’s All-Purpose Symbolic Instruction Code) Visual Basic uses an event-driven programming.
IS660Z Programming Games Using Visual Basic Overview of Cannonball.
Visual Basic: ballistics
© Copyright by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Tutorial 21 - “Cat and Mouse” Painter Application.
Making a cartoon or slideshow. Aside VB comments An apostrophe starts a comment to end of line Some of the following slides have code with comments in.
Programming Lecture #2 CS 101 Autumn 2007 Tariq Jadoon.
VB Controls and Events Week 7: Picture Box, Image Box, Option, Check box, Mouse over, Frames, Shapes.
Mouse draw Using the mouse and a palette to make a picture.
Introduction to Computing Dr. Nadeem A Khan. Lecture 13.
Scope of Variables and Constants A Variable or Constant may exist and be Visible for an entire project, for only one form, or for only one procedure Therefore,
Slide 1 VB Default Controls Text Box, Check Box, Option Button & Frames.
Introduction to Visual Basic. What is Visual Basic? An environment for developing Windows applications Components –A GUI (Graphical User Interface - gooey)
Introduction To Visual Basic 6. Announcements  Thursday, Oct 9th, 7:30PM, C106 Lloyd Douglas (NSF) Diversity in Science-Who needs it? 5 extra credits.
Adding Controls to User Forms. Adding Controls A user form isn’t much use without some controls We’re going to add controls and write code for them Note.
CS0004: Introduction to Programming Events. Review  Event Procedure  A set of instructions to be executed when a certain event happens.  Many event-driven.
Visual Basic Examples.
Sep-05 Slide:1 ActiveX Controls in VB ActiveX Controls in VB6.
3/9/2004 PPCC - Introduction to VB6 Copyright ©2004, Tore Bostrup 1 Introduction to VB6 Week 2.
Multimedia ! Graphics ! Animation ! Sound Tip Of the Day 4 Tip of the Day: When using graphic methods at Form Load, the AutoRedraw property of the container.
Visual Basic Games: Week 3 Global variables, parameters, Select, KeyDown Enable, Visible, Focus State of Game Read chapter 3.
CSC 298 Windows Forms.
Visual Basic Programming With Mr. deVries. Programming A series of instructions that are used to solve a problem Programmers must be open to seeing the.
06/10/ Working with Data. 206/10/2015 Learning Objectives Explain the circumstances when the following might be useful: Disabling buttons and.
Visual Basic Starter Vocabulary. IDE Integrated Development Environment A setting in which programs are written, tested, debugged, etc. You could write.
Programming Examples to Accompany Structure Topic Please use speaker notes for additional information!
Chapter 9 - VB.Net by Schneider1 Chapter 9 – Additional Controls and Objects 9.1 List Boxes, Combo Boxes, and the File-Opening Control The List Box Control.
 Application – another name for a program.  Interface – is what appears on the screen when the application is running.  Program Code – is instructions.
VB Games: Preparing for Memory Brainstorm controls & events Parallel structures (again), Visibility, LoadPicture, User-defined procedures, Do While/Loop,busy.
Arrays Code: Arrays Controls: Control Arrays, PictureBox, Timer.
Some graphics. Projects included A slideshow a dark moon moving phases of the moon billiards Your own icons and bitmaps Pie chart.
COM148X1 Interactive Programming Lecture 7. Topics Today HCI Event Handling.
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”
Arrays1 From time to time an object (a variable, a picture, a label or a command) does not serve as well as a set of objects of a similar kind addressed.
Chapter 2 P. 1 Introducing more controls (on the Toolbox) (Fig. 2.1) - Text box - Frame - Option button - Check box - Image Example P. 44 Figure 2.2 Message.
Visual Basic Games: Week 4 Recap Parallel structures Initialization Prepare for Memory Scoring Shuffling Homework: when ready, move on to next game/chapter.
3.9 to  Also refer to as Program  Contains set of instructions that tells the computer how to perform specific task.  Each line of code is referred.
© 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.
CS 101 Test 2 Study Guide Acronyms RAD - Rapid Application Development IDE - Integrated Development Environment GUI - Graphical User Interface VB - Visual.
EGR 115 Introduction to Computing for Engineers 3D animation in MATLAB Monday 17 Nov 2014 EGR 115 Introduction to Computing for Engineers.
VB Objects & Events (Exercises) School of Business Eastern Illinois University © Abdou Illia, Spring 2003 (Week 3, Friday 1/31/2003)
1 Visual Basic Part I - A tool for customizing your program Principles of GIS
Debugging, Static Variables, ByRef, ByValue Chapt. 6 in Deitel, Deitel and Nieto.
Word Processor Version.2. Methods Visual Basic is –Object Oriented –Event Driven Objects –Properties –Methods.
MIS333k(Poynor) Chapter 2. Intro to VBA Events Where to plug in your programs?
Programming games in Visual Basic Review programming & VB topics Insertion sort. Best times. Generate questions & answer patterns for quiz Lab/Homework:
VB 4 Controls Scrollbar Radio button check box listboxes timers control arrays.
Practical Programming COMP153-08S Lecture: Repetition.
CECS 5020 Computers in Education Forms and Menus.
Controlling Program Flow with Decision Structures.
Visual Basic Review LBS 126. VB programming Project Form 1Form 2Form 3 Text boxButton Picture box Objects Text box Button Objects.
CIS 338: Events Dr. Ralph D. Westfall April, 2011.
More Visual Basic Code: if-then-else, for loops Controls: Multiple forms, List Boxes, Radio buttons, frames,
National Diploma Unit 4 Introduction to Software Development Procedures and Functions.
Chapter 4.  Variables – named memory location that stores a value.  Variables allows the use of meaningful names which makes the code easier to read.
Introduction To Visual Basic 6
VISUAL BASIC 6.0 Designed by Mrinal Kanti Nath.
Using PowerPoint to Scaffold a Text
Lesson 16 Sub Procedures Lesson 17 Functions
Please use speaker notes for additional information!
Event Driven Programming
Introduction to Object-Oriented Programming
Visual Basic Properties, Methods and Events
Visual Basic.
Chapter 6 - Visual Basic Schneider
Breakout in Greenfoot Barb Ericson
Visual Basic: Week 5 Review User defined functions
3.Visual Basic Controls.
Presentation transcript:

VB – Event driven Programming Timers & Motion, Sub & Function Arif Zaman

Form Properties Caption, Font Name (don’t change after starting programming) Top, Left, Width, Height ScaleWidth, ScaleHeight (are very useful to measure the actual working area of the form). They can also be used to set up convenient co- ordinates (read on your own). MousePointer (HourGlass)

Graphics AutoRedraw=TRUE (if graphics) DrawWidth DrawMode (read on your own) DrawStyle FillColor FillStyle

Event Driven Programming Up till now the programmer = user. Essential to learn USER oriented programming. A waiter provides the “menu” and then “waits” for instructions. The customer (user) may want the after-dinner mint first, and the appetizer at the end. We respond to user events.

Some Events Click, DblClick KeyDown, KeyPress, KeyUp MouseDown, MouseMove, MouseUp ReSize (Should reposition and resize things appropriately)

Counting Clicks Private Sub Form_Click() n = n + 1 Print n End Sub You would expect to get different numbers every time you click. What happened? Debug by putting a breakpoint.

Global - Local Variables Dim variable at the top creates global variables. Other variables are local, They are created when a sub is started, and discarded at the end. Forms (and their properties) are global. Try adding the line: Dim n to the top of the previous program. Now it works.

This works Private Sub Form_Click() Form1.Left = Form1.Left + 50 End Sub Because Form.Left is global

Buttons If you want to click other things besides forms, you can make buttons. Caption, Font (others just like form) Events: Click, MouseDown, MouseMove You can mess around with Picture, Style (graphical) DisabledPicture, DownPicture MaskColor and UseMaskColor.

How to keep Tariq busy all day Make a button called Command1 on a form called Form1, with a caption “Hit Me”. Private Sub Command1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single) Command1.Top = Rnd * (Form1.ScaleHeight - Command1.Height) Command1.Left = Rnd * (Form1.ScaleWidth - Command1.Width) End Sub Private Sub Command1_Click() Command1.Caption = "You Win" End Sub

Your own Sub & Function Just copy the format: Private Sub MySub Remember variables will be local unless explicitly made global Subs can have “arguments”. Functions can return ONE value. Private Function ConvertX(x) ConvertX = x * _ Form1.ScaleWidth End Function Private Function ConvertY(y) ConvertY = (1-y) * _ Form1.ScaleHeight End Function Command1.Left = ConvertX(0.5) Command1.Top = ConvertY(0.9)

Slide a button to the left We want a continuous sliding motion. Try Private Sub Command1_Click() For i = 0 To 5000 Command1.Left = i Next i End Sub Slow it down by Private Sub Command1_Click() For i = 0 To Command1.Left = i/100 Next i End Sub

Do it Right: use a Timer Double click the clock icon (timer). This is a control that has no visible appearance on the form. Set the interval property to 4 and perhaps disable it to start off. Private Sub Timer1_Timer() Command1.Left = _ Command1.Left + 10 If Command1.Left = 5000 Then Timer1.Enabled = False End If End Sub Private Sub Command1_Click() Command1.Left = 0 Timer1.Enabled = True Timer1.Interval = 4 End Sub

A Bouncing Ball Position changes by the velocity every time. Velocity increases by a fixed amount every time. When you hit the ground, you bounce. Velocity becomes negative. Position becomes positive. Height = Form.ScaleHeight- radius of ball PosX = PosX + VelX PosY = PosY + VelY VelY = VelY + 10 If PosX>Height Then VelY = -VelY PosX = 2*Height – PosX End If

Slightly Non-Physical Dim VelX Dim VelY Dim Ht Dim Wd Private Sub Form_Load() VelX = 25 VelY = 0 Ht = Form1.ScaleHeight - Shape1.Height Wd = Form1.ScaleWidth - Shape1.Width Timer1.Interval = 20 Timer1.Enabled = True End Sub Private Sub Timer1_Timer() Shape1.Top = Shape1.Top + VelY Shape1.Left = Shape1.Left + VelX VelY = VelY + 10 If Shape1.Top > Ht Then Shape1.Top = 2 * Ht - Shape1.Top VelY = -VelY * 0.9 End If If Shape1.Left > Wd Then Shape1.Left = 2 * Wd - Shape1.Left VelX = -VelX ElseIf Shape1.Left < 0 Then Shape1.Left = -Shape1.Left VelX = -VelX End If End Sub