LightSwitch – Setting ComboBox values in code

Overview of Article

The following article describes how values and default values can be set for a ComboBox. The Customer and Town data from the previous post will be used. In this example, the default town for new customers will be set to the town of ‘Reading’. In keeping with good practice, the logic is applied at the entity level. The logic could also be coded at screen level but it is best practice to maintain a clean separation between presentation and logic layers.

Data

The previous post which can be found here describes the schema of our Customer and Town tables. After these have been created, we’ll begin writing our entity code.

  1. Open up the Customer table. On the toolbar, drop down the ‘Write Code’ button and select the ‘Customer_Created’ event.

    image
    Fig1 – select the ‘Customer_Created’ event
      

  2. The code window now opens. Add the following code:
     Private Sub Customer_Created()
       Me.Town = DataWorkspace.ApplicationData.Towns.Where(Function(town) town.TownName = "Reading").FirstOrDefault()
     End Sub
    
  3. Here’s how the code would look like in C#

    this.Town = DataWorkspace.ApplicationData.Towns.Where((Town) => Town.TownName == "Reading").FirstOrDefault();

The code uses a Lambda expression to find a town matching the TownName of ‘Reading’. The town for the Customer is then set to the town which has been returned.

The above logic is executed when an instance of a new Customer is created. However, business logic can also be applied in various other places at the entity level. For example, the town could be set to ‘London’ for customers with a firstname beginning with ‘T’. In order to do this, open the customer table, select the ‘FirstName’ column and click on the ‘Write Code’ button.

 

image
Fig2 – Illustrations of other field/property level events

Select the FirstName_Changed event and write the following code.

Private Sub FirstName_Changed()
    If Me.FirstName.StartsWith("T") Then
        Me.Town = DataWorkspace.ApplicationData.Towns.Where(Function(town) town.TownName = "London").FirstOrDefault()
    End If
End Sub

The C# code looks like this:

partial void FirstName_Changed()
{
    if (this.test.StartsWith("T"))
    {

this.Town = DataWorkspace.ApplicationData.Towns.Where((Town) => Town.TownName == "Reading").FirstOrDefault();
} }

Screens

We can reuse the ‘Editable Grid Screen’ on the town table from the previous post to enter in some town names. For the above examples to work, the towns ‘Reading’ and ‘London’ should be created. Here’s a brief reminder of how to create a data entry screen for the ‘Customer’ table.

  1. Create a new screen based upon the ‘New Data Screen’ template. In the ‘Screen Data’ dropdown, choose ‘Customer’.  
  2. By default, a ‘Two Row’ screen is created and ‘Town’ will appear as ‘Modal Window Picker’. Click the downward arrow next to Modal Window Picker to change the control to a ‘ComboBox’.

    image_thumb[8]
    Fig 3 – Change from ‘Modal Window Picker’ to ‘ComboBox’

Conclusion

If we now run our application and open the ‘CreateNewCustomer’ form, the town ComboBox is defaulted to ‘Reading’. Since the logic has been defined at entity level, nothing has been added at the screen level. The advantage of this is that every subsequent call to create a new customer will also default the town to ‘Reading’.

 

image

Fig 4 – Create New Customer screen

There may be scenarios when you want to set the logic at the client level. At the screen level, LightSwitch exposes a ‘Saving’ event that you can use for this purpose. An example code snippet can be found through the following thread.

http://social.msdn.microsoft.com/Forums/en-US/lightswitchgeneral/thread/b1b4fe97-5e38-4ced-ae23-4bfc55003748/#f38f9051-2aeb-4b01-99ab-2ca449945115 

 

Advertisements

About dotnettim

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

5 Responses to LightSwitch – Setting ComboBox values in code

  1. Gurvinder says:

    Hi,
    When i use the code i get target invocation exception was unhandled by user code.
    Thanks

  2. Muhammad Zaly says:

    Hi,

    Your code solved my problem of setting default value in combo box despite searching tirelessly all over the net. Great work.

    Zaly
    Malaysia

  3. deepali says:

    Thanks a lot for this snippet. We had been working on this little part since yesterday to achieve this thing. It saved my day.

  4. Gerry says:

    Thanks! I was trying to do this the hard way by putting it at the screen level.

  5. Pingback: Default-Werte, auch bei Fremdschlüssel-Feldern – WurmWeb

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