Skip to main content
All docs
V23.2

Clipboard Access Policy

  • 6 minutes to read

The DevExpress.Data.Utils.ClipboardAccessPolicy allows you to control/manage clipboard-related operations when using DevExpress UI controls (within Windows Forms and WPF applications).

Apply a Policy

Use the following methods to apply a policy (call these methods at application startup):

  • SuppressAllOperations() – Applies a policy to suppress all clipboard-related operations within DevExpress WinForms and WPF controls.
  • SuppressCopyOperations() – Applies a policy to suppress all copy-to-clipboard operations within DevExpress UI controls.
  • SuppressPasteOperations() – Applies a policy to suppress all paste-from-clipboard operations within DevExpress UI controls.
  • SuppressClearOperations() – Applies a policy to suppress clipboard clear operations within DevExpress UI controls.
  • SuppressContainsOperations() – Applies a policy to suppress clipboard-access operations performed by DevExpress WinForms and WPF controls.
  • ThrowAlways() – Applies a policy to suppress all clipboard-related operations in DevExpress UI controls. Raises an exception when a user or UI control attempts to initiate a clipboard-related operation.
  • ThrowOnErrors() – Applies a policy to throw an exception when a clipboard-related operation fails within a DevExpress UI control.

The following example allows users to copy data in ANSI text format (when displayed in a DevExpress UI control) to the clipboard. If the control displays data in a different format, the copy-to-clipboard operation is canceled:

using System;
using System.Windows.Forms;
using DevExpress.Data.Utils;

namespace DXApplication {
    internal static class Program {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main() {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            ClipboardAccessPolicy.SuppressCopyOperations();
            Application.Run(new Form1());
        }
    }
}

Manage Copy/Paste/Clear Clipboard Operations using Events

Handle the following events in the ClipboardAccessPolicy class to configure policy settings:

The following example allows a user to paste data in ANSI text format within a DevExpress UI control (from the clipboard). If the user pastes data in a different format, the paste-from-clipboard operation is canceled:

using System;
using System.Windows.Forms;
using DevExpress.Data.Utils;

namespace DXApplication {
    internal static class Program {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main() {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            ClipboardAccessPolicy.SuppressCopyOperations();
            ClipboardAccessPolicy.Pasting += ClipboardAccessPolicy_Pasting;
            Application.Run(new Form1());
        }
        private static void ClipboardAccessPolicy_Pasting(object sender, ClipboardAccessPolicy.ClipboardOperationRequestEventArgs e) {
            if (e.DataFormat == DataFormats.Text)
                e.Cancel = false;
        }
    }
}

Handle Exceptions

DevExpress UI controls catch clipboard-related errors or exceptions (error swallowing), and then continue without logging, processing, or reporting errors. Call the ThrowOnErrors() method at application startup to manually handle clipboard-related errors. The method applies a policy that allows all clipboard-related operations. The policy throws an exception when a clipboard-related operation fails in a DevExpress WinForms or WPF control.

You can also use the ThrowAlways method to apply a policy that disables all clipboard-related operations (copy, paste, clear) in DevExpress WinForms and WPF controls. The policy throws an exception when a user or UI control attempts to initiate a clipboard-related operation.

Tip

Use AllowOperation and/or AllowDataFormats methods to enable specific operations for all (or specific) data formats when a restrictive policy is enabled.

Handle the Failed event to respond to associated failures.

Example 1

The following example introduces a policy that allows a user to copy text to the clipboard and disallows other clipboard-related operations. If the user or a control attempts to execute a disallowed operation, the policy throws an exception.

using System;
using System.Windows.Forms;
using DevExpress.Data.Utils;
using DevExpress.Data.Internal;

namespace DXApplication {
    internal static class Program {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main() {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            ClipboardAccessPolicy.AllowOperation(ClipboardOperation.Copy, DataFormats.Text);
            ClipboardAccessPolicy.ThrowAlways();
            Application.Run(new Form1());
        }
    }
}

Example 2: Allow Specific Operations

using System;
using System.Windows.Forms;
using DevExpress.Data.Utils;

namespace DXApplication {
    internal static class Program {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main() {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            ClipboardAccessPolicy.ThrowAlways();
            ClipboardAccessPolicy.Failed += ClipboardAccessPolicy_Failed;
            Application.Run(new Form1());
        }
        private static void ClipboardAccessPolicy_Failed(object sender, ClipboardAccessPolicy.FailedEventArgs e) {
            if (e.DataFormat == DataFormats.Text)
                e.Throw = false;
        }
    }
}

Example 3: Handle Internal Exceptions

The following example suppresses an internal exception and displays a message box:

using System;
using System.Windows.Forms;
using DevExpress.Data.Utils;
using DevExpress.XtraEditors;

namespace DXApplication11 {
    internal static class Program {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main() {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            ClipboardAccessPolicy.ThrowAlways();
            ClipboardAccessPolicy.Failed += ClipboardAccessPolicy_Failed;
            Application.Run(new Form1());
        }
        private static void ClipboardAccessPolicy_Failed(object sender, ClipboardAccessPolicy.FailedEventArgs e) {
            e.Throw = false;
            XtraMessageBox.Show(String.Format("{0} operation is not allowed.", e.Operation.ToString()), "Warning");
        }
    }
}