Home > Articles > Programming > Visual Studio

  • Print
  • + Share This
From the author of Interacting with LightSwitch Controls at Runtime

Interacting with LightSwitch Controls at Runtime

Sometimes you might want to interact programmatically with controls available inside screens. LightSwitch is based on Silverlight 4, so controls inside screens are nothing but Silverlight 4 controls. You can retrieve the instance of the desired control programmatically via a method called FindControl and then convert the instance into the appropriate type. For example, the code in Listing 4 demonstrates how to retrieve the instance of a text box called CustomerName and how to intercept the pressure of a key inside the control.

Listing 4—Interacting with Silverlight controls at runtime.

'Hold a reference to the text box where page number can be changed
Private customerNameBox As Windows.Controls.TextBox

Private Sub EditableCustomersGrid_Created()
    'Return the specified control in a LightSwitch fashion
    Dim customerBox = Me.FindControl("CustomerName")

    'Subscribe to the ControlAvailable event and get
    'the real control via the e.Control property
    AddHandler customerBox.ControlAvailable, Sub(sender As Object, e As ControlAvailableEventArgs)
                     Me.customerNameBox = CType(e.Control, Windows.Controls.TextBox)

                     'When the user presses Enter, show a message
                     AddHandler Me.customerNameBox.KeyUp, Sub(senderK As Object,
                       eK As Windows.Input.KeyEventArgs)
                                        ShowMessageBox("You pressed Enter")
                                          End Sub
                    End Sub
End Sub

In this case, a LightSwitch text box is converted into a Silverlight System.Windows.Controls.TextBox. A label control in LightSwitch should be converted into a System.Windows.Controls.TextBlock. In this way, you can add some behaviors to the LightSwitch controls with some code.

  • + Share This
  • 🔖 Save To Your Account