How to: Modify a Screen by Using Code

Visual Studio LightSwitch 2011 enables you to accomplish many screen-related design tasks by using designers and tool windows. However, there are certain tasks that you might want to accomplish by using code. This topic shows you how to accomplish a set of common screen-related design tasks by using the screen object model. For more information about where you can write code in your application, see any of the following topics:

For general guidance about writing code in Visual Studio LightSwitch 2011, see Writing Code in LightSwitch.

The following list describes some common data-related tasks that you accomplish by using the screen object model.

You can hide or show controls on a screen by using code. You can also specify whether controls are read-only or disabled.

The following example hides a company name in a data grid if the company name is Coho Winery. This example also makes the control read-only so that viewers cannot modify the company name by typing text into the control.

Private Sub FindControlInList()
    Dim index As Integer = 0

    For Each cust As Customer In Customers

        If cust.CompanyName = "Great Lakes Food Market" Then
            With FindControlInCollection("CompanyName", Customers(index))
                .IsVisible = False
                .IsReadOnly = True
            End With

        End If
        index = index + 1
End Sub

The following example hides the company name in a details view on the screen if the company name of the selected item is Coho Winery. This example also disables the Delete button so that users cannot delete a customer who works for Coho Winery.

Private Sub Customers_SelectionChanged()
    FindControl("Customers_DeleteSelected").IsEnabled = True

    If Me.Customers.SelectedItem.CompanyName = "Great Lakes Food Market" Then

        FindControl("CompanyName1").IsVisible = False
        FindControl("Customers_DeleteSelected").IsEnabled = False

    End If

End Sub

Community Additions