VB Event driven Programming Timers - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

VB Event driven Programming Timers

Description:

They can also be used to set up convenient co-ordinates (read on your own) ... A waiter provides the 'menu' and then 'waits' for instructions. ... – PowerPoint PPT presentation

Number of Views:139
Avg rating:3.0/5.0
Slides: 16
Provided by: arifz8
Category:

less

Transcript and Presenter's Notes

Title: VB Event driven Programming Timers


1
VB Event driven ProgrammingTimers Motion,
Sub Function
  • Arif Zaman

2
Form Properties
  • Caption, Font
  • Name (dont 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)

3
Graphics
  • AutoRedrawTRUE (if graphics)
  • DrawWidth
  • DrawMode (read on your own)
  • DrawStyle
  • FillColor
  • FillStyle

4
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.

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

6
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.

7
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.

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

9
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.

10
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

11
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)

12
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 500000
  • Command1.Left i/100
  • Next i
  • End Sub

13
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

14
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 PosXgtHeight Then
  • VelY -VelY
  • PosX 2Height PosX
  • End If

15
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 gt Ht Then
  • Shape1.Top 2 Ht - Shape1.Top
  • VelY -VelY 0.9
  • End If
  • If Shape1.Left gt Wd Then
  • Shape1.Left 2 Wd - Shape1.Left
  • VelX -VelX
  • ElseIf Shape1.Left lt 0 Then
  • Shape1.Left -Shape1.Left
  • VelX -VelX
  • End If
  • End Sub
Write a Comment
User Comments (0)
About PowerShow.com