RIA Development Center
Features Tips Events
Jon Galloway discusses the challenges and solutions to handling keyboard input in Silverlight including:
  • Silverlight not firing the KeyDown event for cursor (arrow) keys
  • The difference between Key and PlatformKey
  • The missing Key Enumeration
  • How to create a keyboard handler event
  • Read more
    See more tips
    Get regular email alerts when we publish new features!
    DevX RIA Development Update

    More Newsletters
    Get Started with Silverlight Using Visual Studio 2008 and Expression Blend 2 (cont'd)
    More Resources
  • MSDN: Microsoft Visual Studio 2008
        Downloads
  • Free Trial: Microsoft Expression Blend 2
  • Writing the Logic Using Visual Basic
    With the UI built, it's time to write the code to make it do something useful—for instance, display the current time in the button.

    Double-click the Page.xaml.vb file in Solution Explorer to load it in the code editor.

    Figure 14. Handlers: Creating the event handler for the Completed event.

    In the Page_Loaded() subroutine, add the following bolded lines:

    
    Partial Public Class Page
        Inherits Canvas
    
        Public Sub Page_Loaded(ByVal o As Object, ByVal e As EventArgs)
            ' Required to initialize variables
            InitializeComponent()
    
            Me.Timeline1.Duration = New Duration(New TimeSpan(0, 0, 1))
            Me.Timeline1.Begin()
    
        End Sub
    
    In the preceding code, the Timeline1 triggers an event (the Completed event) each second (set through the Duration object). The Timeline object is somewhat similar to the Timer control that Windows developers are familiar with. The Begin() method starts the countdown and one second later the Completed event is fired.

    The next step would be to service the Completed event, which you can easily do by first selecting the Timeline1 object from the top of the code editor, and then selecting its corresponding Completed event (see Figure 14).

    Figure 15. Completed: Testing the application in both IE and FireFox.

    Code the Completed event handler as follows:

    
    Private Sub Timeline1_Completed( _
       ByVal sender As Object, _
       ByVal e As System.EventArgs) _
       Handles Timeline1.Completed
    
            Dim textBlk As TextBlock = Me.btnTime.Children(1)
            textBlk.Text = Now.ToString
            Me.Timeline1.Begin()
    
        End Sub
    End Class
    
    Basically, you set the current time by retrieving the TextBlock control embedded within the canvas (btnTime) and setting its Text property. The canvas has two children:
    • Children(0): Rectangle control
    • Children(1): TextBlock control
    After displaying the time, you call the Begin() method to start the countdown again.

    That's it! Press F5 in Visual Studio 2008 and you will see your Silverlight application displayed in Internet Explorer. If you load the sample application in FireFox, it will look and work the same (see Figure 15). The button will update the time every second.

    Page 5 of 7
    Previous Page: Building the User Interface Using XAML Next Page: Transformation
    Page 1: IntroductionPage 5: Writing the Logic Using Visual Basic
    Page 2: Obtaining the ToolsPage 6: Transformation
    Page 3: Getting StartedPage 7: Deploying Silverlight Applications
    Page 4: Building the User Interface Using XAML 
    Want to know who achieved gaming stardom? Keep checking RIGHT HERE! In the upcoming weeks, we will be showcasing some of the hottest next-gen games using Silverlight 2. You will be able to play them, too! Winners will be announced on June 30, 2008 so be sure to tune in. Happy Gaming!