LightSwitch – Enter static label text on screen

Purpose of Article

It’s very easy in LightSwitch to display labels associated with data records in your tables. Adding some static text to a label on a screen isn’t so obvious. The following article describes how we can achieve this by using a local property.

Screens

  1. In Solution Explorer, right click on the ‘Screens’ folder and select the option to ‘Add Screen’. Choose the ‘New Data Screen’ template, and select ‘(None)’ on the ‘Screen Data’ dropdown.

    image
    Fig 1 – Creating a new screen

  2. Click on the ‘Add Data Item’ button. Select the ‘Local Property’ radio button and select ‘String’ as the type. Name this property ‘LabelText’.

    image
    Fig 2 – Select ‘Local Property’ and set the type to ‘String’

  3. By default, a ‘Two Row’ screen is created. On the top row, select the ‘Choose Content’ dropdown and choose ‘New Group’ from the list of available items. By default, a  ‘Vertical Stack’ is created. Click on the ‘Add’ dropdown and select ‘LabelText’.

    image
    Fig 3 – The designer should look like this

  4. By default, LabelText is created as a TextBox. Use the dropdown to change this control to a label.

    image
    Fig 4 – Changing LabelText to Label

  5. On the properties of LabelText, set the ‘Label Position’ to ‘None’. We choose not to use the ‘Display Name’ property to display our text becuase by default, LS appends the ‘:’ symbol to the end of the the text.

    image
    Fig 5 – Don’t display the label

  6. Click on the ‘Write Code’ button and select the ‘LabelScreen_Loaded’ event.

    image
    Fig 5 – Use the Loaded event to set the label text

  7. Write the following code to set the label text
    Private Sub LabelScreen_Loaded()
        LabelText = "Unauthorised use of this system constitutes an offence under the Computer Misuse Act 1990"
    End Sub

Running the Application

Now run the project and open the form. The label text appears on your form as shown in the screenshot below.

image

Fig 6 – Final screen

Technorati Tags: ,
Advertisements

About dotnettim

Tim Leung is a Microsoft .Net / SQL Server developer based in England.
This entry was posted in LightSwitch, Uncategorised. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s