Instead of a procedural approach to programming, applications are now ''event driven''. In essence, the developer defines what the user interface is to look like, and then defines subroutines which will be called when particular events are triggered. For example, the Visual Basic programmer might create a Form containing a number of Buttons. For each Button, the programmer will define the Visual Basic code to be executed when a ''Click'' event is triggered on each button. When the application runs, an ''event loop'' sits in the background and waits for an event to be triggered in the application. Once an event is triggered (such as button click or a a keystroke in a text field) the event loop looks at the object from which the event was triggered to see if an event handler has been defined by the developer for that particular event type. If a handler has been defined, that subroutine is executed and the event loop continues to loop waiting for the next event to be triggered.
 
Instead of a procedural approach to programming, applications are now ''event driven''. In essence, the developer defines what the user interface is to look like, and then defines subroutines which will be called when particular events are triggered. For example, the Visual Basic programmer might create a Form containing a number of Buttons. For each Button, the programmer will define the Visual Basic code to be executed when a ''Click'' event is triggered on each button. When the application runs, an ''event loop'' sits in the background and waits for an event to be triggered in the application. Once an event is triggered (such as button click or a a keystroke in a text field) the event loop looks at the object from which the event was triggered to see if an event handler has been defined by the developer for that particular event type. If a handler has been defined, that subroutine is executed and the event loop continues to loop waiting for the next event to be triggered.
   −
== Event Triggers ==
+
== Visual Event Triggers ==
    
There are a number of ways events can be triggered:
 
There are a number of ways events can be triggered:
 
* Operating System - The Windows operating system can trigger events which can be handled by the application. For example, Windows will trigger an event when part of a window is obscured by another window. In this situatio,n Visual Basic receives an event when the previously obscured window area is re-exposed so that it knows to repaint the area.
 
* Operating System - The Windows operating system can trigger events which can be handled by the application. For example, Windows will trigger an event when part of a window is obscured by another window. In this situatio,n Visual Basic receives an event when the previously obscured window area is re-exposed so that it knows to repaint the area.
   −
* Programmitically triggered events - The event of any object may be triggered by the programmer from within the Visual Basic code. For example, a program may need to simulate the event triggered by a user clicking on a button.
+
* Programmatically triggered events - The event of any object may be triggered by the programmer from within the Visual Basic code. For example, a program may need to simulate the event triggered by a user clicking on a button.
    
== Wiring Up Events in Visual Studio ==
 
== Wiring Up Events in Visual Studio ==
   −
In this section we will look at the steps involved in configuring events on controls in a Form. Create a new Windows Application project in Visual Studio (see [[Creating a New Visual Basic Project]] for details on how to do this).
+
In this section we will look at the steps involved in configuring events on controls in a Form. Create a new Windows Application project in Visual Studio called ''VBevents'' (see [[Creating a New Visual Basic Project]] for details on how to do this).
    
In this example we are going to create a Form containing a TextBox, a Label and a Button. We are then going to set up event handlers such that any text typed into the TextBox will appear in the Label control. We will also wire up an event handler on the Button to close the application when the button is pressed.
 
In this example we are going to create a Form containing a TextBox, a Label and a Button. We are then going to set up event handlers such that any text typed into the TextBox will appear in the Label control. We will also wire up an event handler on the Button to close the application when the button is pressed.
 
[[Image:visual_basic_event_form.jpg]]
 
[[Image:visual_basic_event_form.jpg]]
   −
Select each control in turn and using the Properties panel change the Name of the controls to ''myButton'', ''myLabel'' and ''inputText'' respectively. The Form design is now complete and we can now begin to connect the events to event handlers.
+
Select each control in turn and using the Properties panel change the Name of the controls to ''myButton'', ''myLabel'' and ''textInput'' respectively. The Form design is now complete and we can now begin to connect the events to event handlers.
   −
We will begin by defining an event procedure for the TextBox. To access the ''inputText'' control event procedures simply double click on the TextBox control in the Form. Visual Studio will subsequently display the event procedures for the ''inputText'' control. By default Visual Studio picks the most common event, the ''TextChanged'' event, and creates a stub of the subroutine to be called when that event is triggered by the user.   
+
We will begin by defining an event procedure for the TextBox. To access the ''textInput'' control event procedures simply double click on the TextBox control in the Form. Visual Studio will subsequently display the event procedures for the ''textInput'' control. By default Visual Studio picks the most common event, the ''TextChanged'' event, and creates a stub of the subroutine to be called when that event is triggered by the user.   
    
