locked
Help with ModalDialog RRS feed

  • Question

  • I’ve been following the article A PRACTICAL EXTENSION METHOD FOR THE MODAL WINDOW ENTER KEY “BUG” IN LIGHTSWITCH.

    I’ve created the ModalDialog class in the Application.cs file but I’m having problems how to call the methods from the screen.

    He’s the code I have so far but I get a NullReferenceException error when I click the modal Cancel button.

    public partial class zSearchMediaListDetail

        {

            public ModalDialog TransactionsDialog;

            partial void TransactionsAddAndEditNew_Execute()

            {

                ModalDialog TransactionsDialog = new ModalDialog(this.Transactions, "Transactions_SelectedItem");

                TransactionsDialog.Initialise(true);

                TransactionsDialog.AddEntity();

            }

            partial void zSearchMediaListDetail_Closing(ref bool cancel)

            {

               TransactionsDialog.DialogCancel();

            }

    Are there any examples of how this is used?

    Thanks

    Friday, September 11, 2015 1:46 PM

Answers

  • Hi, I do not know how to override button code in application.cs. Yann Duran, Improved Modal Window Helper Class (based on Sheel's Article "Creating a Custom Add Or Edit Dialog"), has an great example of a helper class that you can modify with your needs after you have developed the code on a particular screen. Note I am not using the out of the box modal windows but am creating a custom modal window. The helper class will also create custom modal windows.

    My modal window has OK and Cancel buttons. My code for the OK button, will cancel if no changes are made on an add record on enter key. I am using third party controls so the logic is a bit different, the screen opens with no selected item and I track when an item is selected on the third party grid with Boolean isSelectedItem and object selectedItem.

     partial void BeginNew_Execute()
            {
                addRecord = true;
                var v = MainAccounts.AddNew();
              
                if (isSelectedItem)
                {
                    Dispatchers.Main.BeginInvoke(() =>
                    {
                        selectedItem = this.radGrid.SelectedItem;
                        this.radGrid.SelectedItem = v;
                    });
                }
                else
                {
                    Dispatchers.Main.BeginInvoke(() =>
                    {
                        this.radGrid.SelectedItem = v;
                    });
                }
                this.OpenModalWindow("Group");
            }
    
            partial void Cancel_Execute()
            {
                if (addRecord)
                {
                    MainAccounts.SelectedItem.Delete();
                    addRecord = false;
                    Dispatchers.Main.BeginInvoke(() =>
                    {
                        if (!isSelectedItem)
                        {
                            radGrid.SelectedItems.Clear();
                        }
                        else
                        {
                            radGrid.SelectedItem = selectedItem;
                        }
                    });
                }
                else
                {
                    this.MainAccounts.SelectedItem.Details.DiscardChanges();
                    Dispatchers.Main.BeginInvoke(() =>
                    {
                        this.radGrid.SelectedItem = MainAccounts.SelectedItem;
                    });
                }
                this.CloseModalWindow("Group");
            }
    
            partial void BeginEdit_Execute()
            {
                addRecord = false;
                Dispatchers.Main.BeginInvoke(() =>
                {
                    MainAccounts.SelectedItem = radGrid.SelectedItem as MainAccount;
                });
                this.OpenModalWindow("Group");
            }
    
            partial void Ok_Execute()
            {
                if (addRecord)
                {
                    addRecord = false;
                    if (String.IsNullOrEmpty(MainAccounts.SelectedItem.Description +
                                             MainAccounts.SelectedItem.Display +
                                             MainAccounts.SelectedItem.Summary))
                    {
                        MainAccounts.SelectedItem.Delete();
                        Dispatchers.Main.BeginInvoke(() =>
                        {
                            if (!isSelectedItem)
                            {
                                radGrid.SelectedItems.Clear();
                            }
                            else
                            {
                                radGrid.SelectedItem = selectedItem;
                            }
                        });
                    }
                    else
                    {
                        Dispatchers.Main.BeginInvoke(() =>
                        {
                            if (!isSelectedItem)
                            {
                                radGrid.SelectedItems.Clear();
                            }
                            else
                            {
                                radGrid.SelectedItem = selectedItem;
                            }
                        });
                    }
                }
                else
                {
                    Dispatchers.Main.BeginInvoke(() =>
                    {
                        this.radGrid.SelectedItem = MainAccounts.SelectedItem;
                    });
                }
                this.CloseModalWindow("Group");
            }
    
            partial void BeginDelete_Execute()
            {
                MainAccounts.SelectedItem.Delete();
                //this.Save();
                Dispatchers.Main.BeginInvoke(() =>
                {
                    this.radGrid.Rebind();
                });
            }

    I hope this helps or at least stimulates you to forge your own.

    Mike

    tejana wordpress




    • Edited by dynds1 Sunday, September 13, 2015 12:21 PM
    • Proposed as answer by Angie Xu Monday, September 21, 2015 1:37 AM
    • Marked as answer by Angie Xu Thursday, September 24, 2015 6:35 AM
    Sunday, September 13, 2015 12:06 PM

All replies

  • Hi,

    If one adds a group to the screen, change it to a modal window and add content as per:

    Creating a Custom Add or Edit Dialog (Sheel Shah)

    then

    this.FindControl("Group").ControlAvailable += Group.ControlAvailable;

    Can give you a place to put:

     

    voidGroup_ControlAvailable(objectsender, ControlAvailableEventArgse1)

            {

    UIElement uiElement = e1.Control as UIElement;
                         uiElement.KeyUp += (s2, e2) =>
                              {
                                  if (e2.Key == Key.Enter || e2.Key==Key.Escape)
                                      e2.Handled = true;

    }}

    If you leave the "bug" it will act like many people expect.

    Pressing the enter key normally fires a button. You could program your button code to check if you want to save or cancel.

    Mike

    Friday, September 11, 2015 6:18 PM
  • Hi Mike, thanks for the reply.

    Its how to program the save and cancel buttons I'm having problems with. Could you please supply an example of how this is done from the modal window to the ModalDialog class in the Application.cs.

    Many thanks

     
    Sunday, September 13, 2015 3:56 AM
  • Hi, I do not know how to override button code in application.cs. Yann Duran, Improved Modal Window Helper Class (based on Sheel's Article "Creating a Custom Add Or Edit Dialog"), has an great example of a helper class that you can modify with your needs after you have developed the code on a particular screen. Note I am not using the out of the box modal windows but am creating a custom modal window. The helper class will also create custom modal windows.

    My modal window has OK and Cancel buttons. My code for the OK button, will cancel if no changes are made on an add record on enter key. I am using third party controls so the logic is a bit different, the screen opens with no selected item and I track when an item is selected on the third party grid with Boolean isSelectedItem and object selectedItem.

     partial void BeginNew_Execute()
            {
                addRecord = true;
                var v = MainAccounts.AddNew();
              
                if (isSelectedItem)
                {
                    Dispatchers.Main.BeginInvoke(() =>
                    {
                        selectedItem = this.radGrid.SelectedItem;
                        this.radGrid.SelectedItem = v;
                    });
                }
                else
                {
                    Dispatchers.Main.BeginInvoke(() =>
                    {
                        this.radGrid.SelectedItem = v;
                    });
                }
                this.OpenModalWindow("Group");
            }
    
            partial void Cancel_Execute()
            {
                if (addRecord)
                {
                    MainAccounts.SelectedItem.Delete();
                    addRecord = false;
                    Dispatchers.Main.BeginInvoke(() =>
                    {
                        if (!isSelectedItem)
                        {
                            radGrid.SelectedItems.Clear();
                        }
                        else
                        {
                            radGrid.SelectedItem = selectedItem;
                        }
                    });
                }
                else
                {
                    this.MainAccounts.SelectedItem.Details.DiscardChanges();
                    Dispatchers.Main.BeginInvoke(() =>
                    {
                        this.radGrid.SelectedItem = MainAccounts.SelectedItem;
                    });
                }
                this.CloseModalWindow("Group");
            }
    
            partial void BeginEdit_Execute()
            {
                addRecord = false;
                Dispatchers.Main.BeginInvoke(() =>
                {
                    MainAccounts.SelectedItem = radGrid.SelectedItem as MainAccount;
                });
                this.OpenModalWindow("Group");
            }
    
            partial void Ok_Execute()
            {
                if (addRecord)
                {
                    addRecord = false;
                    if (String.IsNullOrEmpty(MainAccounts.SelectedItem.Description +
                                             MainAccounts.SelectedItem.Display +
                                             MainAccounts.SelectedItem.Summary))
                    {
                        MainAccounts.SelectedItem.Delete();
                        Dispatchers.Main.BeginInvoke(() =>
                        {
                            if (!isSelectedItem)
                            {
                                radGrid.SelectedItems.Clear();
                            }
                            else
                            {
                                radGrid.SelectedItem = selectedItem;
                            }
                        });
                    }
                    else
                    {
                        Dispatchers.Main.BeginInvoke(() =>
                        {
                            if (!isSelectedItem)
                            {
                                radGrid.SelectedItems.Clear();
                            }
                            else
                            {
                                radGrid.SelectedItem = selectedItem;
                            }
                        });
                    }
                }
                else
                {
                    Dispatchers.Main.BeginInvoke(() =>
                    {
                        this.radGrid.SelectedItem = MainAccounts.SelectedItem;
                    });
                }
                this.CloseModalWindow("Group");
            }
    
            partial void BeginDelete_Execute()
            {
                MainAccounts.SelectedItem.Delete();
                //this.Save();
                Dispatchers.Main.BeginInvoke(() =>
                {
                    this.radGrid.Rebind();
                });
            }

    I hope this helps or at least stimulates you to forge your own.

    Mike

    tejana wordpress




    • Edited by dynds1 Sunday, September 13, 2015 12:21 PM
    • Proposed as answer by Angie Xu Monday, September 21, 2015 1:37 AM
    • Marked as answer by Angie Xu Thursday, September 24, 2015 6:35 AM
    Sunday, September 13, 2015 12:06 PM