Welcome to Office Zealot Sign in | Join | Help

Avalon Adventures Part 4

Part 4 – An Event to Remember

 

Code for this demo can be found in the downloads section of www.dashpoint.com

 

Sorry for the delay. Here’s the next chapter….

 

In this chapter we are going to discuss adding events to our controls. The control we will explore is the button we added on the main form. The job of this button is to open up the customer entry screen.

 

Adding an event to this control is pretty simple. You simply add a “Click” attribute to your control and specify the subroutine to call when you click on the button.

 

Your button control will now look like this

 

   Grid.Column="0" 

   Click="CustomerClick">

   Open Customer Screen

 

This code will call a subroutine named CustomerClick. This routine needs to have the proper signature for the Click event. The following listing shows the correct signature for the click event:

 

Private Sub CustomerClick(ByVal sender As Object, _

  ByVal e As RoutedEventArgs)

        Me.ShowCustomerScreen()

End Sub

 

As you can see this is very similar to what you do in VB.NET today. One item that is broken is the Handles statement in VB.NET. Normally you wouldn’t specify the click event in the control itself but would specify a Handles statement on the Subroutine(s) that will respond to this event.

 

Why does Avalon and XAML behave in this manner? Basically…the VB.NET compiler/code-generator does not emit the correct VB code when compiling.  Below is a sample of the code that gets generated by the VB compiler

 

Partial Public Class CustomerEntry

  Inherits _

    System.Windows.Window

  Implements _

   System.Windows.Serialization.IPageConnector

       

 Protected Friend CustomerInfo As _

     System.Windows.Controls.Grid

       

 Protected Friend txtCompanyName As _

    System.Windows.Controls.TextBox

 

These lines need to be declared using the WithEvents keyword which would give you the ability to use Handles statements.

 

So what is a VB developer to do if they want more than one handler per control ? Easy use the AddHandler syntax.

 

Lets say we  want to add a second handler to the “Click” event of the button…. There are two parts to adding a new handler. The first one is to add a subroutine with the correct event signature. We will use the following routine to demonstrate our 2nd event handler.

 

Private Sub CustomerClick2(ByVal sender As Object,_

                        ByVal e As RoutedEventArgs)

   MessageBox.Show("Handler 2")

End Sub

 

Now you need to wire up the event with a command called AddHandler. Add handler has two parameters. The first parameter is a reference to the control and event you wish to handle. The second is the AddressOf the subroutine you wish to call. Add the following code to your OnLoaded subroutine.

 

AddHandler Me.cmdOpenCustomer.Click, _

  AddressOf CustomerClick2

 

Now when you click the button the customer screen will open followed by the MessageBox created in the 2nd event handler.

 

That’s it for this episode… My next post will cover Converters. Converters are classes that you can use to format the values that are shown in your controls. For instance the data fields on the OrderEntry screen. They all have Date and Time values in them. How do you remove the time… That’s where Converters come in……

 

 

 

 

 

 

 

Published Friday, December 17, 2004 1:06 PM by rod

Comments

No Comments

Anonymous comments are disabled