Requesting user input in an event handler

 


Let us implement a more complex example where user input is requested in an event handler. Suppose that you need a user confirmation before adding materials to a goods receipt tabular section, and a single confirmation is required for each document.

Just like the previous example, this one does not fit any commercial solution. Still, both examples perfectly illustrate the general approach to avoiding modal windows, which is described earlier in this section.

The new procedure will include single picking with multiple selection available. 

  1. In the GoodsReceipt document form, open the Pick command property palette and then, in the Action field, select the Pick procedure.
  2. Update the Pick command handler as shown in listing 26.8.

    Listing 26.8. Pick button click handler

    &AtClient
    Procedure Pick (Command)
     
        FormParameters = New Structure ("MultipleChoice", True);
        OpenForm("Catalog.MaterialsAndServices.ChoiceForm", FormParameters, Items.Materials);
     
    EndProcedure
  3. Add a blocking confirmation dialog box to the ChoiceProcessing handler of the Materials table (listing 26.9).

    Listing 26.9. ChoiceProcessing event handler of the Materials table with a confirmation dialog box

    &AtClient
    Procedure MaterialChoiceProcessing(Item, SelectedValue, StandardProcessing)
     
        If AnswerBeforeAdd <> True Then
            StandardProcessing = False;
            Notification = New NotifyDescription("AddCompletion", ThisObject, SelectedValue);
            ShowQueryBox(Notification, "Do you want to add materials to the tabular section?",
                QuestionDialogMode.YesNo);
        Else
            For Each SelectedItem In SelectedValue Do
                NewRow = Object.Materials.Add();
                NewRow.Material = SelectedItem;
            EndDo;
        EndIf;
     
    EndProcedure
    The AnswerBeforeAdd variable is used to determine whether a user answered the question.

    If a user has not answered the question, the procedure cancels the standard processing and displays the blocking confirmation dialog box. If a user answered the question earlier, the list of materials is added to the tabular section without any confirmations.

    If the AnswerBeforeAdd variable value is not true, the procedure creates a NotifyDescription object. The first parameter of the object constructor is the name of the notification handler procedure, which is executed once the user input is received. The second parameter is the name of the module where the procedure is located (in this example it is the form module). And the third optional parameter contains an array of selected material values (SelectedValue).

    Then a nonmodal method ShowQueryBox() displays the confirmation dialog box. The method accepts the NotifyDescription object as a parameter. This object points to the exported module procedure (AddCompletion) that is executed once a user makes a selection in the blocking dialog box.

    The MaterialsList parameter of the AddCompletion procedure contains the array of selected material values. The Result parameter contains the user input. If the user answer is Yes, the procedure sets AnswerBeforeAdd to True and then adds the selected materials to the document tabular section.
  4. Add the procedure shown in listing 26.10 to the form module.

    Listing 26.10. AddCompletion() notification handler procedure

    &AtClient
    Procedure AddCompletion(Result, MaterialsList) Export
    
        If Result = DialogReturnCode.Yes Then
            AnswerBeforeAdd = True;
     
            For Each selectedItem In MaterialsList Do
                NewRow = Object.Materials.Add();
                NewRow.Material = SelectedItem;
            EndDo;
     
        EndIf;
     
    EndProcedure
    Note that once a user confirms that they want to add materials to the document, the confirmation is never requested again because the value of the AnswerBeforeAdd variable is set to True.

    The last thing to do is declare that variable.
  5. Add the script from listing 26.11 to the beginning of the form module.

    Listing 26.11. Variable declaration in a form module

    &AtClient
    Var AnswerBeforeAdd;

Comments
0
Add comment