For my custom Modal Windows, I like to use the Modal Windows Helper Class by @YannDuran. When you create it, you pass it the table or query you want to add to, the name of your custom Modal Window, and, optionally, a title to place at the top of your Modal Window. The class pretty much takes care of the rest including proper handleing of the X
button.
Your code would then be something like this:
'Declare a Modal Window Helper for use in this screen
Private AddClientHelper As ModalWindowHelper
Private Sub ScreenName_InitializeDataWorkspace(saveChangesTo As System.Collections.Generic.List(Of Microsoft.LightSwitch.IDataService))
'Create Helpers
Me.AddClientHelper = New ModalWindowHelper(Me.qClientTable, "mwAddClient", "Add Client")
End Sub
Private Sub ScreenName_Created()
'Initialize Helpers
Me.AddClientHelper.Initialise()
End Sub
Private Sub qClientTableAddAndEditNew_CanExecute(ByRef result As Boolean)
'Check to see if user is allowed to add an Entity
result = Me.AddClientHelper.CanAdd()
End Sub
Private Sub qClientTableAddAndEditNew_Execute()
'Add a new Entity to the Collection
Me.AddClientHelper.AddEntity()
End Sub
Private Sub qClientTableEditSelected_CanExecute(ByRef result As Boolean)
'Check to see if user is allowed to view an Entity
result = Me.AddClientHelper.CanView()
End Sub
Private Sub qClientTableEditSelected_Execute()
'Open selected Entity for viewing/editing
Me.AddClientHelper.ViewEntity()
End Sub
'Save button on custom Modal Window
Private Sub btnSaveClient_Execute()
'Check for validation errors
If (Me.Details.ValidationResults.HasErrors = False) Then
'Close the modal window
Me.AddClientHelper.DialogOk()
'Save the new Client to the database
Me.Save()
Else
'If validation errors exist,
Dim res As String = ""
'Add each one to a string,
For Each msg In Me.Details.ValidationResults
res = res & msg.Property.DisplayName & ": " & msg.Message & vbCrLf
Next msg
'And display them in a message box
Me.ShowMessageBox(res, "Validation error", MessageBoxOption.Ok)
End If
End Sub
'Cancel button on custom Modal Window
Private Sub btnCancelClient_Execute()
'Cancel the entry, discarding the changes
Me.AddClientHelper.DialogCancel()
End Sub