TX Words: Conditional Instructions

In context with the ability to create forms, TX Text Control also provides the option to apply a specific behavior to form fields when one or more events occur. The sample illustrates the concept of such conditional instructions by deploying and previewing a form where the layout and behavior depends on predefined conditions.

The source code for this example is contained in the following directories:

  • %USERPROFILE%\Documents\TX Text Control 32.0.NET for WPF\CSharp\TX Words\Conditional Instructions
  • %USERPROFILE%\Documents\TX Text Control 32.0.NET for WPF\VB.NET\TX Words\Conditional Instructions

Using the Sample

By running this sample, a form is loaded that already contains conditional instructions that are listed in the overview of the Conditional Instructions sidebar. Each entry represents a set of actions to be performed when one or more conditions are fulfilled.

Image

In order to deploy such a form, the content must be protected. This can be done by toggling both the Permissions tab's Fill in Form Fields and Read Only button before clicking Enforce Protection.

Image

To preview the conditional instructions of a form, the sample provides a Preview button inside the Form Fields tab that is only enabled in case the Enabled Form Validation button is toggled. When clicking this button, the document becomes write-protected with the previous mentioned permissions settings. When untoggling the button, the content is reset to the state that was displayed before previewing the conditional instructions behavior.

On performing the preview mode, you can check the form behavior that is also described next to each form field. To show whether the defined form field content is valid, the Highlight Invalid Values button can be toggled.

Image

To get an insight of creating conditional instructions, check out the article Creating Conditional Instructions for Form Fields.

The Code Behind

To implement the preview behavior, it is necessary to store the current content and document permission settings before activating the preview mode. The content is saved to a byte array by using the TextControl.Save method. The DocumentPermissions.AllowEditingFormFields and DocumentPermissions.ReadOnly property values are stored to temporary bool variables.

To activate the preview, both document permissions variables are set to true and the TextControl.EditMode to ReadAndSelect.

private void ActivateCIPreview() {
        // Store the current TextControl content.
        m_txTextControl.Save(out m_rbtTextControlContent, BinaryStreamType.InternalUnicodeFormat);

        // Store the current DocumentPermissions.AllowEditingFormFields and .ReadOnly property values.
        m_bAllowEditinFormFieldsTemp = m_txTextControl.DocumentPermissions.AllowEditingFormFields;
        m_bReadOnlyTemp = m_txTextControl.DocumentPermissions.ReadOnly;

        // Set those document permissions that are necessary to show a preview.
        m_txTextControl.DocumentPermissions.AllowEditingFormFields = true;
        m_txTextControl.DocumentPermissions.ReadOnly = true;

        // Hide all ribbon groups behind the TXITEM_FormValidationGroup group
        for (int i = 3; i < m_rtRibbonFormFieldsTab.Items.Count; i++) {
                (m_rtRibbonFormFieldsTab.Items[i] as RibbonGroup).Visibility = System.Windows.Visibility.Collapsed;
        }

        // Activate the preview by setting the TextControl.EditMode property value to
        // EditMode.ReadAndSelect.
        m_bIsPreviewActivated = true;
        m_txTextControl.EditMode = EditMode.ReadAndSelect;

        // Disable the TXITEM_EnableFormValidation ribbon toggle button
        m_rbtnEnableFormValidation.IsEnabled = false;
}
Private Sub ActivateCIPreview()
        ' Store the current TextControl content.
        Me.m_txTextControl.Save(m_rbtTextControlContent, BinaryStreamType.InternalUnicodeFormat)

        ' Store the current DocumentPermissions.AllowEditingFormFields and .ReadOnly property values.
        m_bAllowEditinFormFieldsTemp = Me.m_txTextControl.DocumentPermissions.AllowEditingFormFields
        m_bReadOnlyTemp = m_txTextControl.DocumentPermissions.ReadOnly

        ' Set those document permissions that are necessary to show a preview.
        Me.m_txTextControl.DocumentPermissions.AllowEditingFormFields = True
        Me.m_txTextControl.DocumentPermissions.ReadOnly = True

        ' Hide all ribbon groups behind the TXITEM_FormValidationGroup group
        For i As Integer = 3 To Me.m_rtRibbonFormFieldsTab.Items.Count - 1
                TryCast(Me.m_rtRibbonFormFieldsTab.Items(i), RibbonGroup).Visibility = Windows.Visibility.Collapsed
        Next

        ' Activate the preview by setting the TextControl.EditMode property value to
        ' EditMode.ReadAndSelect.
        m_bIsPreviewActivated = True
        Me.m_txTextControl.EditMode = EditMode.ReadAndSelect

        ' Disable the TXITEM_EnableFormValidation ribbon toggle button
        m_rbtnEnableFormValidation.IsEnabled = False
End Sub

When deactivating the preview, the saved content is reloaded by using the TextControl.Load method and the document permissions are reset to the values. An explicit resetting of the TextControl.EditMode property value to Edit is not necessary, because it is already set by loading the old content.

private void DeactivateCIPreview(bool reloadContent) {
        // Reset the preview button checked state
        m_rtbtnPreviewConditionalInstructions.IsChecked = false;
        m_bIsPreviewActivated = false;

        //  Enable the "Enable Form Validation" button.
        m_rbtnEnableFormValidation.IsEnabled = true;

        // Reset the Textcontrol content and permissions.
        if (reloadContent) {
                // Reload the Textcontrol content.
                m_txTextControl.Load(m_rbtTextControlContent, BinaryStreamType.InternalUnicodeFormat);
                m_rbtTextControlContent = null;

                // Reset the DocumentPermissions.AllowEditingFormFields and .ReadOnly property values.
                m_txTextControl.DocumentPermissions.AllowEditingFormFields = m_bAllowEditinFormFieldsTemp;
                m_txTextControl.DocumentPermissions.ReadOnly = m_bReadOnlyTemp;
        }
}
Private Sub DeactivateCIPreview(ByVal reloadContent As Boolean)
        ' Reset the preview button checked state
        m_rtbtnPreviewConditionalInstructions.IsChecked = False
        m_bIsPreviewActivated = False

        '  Enable the "Enable Form Validation" button.
        m_rbtnEnableFormValidation.IsEnabled = True

        ' Reset the Textcontrol content and permissions.
        If reloadContent Then
                ' Reload the Textcontrol content.
                Me.m_txTextControl.Load(m_rbtTextControlContent, BinaryStreamType.InternalUnicodeFormat)
                m_rbtTextControlContent = Nothing

                ' Reset the DocumentPermissions.AllowEditingFormFields property value and .ReadOnly property values.
                Me.m_txTextControl.DocumentPermissions.AllowEditingFormFields = m_bAllowEditinFormFieldsTemp
                Me.m_txTextControl.DocumentPermissions.ReadOnly = m_bReadOnlyTemp
        End If
End Sub