none
How to prevent a Modal Window from Closing?

Answers

  • Is removing the red "X" from the title bar good enough?

    I modified my ModalWindow class to do this a while back, so now, by default, all of my modal windows don't have a "X" to close the window with, & it can then only be close by an added  "Cancel" (or "Close") button.

    In the Initialise method, you can override the default, to allow the "X" if required.


    Yann - LightSwitch Central - Click here for FREE Themes, Controls, Types and Commands
     
    If you find a reply helpful, please click "Vote as Helpful", if a reply answers your question, please click "Mark as Answer"
     
    By doing this you'll help people find answers faster.

    Friday, August 24, 2012 5:32 AM
    Moderator
  • Hi Garth,

    To prevent the Modal Window from closing, use a Closing event handler instead and set e.Cancel = true... i.e.:

    partial void MyScreen_InitializeDataWorkspace(List<IDataService> saveChangesTo)
    {
        this.FindControl("MyModalDialog").ControlAvailable += (s, e) =>
        {
            (e.Control as System.Windows.Controls.ChildWindow).Closing += MyModalDialog_Closing;
        };
    
    }
    
    
    void MyModalDialog_Closing(object sender, EventArgs e)
    {
        e.Cancel = true;
    }


    My Blog: dotnetlore.com Twitter: @DotNetLore

    Tuesday, May 22, 2012 2:41 AM

All replies

  • Hi Garth,

    To prevent the Modal Window from closing, use a Closing event handler instead and set e.Cancel = true... i.e.:

    partial void MyScreen_InitializeDataWorkspace(List<IDataService> saveChangesTo)
    {
        this.FindControl("MyModalDialog").ControlAvailable += (s, e) =>
        {
            (e.Control as System.Windows.Controls.ChildWindow).Closing += MyModalDialog_Closing;
        };
    
    }
    
    
    void MyModalDialog_Closing(object sender, EventArgs e)
    {
        e.Cancel = true;
    }


    My Blog: dotnetlore.com Twitter: @DotNetLore

    Tuesday, May 22, 2012 2:41 AM
  • Hiya Jewel,

    Have you been able to get this to work?

    Setting e.Cancel = true just doesn't stop the LS Modal Window from closing.  I agree that it sure should work.

    Has anyone been able to stop a LS modal window from closing?

    Thanks,

      Garth


    Garth Henderson - Vanguard Business Technology

    Friday, August 24, 2012 5:19 AM
  • Hi Garth,

    I just tried the following in Visual Studio 2012 LightSwitch and it successfully prevented the Modal Dialog from closing (though the parent screen itself could still be closed, of course):

    partial void CreateNewProduct_InitializeDataWorkspace(List<IDataService> saveChangesTo)
    {
        this.FindControl("MyModalDialog").ControlAvailable += (s, e) =>
        {
            (e.Control as System.Windows.Controls.ChildWindow).Closing += MyModalDialog_Closing;
        };
    
    }
    
    void MyModalDialog_Closing(object sender, System.ComponentModel.CancelEventArgs e)
    {
        e.Cancel = true;
    }
    


    My Blog: dotnetlore.com Twitter: @DotNetLore

    Friday, August 24, 2012 5:32 AM
  • Is removing the red "X" from the title bar good enough?

    I modified my ModalWindow class to do this a while back, so now, by default, all of my modal windows don't have a "X" to close the window with, & it can then only be close by an added  "Cancel" (or "Close") button.

    In the Initialise method, you can override the default, to allow the "X" if required.


    Yann - LightSwitch Central - Click here for FREE Themes, Controls, Types and Commands
     
    If you find a reply helpful, please click "Vote as Helpful", if a reply answers your question, please click "Mark as Answer"
     
    By doing this you'll help people find answers faster.

    Friday, August 24, 2012 5:32 AM
    Moderator
  • Thanks, Jewel for the confirmation with VS 2012.   I'm just starting to port to VS2012.

    However, I am using VS 2010 with our current apps and the code isn't working.

    The event method is called - but setting the e.Cancel = true seems to do nothing.    The window continues to close.

    I'm using the same System.ComponentModel.CancelEventArgs exception class.

    Please, has anyone gotten this code to work with VS2010?


    Garth Henderson - Vanguard Business Technology


    Friday, August 24, 2012 6:49 PM
  • Thanks, Yann.  

    Does page 294 in Chapter 8 of "Pro Visual Studio LightSwitch 2011 Development" detail your solution?


    Garth Henderson - Vanguard Business Technology

    Friday, August 24, 2012 6:55 PM
  • Hi Garth,

    I was finally able to try it under VS 2010 and I see what you mean.  Barring Microsoft fixing the bug in LightSwitch 2011 as they have under 2012, I'm guessing Yann's approach is your best bet.



    My Blog: dotnetlore.com Twitter: @DotNetLore

    Friday, August 24, 2012 7:31 PM
  • Aloha,

    I was able to create a work around using both Jewel and Yann's solutions.

    Note:  I wanted to just pop back up the modal window after it closed - but couldn't make that happen in VS 2010.

    Our requirement is that we have several Modal Windows that are used for what we call Workflow Actions.

    Each Action is a custom MW that has a specific set of buttons.

    My workaround solution with VS 2010:  We use the Modal Window Closed event to display a message letting the user know that the MW is closing because they clicked the X or hit the Return key.   The message explains that the user can simply reopen the window to contiue their work - if it wasn't complete. 

    Then I call the appropriate button execute method (from within the MWClosed method) on the screen that represents the default action.  The screen button execute method (as speicified in Tim/Yann's book) must be wrapped in a delegate method:

                    this.Details.Dispatcher.BeginInvoke(
                        delegate()
                        {
                            this.Save();
                            ARTranSave_Execute();
                        });
    This is easy enough to do as long as we can make sure each Action can be restarted until completed by the user.  Often, based on the WF, each Action remains open until it is approved by another Role.

    Of course, with VS 2012, I'll use the original solution posted by Jewel.

    Many thanks to both of you.


    Garth Henderson - Vanguard Business Technology

    Friday, August 24, 2012 11:29 PM
  • You're welcome.

    Yann - LightSwitch Central - Click here for FREE Themes, Controls, Types and Commands
     
    If you find a reply helpful, please click "Vote as Helpful", if a reply answers your question, please click "Mark as Answer"
     
    By doing this you'll help people find answers faster.

    Saturday, August 25, 2012 11:03 AM
    Moderator
  • Please could provide me this code in vb.net?
    Monday, November 19, 2012 8:27 PM
  • There are some web sites that convert between the languages.

    Here's one:

    http://www.developerfusion.com/tools/convert/csharp-to-vb/?batchId=7108a75e-da62-4a7a-b582-6421ab7e7f30


    Garth Henderson - Vanguard Business Technology

    Monday, November 19, 2012 10:22 PM