locked
Changes to Lightswitch 2012 Modal Window? RRS feed

  • Question

  • I'm looking for a way that I can prevent the "x"/close button and the enter or escape key from closing a modal window.  I have placed a cancel button on my modal window that performs a few operations before it closes the window and I need to ensure that those operations always run when the modal window is closed.

    I have read Yann Duran's article at http://social.msdn.microsoft.com/Fo...8 and I like the points he makes about cleaning up his helper class and making a few improvements over the alternative helper class.  He doesn't give any examples or ilustrations on his article about how to use the class.

    I have also read the Sheel Shah article at http://blogs.msdn.com/b/lightswitch...nbsp;about using that modal window helper class, but in the examples of implementing the class, there are references to methods that are not available in Lightswitch 2012 (or at least I can't find where they should go).

    Does anyone know if there is an article somewhere about how to remove the "x" button and how to prevent the enter or escape keys from closing a modal window in a Lightswitch 2012 application?  Thanks for any insight!
    Monday, June 17, 2013 6:43 PM

All replies

  • Here's what I use successfully:

    There are four bits of code. 

    1&2: fix the "X" modal window problem

    3&4: fix the "enter" modal window problem

    1. Yann Duran's helper modal window (based on Sheel's MW) (I added a ViewEntity method, but that is not required) which includes a Boolean parameter to initialize the window without the "X".

    2. The screen code to instantiate/initialize/open the modal window

    3. The screen code to create an event handler for the control that you want to use in a modal window (note: each control in the modal window that you want to allow a return and not close the modal window will need this event handler)

    4. The screen code to capture the "enter" key and not close the modal window (depending upon the control, you'll have to copy and change "textbox" to "AutocompleteBox" or other type of control as another method to make the "enter" work for that type of control). 

    1. Here's the modal window code (goes in your application.cs file):

        public class ModalDialog  // Yann Duran's modal window
        {
            private IVisualCollection _collection;
            private string _dialogName;
            private string _entityName;
            private IScreenObject _screen;
            private IContentItemProxy _window;
            private IEntityObject _entity;
    
            public ModalDialog(IVisualCollection visualCollection, string dialogName, string entityName = "")
            {
                _collection = visualCollection;
                _dialogName = dialogName;
                _entityName = ((entityName != "") ? entityName : _collection.Details.GetModel().ElementType.Name);
                _screen = _collection.Screen;
            }
    
            public void Initialise(bool hasCloseButton = true)
            {
                _window = _screen.FindControl(_dialogName);
                _window.ControlAvailable += (object s, ControlAvailableEventArgs e) =>
                {
                    var window = (ChildWindow)e.Control;
                    window.HasCloseButton = hasCloseButton;
                    window.Closed += (object s1, EventArgs e1) =>
                    {
                        DialogClosed(s1);
                    };
                };
            }
    
            public bool CanAdd()
            {
                return (_collection.CanAddNew == true);
            }
    
            public bool CanView()
            {
                return (_collection.SelectedItem != null);
            }
    
            public void AddEntity()
            {
                // IEntityObject result = null;
    
                // _window.DisplayName = string.Format("Add {0}", _entityName);  (original)
                _window.DisplayName = string.Format("HEADERNAME", _entityName);
                _collection.AddNew();
    
                OpenModalWindow();
            }
    
            public void ViewEntity()
            {
                // _window.DisplayName = string.Format("View {0}", _entityName);(original)
                _window.DisplayName = string.Format("HEADERNAME", _entityName);
                OpenModalWindow();
            }
    
    
            public void EditEntity()
            {
                // _window.DisplayName = string.Format("Edit {0}", _entityName);  (original)
                _window.DisplayName = string.Format("HEADERNAME", _entityName);
                _collection.EditSelected();
                OpenModalWindow();
            }
    
            private void OpenModalWindow()
            {
                _entity = _collection.SelectedItem as IEntityObject;
                _screen.OpenModalWindow(_dialogName);
            }
    
            public void DialogOk()
            {
                if (_entity != null)
                {
                    _screen.CloseModalWindow(_dialogName);
                }
            }
    
            public void DialogCancel()
            {
                if (_entity != null)
                {
                    _screen.CloseModalWindow(_dialogName);
                    DiscardChanges();
                }
            }
    
            public void DialogClosed(object sender)
            {
                var window = (ChildWindow)sender;
    
                if (window.DialogResult.HasValue == false)
                {
                    DiscardChanges();
                }
            }
    
            private void DiscardChanges()
            {
                if (_entity != null)
                {
                    _entity.Details.DiscardChanges();
                }
            }
        }

    2. Here's the screen code:

                EditModaldialog = new ModalDialog(this.Review_MeasurementCurrentBusinessGoals, "EditModalWindowMW");
                EditModaldialog.Initialise(false);
                EditModaldialog.ViewEntity();
    

    3. Here's the "prevent enter button from closing the modal window" code (goes in screen_created method):

                    this.FindControl("mycontrol").ControlAvailable += new EventHandler<ControlAvailableEventArgs>(TextBox_ControlAvailable);
    

    4. Here's the event handler methods (go in screen code file):

            private void TextBox_ControlAvailable(object sender, ControlAvailableEventArgs e)
            {
                TextBox t = (TextBox)e.Control;
                t.KeyUp += new System.Windows.Input.KeyEventHandler(t_KeyUp);
            }
            private void t_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
            {
                if (e.Key == System.Windows.Input.Key.Enter)
                {
                    e.Handled = true;
                }
            }
    

    Hope this helps?

    Scott

    Wednesday, June 19, 2013 4:16 PM
  • Hi Scott,

    Thanks for the reply.  I am still having issues getting it to work, however.  I have not made it to steps 3&4 in your post, I am having problems getting steps 1&2 to work correctly.  I have converted all your code to VB.

    I am using Visual Studio 2012, and my lightswitch app doesn't have an application.vb file, so I created a file as part of the client called ModalDialog.vb.  Here is that code.  I made a few changes to your code to merge Yann Duran's code with yours, mostly added all his import statements and went back to his Display Names.  I was receiving errors with the initialize method so had to copy Yann Duran's and tweak it to match yours with the implementation of the hasCloseButton.  I also added a CanEdit method.

    Imports System.ComponentModel
    Imports System.Windows.Controls
    Imports Microsoft.LightSwitch
    Imports Microsoft.LightSwitch.Client
    Imports Microsoft.LightSwitch.Presentation
    Imports Microsoft.LightSwitch.Threading
    Imports Microsoft.LightSwitch.Presentation.Extensions
    
    Namespace LightSwitchApplication
    
        Public Class ModalDialog
            Private _collection As IVisualCollection
            Private _dialogName As String
            Private _entityName As String
            Private _screen As IScreenObject
            Private _window As IContentItemProxy
            Private _entity As IEntityObject
    
            Public Sub New(visualCollection As IVisualCollection, dialogName As String, Optional entityName As String = "")
                _collection = visualCollection
                _dialogName = dialogName
                _entityName = (If((entityName <> ""), entityName, _collection.Details.GetModel().ElementType.Name))
                _screen = _collection.Screen
            End Sub
    
            'Public Sub Initialize(Optional hasCloseButton As Boolean = True)
            '    _window = _screen.FindControl(_dialogName)
            '    _window.ControlAvailable += Function(s As Object, e As ControlAvailableEventArgs)
            '                                    Dim window = DirectCast(e.Control, ChildWindow)
            '                                    window.HasCloseButton = hasCloseButton
            '                                    window.Closed += Function(s1 As Object, e1 As EventArgs)
            '                                                         DialogClosed(s1)
    
            '                                                     End Function
    
            '                                End Function
            'End Sub
    
            Public Sub Initialize(Optional hasCloseButton As Boolean = True)
                _window = _screen.FindControl(_dialogName)
    
                AddHandler _window.ControlAvailable, _
                Sub(s As Object, e As ControlAvailableEventArgs)
                    Dim window = DirectCast(e.Control, ChildWindow)
                    window.HasCloseButton = hasCloseButton
                    AddHandler window.Closed, _
                     Sub(s1 As Object, e1 As EventArgs)
                         DialogClosed(s1)
                     End Sub
                End Sub
            End Sub
    
    
            Public Function CanAdd() As Boolean
                Return (_collection.CanAddNew = True)
            End Function
    
            Public Function CanView() As Boolean
                Return (_collection.SelectedItem IsNot Nothing)
            End Function
    
            Public Function CanEdit() As Boolean
                Return _collection.CanEdit AndAlso _collection.SelectedItem IsNot Nothing
            End Function
    
            Public Sub AddEntity()
                ' Dim result As IEntityObject = Nothing
    
                _window.DisplayName = String.Format("Add {0}", _entityName)
                _collection.AddNew()
    
                OpenModalWindow()
            End Sub
    
            Public Sub ViewEntity()
                _window.DisplayName = String.Format("View {0}", _entityName)
                OpenModalWindow()
            End Sub
    
    
            Public Sub EditEntity()
                _window.DisplayName = String.Format("Edit {0}", _entityName)
                _collection.EditSelected()
                OpenModalWindow()
            End Sub
    
            Private Sub OpenModalWindow()
                _entity = TryCast(_collection.SelectedItem, IEntityObject)
                _screen.OpenModalWindow(_dialogName)
            End Sub
    
            Public Sub DialogOk()
                If _entity IsNot Nothing Then
                    _screen.CloseModalWindow(_dialogName)
                End If
            End Sub
    
            Public Sub DialogCancel()
                If _entity IsNot Nothing Then
                    _screen.CloseModalWindow(_dialogName)
                    DiscardChanges()
                End If
            End Sub
    
            Public Sub DialogClosed(sender As Object)
                Dim window = DirectCast(sender, ChildWindow)
    
                If window.DialogResult.HasValue = False Then
                    DiscardChanges()
                End If
            End Sub
    
            Private Sub DiscardChanges()
                If _entity IsNot Nothing Then
                    _entity.Details.DiscardChanges()
                End If
            End Sub
        End Class
    
    
    
    End Namespace
    

    Here is my screen code:

    Namespace LightSwitchApplication
    
        Public Class MaintenanceAgreementDetail
            Private MaintenanceAgreementSiteAssetsDialog As ModalDialog
    
    
    
    
            Private Sub MaintenanceAgreementSiteAssetsAddAndEditNew_CanExecute(ByRef result As Boolean)
                ' Write your code here.
                result = MaintenanceAgreementSiteAssetsDialog.CanAdd
            End Sub
    
            Private Sub MaintenanceAgreementSiteAssetsAddAndEditNew_Execute()
                ' Write your code here.
                MaintenanceAgreementSiteAssetsDialog.AddEntity()
            End Sub
    
            Private Sub MaintenanceAgreementSiteAssetsEditDialogOk_Execute()
                ' Write your code here.
                MaintenanceAgreementSiteAssetsDialog.DialogOk()
            End Sub
    
            Private Sub MaintenanceAgreementSiteAssetsEditDialogCancel_Execute()
                ' Write your code here.
              MaintenanceAgreementSiteAssetsDialog.DialogCancel()
            End Sub
    
    
            Private Sub MaintenanceAgreementDetail_InitializeDataWorkspace(saveChangesTo As List(Of IDataService))
                MaintenanceAgreementSiteAssetsDialog = New ModalDialog(Me.MaintenanceAgreementSiteAssets, "MaintenanceAgreementSiteAssetsEditDialog")
            End Sub
    
            Private Sub MaintenanceAgreementDetail_Created()
                MaintenanceAgreementSiteAssetsDialog.Initialize(False)
            End Sub
    
            Private Sub MaintenanceAgreementSiteAssetsEditSelected_CanExecute(ByRef result As Boolean)
                ' Write your code here.
                result = MaintenanceAgreementSiteAssetsDialog.CanEdit
            End Sub
    
            Private Sub MaintenanceAgreementSiteAssetsEditSelected_Execute()
                ' Write your code here.
                MaintenanceAgreementSiteAssetsDialog.EditEntity()
            End Sub
        End Class
    
    End Namespace

    Now that the code is out of the way, on to the problem...

    Adding a new record works great but when I attempt to edit the controls on the modal dialog are read only and for some reason, the default modal window is opened in the background.  I am assuming this is because your code calls _collection.EditSelected() and that call is not in any of the modal dialog helper classes that I have seen.  The read only problem is the same issue I have had with several other modal helper classes.  Here is a screen shot of the modal window opened for editing:

    Edit Modal Screen Shot

    Any ideas on how to get the edit dialog to not be read only, and how to not display the default dialog in the background?

    When I click on cancel, the custom modal closes, but the default modal stays open and I have to manually close that window too.

    Thanks for all the input!  I am absolutely stumped on where to proceed from here.

    Friday, June 21, 2013 12:57 PM
  • @Kran,

        You'll notice that my code calls "viewentity()" vs. "editentity()".  I haven't really dug into the reason why, but "editentity()" did cause the default window to appear.  You are probably correct in that the editselected() method is causing the default window to appear.  The viewentity() method still allowed me to edit (if I wanted it to be view, i'd change the control to read-only).

    RE: the application file, here's where my application.cs file is accessed from? 

    The modal window is bound to the collection.selectedItem.  I've had issue with it opening a screen property (vs. opening a selecteditem in the screen collection).  I typically use the MW for editing a grid (which is probably what you're doing).  I have seen the "read only" view indicated and it usually had to do with the fact that the binding was not binding to the selecteditem.  I'm also assuming that you're control in the MW is not read-only?

    Hope this helps?

    Scott

    Friday, June 21, 2013 4:06 PM
  • Hi Scott,

    I modified the ModalDialog class to comment out the call to edit selected.  Here is what the edit method now looks like:

    Public Sub EditEntity()
        _window.DisplayName = String.Format("Edit {0}", _entityName)
        '_collection.EditSelected()
        OpenModalWindow()
    End Sub

    Now EditEntity() and ViewEntity() are exactly the same except for the DisplayName.

    I verified in my designer that the control is binding correctly and when I open the modal window to edit, the correct record is displayed in the control, it just won't let me edit since it is read only.  I also verified that the controls are not read only in the designer.

    Do you have any other ideas on what might be making my control read only when I open the edit window?

    Thanks for all the input. This will be the perfect solution for our needs if I can ever get it working correctly.

    Thanks,
    Adam

    Friday, June 21, 2013 7:20 PM
  • Adam,

             So you have a LS screen query that returns a collection of MaintenanceAgreements?  The MW control (SiteAsset) is bound to MaintenanceAgreements.selectedItem.SiteAsset (which is a choice dropdown list) and the screen control in the MW (SiteAsset) is not read-only (autocompletebox)?

    Scott

    Friday, June 21, 2013 8:59 PM
  • Hi Scott,

    It's actually a many to many relationship:

    MaintenanceAgreement -> MaintenanceAgreementSiteAssets <- SiteAsset

    The query on the screen is a collection of MaintenanceAgreementSiteAssets and each record contains the MaintenanceAgreement and the SiteAsset.  This is on the details screen for the maintenance agreement and the grid is all the associated SiteAssets.

    The modal window contains only the autocompletebox for SiteAsset and all the choices are filtered by the MaintenanceAgreementID.

    The Data Binding on the SiteAsset autocompletebox is: MaintenanceAgreementSiteAssets.SelectedItem.SiteAsset

    I have double checked that nothing is read only.  It works great for adding a new record, but not editing an existing record.  What is really interesting is that if I add a new record, and then don't save the screen but select the new record and click edit, the modal works.  I'm guessing this is because the record is still in "add" mode since it hasn't been saved yet.

    Thanks for all the help!
    Adam

    Friday, June 21, 2013 9:28 PM
  • Adam,

    What you want to do is create a screen query (SiteAssets) and call it something like "SelectedSiteAsset" with an integer parameter of SiteAssetID and bind that to your MaintenanceAgreementSiteAssets.SelectedItem.SiteAsset.Id in the properties of the parameter in designer and then use (bind/drag) the SelectedSiteAsset.SelectedItem.someproperty in your MW.

    You should be able to edit it then.  It sounds like you're currently trying to edit the MaintenanceAgreementSiteAssets.SelectedItem.SiteAsset which isn't what I think you're after?

    Scott

    Friday, June 21, 2013 9:36 PM
  • Hi Scott,

    I tried your suggestion and it worked but I actually did want to edit the MaintenanceAgreementSiteAssets.SelectedItem.SiteAsset.  I'm not sure why it isn't working but since the modal only has a single select box to select the associated SiteAsset, I was able to accomplish pretty much the same thing by allowing users to delete records and then add a new one.  The modal did work on all my other screens consisting of one-to-many relationships.  Not exactly sure why many-to-many isn't working but I did get a work around going for now.

    Thanks for all your help!

    I did make a few changes to the ModalDialog class that I would appreciate your comments on.  I added the method HandleEnterAndEscapeKey which is used in my screen code for each control in the modal immediately after calling initialize.  I attempted to iterate through all the controls on the screen so that only one method call would handle all the controls, but was unsuccessful in figuring out how to do that.

    I also added the parameter isReadOnly to the OpenModalWindow method so that the ViewSelected method opens a modal with readonly controls and the EditSelected method does not.

    I also found an example online of how to modify the AddEntity method so that I can set default values to the record before the modal is opened.

    Any other thoughts on how this could be made any better?  Thanks for all your help!

    ModalDialog.vb

    'Imports System.ComponentModel
    'Imports System.Windows.Controls
    Imports System.Windows.Input
    'Imports Microsoft.LightSwitch
    Imports Microsoft.LightSwitch.Client
    'Imports Microsoft.LightSwitch.Presentation
    'Imports Microsoft.LightSwitch.Threading
    'Imports Microsoft.LightSwitch.Presentation.Extensions
    
    Namespace LightSwitchApplication
    
        Public Class ModalDialog
            Private _collection As IVisualCollection
            Private _dialogName As String
            Private _entityName As String
            Private _screen As IScreenObject
            Private _window As IContentItemProxy
            Private _entity As IEntityObject
    
            Public Sub New(visualCollection As IVisualCollection, dialogName As String, Optional entityName As String = "")
                _collection = visualCollection
                _dialogName = dialogName
                _entityName = (If((entityName <> ""), entityName, _collection.Details.GetModel().ElementType.Name))
                _screen = _collection.Screen
            End Sub
    
            Public Sub Initialize(Optional hasCloseButton As Boolean = True)
                _window = _screen.FindControl(_dialogName)
    
                AddHandler _window.ControlAvailable, _
                Sub(s As Object, e As ControlAvailableEventArgs)
                    Dim window = DirectCast(e.Control, ChildWindow)
                    window.HasCloseButton = hasCloseButton
                    AddHandler window.Closed, _
                        Sub(s1 As Object, e1 As EventArgs)
                            DialogClosed(s1)
                        End Sub
                End Sub
    
            End Sub
    
            Public Sub HandleEnterAndEscapeKey(controlName As String)
    
                AddHandler _screen.FindControl(controlName).ControlAvailable, _
                    Sub(s As Object, e As ControlAvailableEventArgs)
                        Dim control = TryCast(e.Control, Control)
                        If control IsNot Nothing Then
                            AddHandler control.KeyUp, _
                                Sub(s1 As Object, e1 As KeyEventArgs)
                                    If e1.Key = Key.Enter Or e1.Key = Key.Escape Then
                                        e1.Handled = True
                                    End If
                                End Sub
                        End If
                    End Sub
    
            End Sub
    
            Public Function CanAddNew() As Boolean
                Return (_collection.CanAddNew = True)
            End Function
    
            Public Function CanView() As Boolean
                Return (_collection.SelectedItem IsNot Nothing)
            End Function
    
            Public Function CanEdit() As Boolean
                Return _collection.CanEdit AndAlso _collection.SelectedItem IsNot Nothing
            End Function
    
            Public Sub AddNew(Optional newEntity As IEntityObject = Nothing)
                _window.DisplayName = String.Format("Add {0}", _entityName)
                If newEntity IsNot Nothing Then
                    _collection.SelectedItem = newEntity
                Else
                    _collection.AddNew()
                End If
                OpenModalWindow()
            End Sub
    
            Public Sub ViewSelected()
                _window.DisplayName = String.Format("View {0}", _entityName)
                OpenModalWindow(True)
            End Sub
    
            Public Sub EditSelected()
                _window.DisplayName = String.Format("Edit {0}", _entityName)
                OpenModalWindow()
            End Sub
    
            Private Sub OpenModalWindow(Optional isReadOnly As Boolean = False)
                _window.IsReadOnly = isReadOnly
                _entity = TryCast(_collection.SelectedItem, IEntityObject)
                _screen.OpenModalWindow(_dialogName)
            End Sub
    
            Public Sub DialogOk()
                If _entity IsNot Nothing Then
                    _screen.CloseModalWindow(_dialogName)
                End If
            End Sub
    
            Public Sub DialogCancel()
                If _entity IsNot Nothing Then
                    _screen.CloseModalWindow(_dialogName)
                    DiscardChanges()
                End If
            End Sub
    
            Public Sub DialogClosed(sender As Object)
                Dim window = DirectCast(sender, ChildWindow)
    
                If window.DialogResult.HasValue = False Then
                    DiscardChanges()
                End If
            End Sub
    
            Private Sub DiscardChanges()
                If _entity IsNot Nothing Then
                    _entity.Details.DiscardChanges()
                End If
            End Sub
        End Class
    End Namespace

    Saturday, June 22, 2013 2:44 PM

  • *** Here is how I disabled <Enter><Esc> for Modal dialog.

    To control the closing of the dialog ChildWindow.Closing could be used.

    1.
    privatebool _canClose = false;

    2.
    publicvoid Initialise()
    {
    ...

                    window.HasCloseButton = false;
                    window.Closing += (object s1, CancelEventArgs e1) =>
                    {
                        DialogClosing(s1, e1);
                    };
    3.
            publicvoid DialogClosing(object sender, CancelEventArgs arg)
            {
                arg.Cancel = !_canClose;
            }
    4.
    public void DialogOk()
            {
    ...                _canClose = true

    5.
    public void DialogCancel()
            {
    ...
                     _canClose = true;

     Hope this helps.

     

     

    Sunday, June 23, 2013 3:53 PM
  • Adam,

    Nice additions to the methods.  I had no problem creating a MW for a many-many relationship edit.  Have you tried using the built-in LS button to "open" the MW to see if the control is RO (that would eliminate any code we're talking about)?  I just created a MW and used the built-in button to open the MW with edit ability.  Maybe that will give you some hints?

    Scott

    Monday, June 24, 2013 3:37 PM
  • Hi Scott,

    I finally got a chance to work on my code again this week.  I created a new set of tables with a many-to-many relationship and created a new set of screens that used the default generated modal windows and the controls were read only when the relationship table data was in the grid.  Adding a new record works fine and so does deleting a record, so I think I will just manage to use those two features to achieve similar functionality.

    Thanks for all the help.  My final copy of the modal window code is below.

    'Imports System.ComponentModel
    'Imports System.Windows.Controls
    Imports System.Windows.Input
    'Imports Microsoft.LightSwitch
    Imports Microsoft.LightSwitch.Client
    'Imports Microsoft.LightSwitch.Presentation
    'Imports Microsoft.LightSwitch.Threading
    'Imports Microsoft.LightSwitch.Presentation.Extensions
    
    Namespace LightSwitchApplication
    
        Public Class ModalDialog
            Private _collection As IVisualCollection
            Private _dialogName As String
            Private _entityName As String
            Private _screen As IScreenObject
            Private _window As IContentItemProxy
            Private _entity As IEntityObject
            Private _canCloseWindow As Boolean = False
    
            Public Sub New(visualCollection As IVisualCollection, dialogName As String, Optional entityName As String = "")
                _collection = visualCollection
                _dialogName = dialogName
                _entityName = (If((entityName <> ""), entityName, _collection.Details.GetModel().ElementType.Name))
                _screen = _collection.Screen
            End Sub
    
            Public Sub Initialize(Optional hasCloseButton As Boolean = True)
                _window = _screen.FindControl(_dialogName)
    
                AddHandler _window.ControlAvailable, _
                Sub(s As Object, e As ControlAvailableEventArgs)
                    Dim window = DirectCast(e.Control, ChildWindow)
                    window.HasCloseButton = hasCloseButton
    
                    AddHandler window.Closing, _
                        Sub(s1 As Object, e1 As CancelEventArgs)
                            e1.Cancel = Not _canCloseWindow
                        End Sub
    
                    AddHandler window.Closed, _
                        Sub(s2 As Object, e2 As EventArgs)
                            _canCloseWindow = False
                            DialogClosed(s2)
                        End Sub
                End Sub
    
            End Sub
    
            'This method would have been used in the screen code immediately after calling initialize
            'To be fully functional, this method would need to be called for each individual control on the modal window
            'This method has been replaced by using the _canCloseWindow variable and window.Closing method
            'It has been left in this class for illustration purposes
            Public Sub HandleEnterAndEscapeKey(controlName As String)
    
                AddHandler _screen.FindControl(controlName).ControlAvailable, _
                    Sub(s As Object, e As ControlAvailableEventArgs)
                        Dim control = TryCast(e.Control, Control)
    
                        If control IsNot Nothing Then
                            AddHandler control.KeyUp, _
                                Sub(s1 As Object, e1 As KeyEventArgs)
                                    If e1.Key = Key.Enter Or e1.Key = Key.Escape Then
                                        e1.Handled = True
                                    End If
                                End Sub
                        End If
                    End Sub
    
            End Sub
    
            Public Function CanAddNew() As Boolean
                Return (_collection.CanAddNew = True)
            End Function
    
            Public Function CanView() As Boolean
                Return (_collection.SelectedItem IsNot Nothing)
            End Function
    
            Public Function CanEdit() As Boolean
                Return _collection.CanEdit AndAlso _collection.SelectedItem IsNot Nothing
            End Function
    
            Public Sub AddNew(Optional newEntity As IEntityObject = Nothing)
                _window.DisplayName = String.Format("Add {0}", _entityName)
                If newEntity IsNot Nothing Then
                    _collection.SelectedItem = newEntity
                Else
                    _collection.AddNew()
                End If
                OpenModalWindow()
            End Sub
    
            Public Sub ViewSelected()
                _window.DisplayName = String.Format("View {0}", _entityName)
                OpenModalWindow(True)
            End Sub
    
            Public Sub EditSelected()
                _window.DisplayName = String.Format("Edit {0}", _entityName)
                OpenModalWindow()
            End Sub
    
            Private Sub OpenModalWindow(Optional isReadOnly As Boolean = False)
                _window.IsReadOnly = isReadOnly
                _entity = TryCast(_collection.SelectedItem, IEntityObject)
                _screen.OpenModalWindow(_dialogName)
            End Sub
    
            Public Sub Ok()
                If _entity IsNot Nothing Then
                    _canCloseWindow = True
                    _screen.CloseModalWindow(_dialogName)
                End If
            End Sub
    
            Public Sub Cancel()
                If _entity IsNot Nothing Then
                    _canCloseWindow = True
                    _screen.CloseModalWindow(_dialogName)
                    DiscardChanges()
                End If
            End Sub
    
            Public Sub DialogClosed(sender As Object)
                Dim window = DirectCast(sender, ChildWindow)
    
                If window.DialogResult.HasValue = False Then
                    DiscardChanges()
                End If
            End Sub
    
            Private Sub DiscardChanges()
                If _entity IsNot Nothing Then
                    _entity.Details.DiscardChanges()
                End If
            End Sub
        End Class
    End Namespace

    Wednesday, June 26, 2013 4:15 PM
  • Thanks Kran and ReachPoint.

    I like the simplicity of the "_canClose" to handle the Enter and Esc keys, and I shall be using it in my code and clean up the mess of having a "HandleEnterAndEscapeKey" for each textbox in the modal window.

    I wonder if the team did look into this after this connect request was closed... 

    Best regards,

    CT

    Sunday, February 9, 2014 3:02 AM