The easiest way to create business applications for the Desktop and the Cloud
One of the more common scenarios that we’ve encountered in business applications are data creation screens that involve related data. For example, an end user may be creating a new order record that requires the selection of a customer. In LightSwitch, this involves using a modal picker control. If this customer does not yet exist, the end user may navigate to a different screen to create a customer.
Unfortunately, once the new customer has been added, the modal picker on the new order screen does not automatically refresh. However, we can write some code in our application to get the behavior we want. This involves the following:
Create a table called ‘Customer’ with the string property called ‘Name’. Create another table called ‘Order’ with a string property called ‘Name’. Create a 1-to-Many relationship between ‘Customer’ and ‘Order’ (each ‘Customer’ has many ‘Orders’).
Define a ‘New Data Screen’ for ‘Order’. The name of this screen should be ‘CreateNewOrder’. On the screen, select ‘Add Data Item…’ and add a query property for ‘Customers’. Name this query ‘CustomerChoices’.
Select the modal picker for ‘Customer’ and in the property sheet, change the ‘Choices’ property to ‘CustomerChoices’. This will set the modal picker to use the ‘CustomerChoices’ query to determine the list of possible customer choices. We can then refresh this query from code when a customer is added.
First, define a ‘New Data Screen’ for ‘Customer’ called ‘CreateNewCustomer’. We now need to navigate to the code file for our application object to add the CustomerAdded event. On your ‘CreateNewCustomer’ select the Write Code dropdown and choose ‘CanRunCreateNewCustomer’. This will navigate to the application code file.
In the application code file define an event called ‘CustomerAdded’ and a method used to raise the event. LightSwitch runs in each screen’s logic in a different thread. The overall application also runs in its own thread. We will need to ensure that the event we define is raised on the correct thread. Since it is an application-wide event, we should ensure that it is raised on the application’s thread. This is covered in the code below.
Public Class Application
Public Event CustomerAdded()
Public Sub RaiseCustomerAdded()
'Dispatcher refers to the application's thread dispatcher
'Check if we are already in the application's thread.
'If not, raise the event on the application's thread.
If Me.Details.Dispatcher.CheckAccess() Then
public partial class Application
public delegate void RecordAdded();
public event RecordAdded CustomerAdded;
public void RaiseCustomerAdded()
//Dispatcher refers to the application's thread dispatcher
//Check if we are already in the application's thread.
//If not, raise the event on the application's thread.
Navigate to the ‘CreateNewCustomer’ screen. We will need to add code to the Saved method on our screen that triggers the ‘CustomerAdded’ event. Select the write code dropdown and select CreateNewCustomer_Saved.
Modify the existing code to raise the event. This is shown below.
Public Class CreateNewCustomer
Private Sub CreateNewCustomer_BeforeDataInitialize()
' Write your code here.
Me.CustomerProperty = New Customer()
Private Sub CreateNewCustomer_Saved()
public partial class CreateNewCustomer
partial void CreateNewCustomer_BeforeDataInitialize()
// Write your code here.
this.CustomerProperty = new Customer();
partial void CreateNewCustomer_Saved()
Navigate to the ‘CreateNewOrder’ screen. We will need to add code that will listen for the ‘CustomerAdded’ event and refresh the ‘CustomerChoices’ query when it is raised. Click on the Write Code button and modify the code as follows.
Public Class CreateNewOrder
Private Sub CreateNewOrder_BeforeDataInitialize()
Me.OrderProperty = New Order()
Private Sub CreateNewOrder_Saved()
'Need to remove the event handler (prevent memory leaks)
RemoveHandler Application.CustomerAdded, AddressOf CustomerAdded
Private Sub OrderProperty_Changed()
'Add an event handler for CustomerAdded event
AddHandler Application.CustomerAdded, AddressOf CustomerAdded
Private Sub CustomerAdded()
'CustomerAdded event is raised on the application's thread
'Run the refresh code in the screen's thread
public partial class CreateNewOrder
partial void CreateNewOrder_BeforeDataInitialize()
this.OrderProperty = new Order();
partial void CreateNewOrder_Saved()
//Need to remove the event handler (prevent memory leaks)
Application.CustomerAdded -= new Application.RecordAdded(CustomerAdded);
partial void OrderProperty_Changed()
//Add an event handler for the CustomerAdded event
Application.CustomerAdded += new Application.RecordAdded(CustomerAdded);
private void CustomerAdded()
//CustomerAdded event is raised on the application's thread
//Run the refresh code in the screen's thread
We can now test our application. Run the application and open the ‘CreateNewOrder’ screen. Enter a name for the order and open the Customer picker. There are no customers added to the system. Now open the ‘CreateNewCustomer’ screen. Enter a new customer and save the screen. Navigate back to the ‘CreateNewOrder’ screen. Open the Customer picker again. The added customer should now be displayed.
The functionality we’ve described can be extended to other scenarios (such as workflow style applications).
Congratulations on such a good job, great and simple. Can not imagine what I've struggled to do the same and in the forum I see too many users have looked for this solution.
Thank you very much.
super et merci pour ton aide depuis plusieurs mois
Thank you Thank you, this has been a great help.
How can I do this kind of thing in Light Switch HTML client ?