In our example we want every keystroke performed by the user in the TextBox to be displayed by the Label control. In this case the ''TextChanged'' event is exactly the event we want. If, however, we had wanted to write the procedure for a different event on the TextBox we could simply click on the right-hand drop down menu above the code area to view and and select from a list of events available on this type of control:
 
In our example we want every keystroke performed by the user in the TextBox to be displayed by the Label control. In this case the ''TextChanged'' event is exactly the event we want. If, however, we had wanted to write the procedure for a different event on the TextBox we could simply click on the right-hand drop down menu above the code area to view and and select from a list of events available on this type of control:
 
[[Image:Exampl.jpg]]
 
[[Image:Exampl.jpg]]
   −
With the ''TextChanged'' event still selected it is time to write the Visual Basic code that will be executed when the event is triggered. To do so, we will need to set the ''Text'' property of the ''myLabel'' control to equal the ''Text'' property of the ''inputText'' control. The properties of objects are accessed using what is called dot notation. For example the ''Text'' property of ''myLabel'' is accessed in Visual basic code as follows:
+
With the ''TextChanged'' event still selected it is time to write the Visual Basic code that will be executed when the event is triggered. To do so, we will need to set the ''Text'' property of the ''myLabel'' control to equal the ''Text'' property of the ''textInput'' control. The properties of objects are accessed using what is called dot notation. For example the ''Text'' property of ''myLabel'' is accessed in Visual basic code as follows:
    
<tt>myLabel.Text</tt>
 
<tt>myLabel.Text</tt>
 
[[Image:Example]]
 
[[Image:Example]]
   Exception encountered, of type "Error"
[0582e8a5] /index.php?diff=2376&oldid=2375&title=Understanding_Visual_Basic_Events Error from line 434 of /var/www/techotopia/includes/diff/DairikiDiff.php: Call to undefined function each()
Backtrace:
#0 /var/www/techotopia/includes/diff/DairikiDiff.php(544): DiffEngine->diag()
#1 /var/www/techotopia/includes/diff/DairikiDiff.php(344): DiffEngine->compareSeq()
#2 /var/www/techotopia/includes/diff/DairikiDiff.php(227): DiffEngine->diffLocal()
#3 /var/www/techotopia/includes/diff/DairikiDiff.php(721): DiffEngine->diff()
#4 /var/www/techotopia/includes/diff/DairikiDiff.php(859): Diff->__construct()
#5 /var/www/techotopia/includes/diff/DairikiDiff.php(980): MappedDiff->__construct()
#6 /var/www/techotopia/includes/diff/TableDiffFormatter.php(194): WordLevelDiff->__construct()
#7 /var/www/techotopia/includes/diff/DiffFormatter.php(140): TableDiffFormatter->changed()
#8 /var/www/techotopia/includes/diff/DiffFormatter.php(111): DiffFormatter->block()
#9 /var/www/techotopia/includes/diff/DifferenceEngine.php(881): DiffFormatter->format()
#10 /var/www/techotopia/includes/diff/DifferenceEngine.php(797): DifferenceEngine->generateTextDiffBody()
#11 /var/www/techotopia/includes/diff/DifferenceEngine.php(728): DifferenceEngine->generateContentDiffBody()
#12 /var/www/techotopia/includes/diff/DifferenceEngine.php(662): DifferenceEngine->getDiffBody()
#13 /var/www/techotopia/includes/diff/DifferenceEngine.php(632): DifferenceEngine->getDiff()
#14 /var/www/techotopia/includes/diff/DifferenceEngine.php(453): DifferenceEngine->showDiff()
#15 /var/www/techotopia/includes/page/Article.php(797): DifferenceEngine->showDiffPage()
#16 /var/www/techotopia/includes/page/Article.php(508): Article->showDiffPage()
#17 /var/www/techotopia/includes/actions/ViewAction.php(44): Article->view()
#18 /var/www/techotopia/includes/MediaWiki.php(490): ViewAction->show()
#19 /var/www/techotopia/includes/MediaWiki.php(287): MediaWiki->performAction()
#20 /var/www/techotopia/includes/MediaWiki.php(714): MediaWiki->performRequest()
#21 /var/www/techotopia/includes/MediaWiki.php(508): MediaWiki->main()
#22 /var/www/techotopia/index.php(41): MediaWiki->run()
#23 {main}