iTechtalk

Quick Search

Go Advanced

Member Login

Not registered? | Forgot Password
 
Register
Welcome
 
iTechtalk > Tutorials > Programming » Programming Events in VB(Visual Basic)
Reply
Old 12-03-2008, 11:04 AM   #1 (permalink)
 
techans's Avatar
 
Member
Join Date: Nov 2008
Posts: 35
Default Programming Events in VB(Visual Basic)

Events make up a substantial part of the Visual Basic world. They are a key component of what makes Visual Basic so popular and powerful. As every one knows that, event-driven programming is a new way of thinking; it takes the responsibility of designing the flow or control of a program away from the programmer and puts it in the hands of a user through the means of events. A user triggers an event through a keyboard or mouse, and you as a programmer write code to respond to it.

It’s an event-driven world, it works, and users love it whether they know it or not. Anyone who used a program 10 or 20 years ago knows that she had little or no say over program control. The event-driven world has changed that. Users are now able to control program execution through programmable events.

Virtually every object or control in Visual Basic has events associated with it. It is your job as a Visual Basic programmer to write code that responds to these events. Now, it’s not necessary to write code for every event that a given control may have, but you should be aware of what events a user (or a portion of code) can trigger in your program.

You can find a list of programmable events for an object or control in the Visual Basic code window.

In Figure 1.1, I use the list boxes in the code window to view the programmable events of a command button in the name game program.

Figure 1.2 shows another small program that exhibits the usefulness of events. The program is called Around the World. It uses mouse events to trigger the changing of a label’s caption property.





In previous programs, I placed most if not all code into one event procedure. In the Around the World program, I use multiple intrinsic mouse events called DragOver. The program setup is pretty simple; I use seven image controls and one label. I want someone to be able to drag the airplane image around the window, and when the airplane image is over an image of a country, I want the label’s caption property to display the country name.

Here’s all the code for the Around the World program:

Option Explicit
Private Sub Form_DragOver(Source As Control, X As Single, _
Y As Single, State As Integer)
lblLocation.Caption = “”
End Sub

In this code segment, I want the label’s caption property to display nothing when the plane is dragged over the form, but not over an image. I can accomplish this by writing code for the form’s DragOver event. The DragOver event happens when a user clicks on a control and drags it over something. The DragOver event takes parameters that Visual Basic automatically creates for you when you access a DragOver event from the code window. Do not concern yourself with these parameters for now:

Private Sub imgFrance_DragOver(Source As Control, X As Single,_
Y As Single, State As Integer)
lblLocation.Caption = “ You’re now over France”


End Sub
Private Sub imgGermany_DragOver(Source As Control, X As Single,_
Y As Single, State As Integer)
lblLocation.Caption = “ You’re now over Germany”
End Sub
Private Sub imgMexico_DragOver(Source As Control, X As Single,_
Y As Single, State As Integer)
lblLocation.Caption = “ You’re now over Mexico”
End Sub
Private Sub imgUSA_DragOver(Source As Control, X As Single,_
Y As Single, State As Integer)
lblLocation.Caption = “ You’re now over the USA”
End Sub
Private Sub imgItaly_DragOver(Source As Control, X As Single,_
Y As Single, State As Integer)
lblLocation.Caption = “ You’re now over Italy”
End Sub


Private Sub imgJapan_DragOver(Source As Control, X As Single,_
Y As Single, State As Integer)
lblLocation.Caption = “ You’re now over Japan”
End Sub


When the plane is dragged over any of the country images, I want the label’s caption property to change to that of the country name. To make an image (in this case, the airplane) drag-able, you have to set the DragIcon and DragMode Image properties.

The controls and properties of the Around the World program are described in Table :

Note: These icons can be found in the common directory of Visual Studio (that is, C:\Program Files\Microsoft Visual Studio\Common\Graphics).


CONTROL AND PROPERTY SETTINGS

techans is offline   Reply With Quote
 
Reply

Bookmarks

Tags
events, programming, visual basics

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
TEXT BOX PROPERTIES in Visual Basic moderntech Programming 0 12-03-2008 10:44 AM
Naming Conventions in Visual Basic Programming bluechip Programming 0 12-03-2008 10:29 AM
Controls and Properties of Visual Basic (VB) bluechip Programming 0 12-03-2008 10:26 AM
The Visual Basic Landscape bluechip Programming 0 12-03-2008 10:23 AM
Visual Basic Projects bluechip Programming 0 12-03-2008 10:18 AM


 

Content Relevant URLs by vBSEO 3.3.0