generalinformation-404625-security-clipboard-access-policy.md
The DevExpress.Data.Utils.ClipboardAccessPolicy allows you to control/manage clipboard-related operations when using DevExpress UI controls (within Windows Forms and WPF applications).
Use the following methods to apply a policy (call these methods at application startup):
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());
}
}
}
Imports System
Imports System.Windows.Forms
Imports DevExpress.Data.Utils
Namespace DXApplication
Friend Module Program
''' <summary>
''' The main entry point for the application.
''' </summary>
<STAThread>
Sub Main()
Application.EnableVisualStyles()
Application.SetCompatibleTextRenderingDefault(False)
ClipboardAccessPolicy.SuppressCopyOperations()
Application.Run(New Form1())
End Sub
End Module
End Namespace
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;
}
}
}
Imports System
Imports System.Windows.Forms
Imports DevExpress.Data.Utils
Namespace DXApplication
Friend Module Program
''' <summary>
''' The main entry point for the application.
''' </summary>
<STAThread>
Sub Main()
Application.EnableVisualStyles()
Application.SetCompatibleTextRenderingDefault(False)
ClipboardAccessPolicy.SuppressCopyOperations()
AddHandler ClipboardAccessPolicy.Pasting, AddressOf ClipboardAccessPolicy_Pasting
Application.Run(New Form1())
End Sub
Private Sub ClipboardAccessPolicy_Pasting(ByVal sender As Object, ByVal e As ClipboardAccessPolicy.ClipboardOperationRequestEventArgs)
If e.DataFormat = DataFormats.Text Then
e.Cancel = False
End If
End Sub
End Module
End Namespace
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.
Important
SuppressAllOperations(), SuppressCopyOperations(), SuppressPasteOperations(), SuppressClearOperations(), and SuppressContainsOperations() policies take priority over AllowOperation and AllowDataFormats.
Handle the Failed event to respond to associated failures.
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());
}
}
}
Imports System
Imports System.Windows.Forms
Imports DevExpress.Data.Utils
Imports DevExpress.Data.Internal
Namespace DXApplication
Friend Module Program
''' <summary>
''' The main entry point for the application.
''' </summary>
<STAThread>
Sub Main()
Application.EnableVisualStyles()
Application.SetCompatibleTextRenderingDefault(False)
ClipboardAccessPolicy.AllowOperation(ClipboardOperation.Copy, DataFormats.Text)
ClipboardAccessPolicy.ThrowAlways()
Application.Run(New Form1())
End Sub
End Module
End Namespace
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;
}
}
}
Imports System
Imports System.Windows.Forms
Imports DevExpress.Data.Utils
Namespace DXApplication
Friend Module Program
''' <summary>
''' The main entry point for the application.
''' </summary>
<STAThread>
Sub Main()
Application.EnableVisualStyles()
Application.SetCompatibleTextRenderingDefault(False)
ClipboardAccessPolicy.ThrowAlways()
AddHandler ClipboardAccessPolicy.Failed, AddressOf ClipboardAccessPolicy_Failed
Application.Run(New Form1())
End Sub
Private Sub ClipboardAccessPolicy_Failed(ByVal sender As Object, ByVal e As ClipboardAccessPolicy.FailedEventArgs)
If e.DataFormat = DataFormats.Text Then
e.Throw = False
End If
End Sub
End Module
End Namespace
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");
}
}
}
Imports System
Imports System.Windows.Forms
Imports DevExpress.Data.Utils
Imports DevExpress.XtraEditors
Namespace DXApplication11
Friend Module Program
''' <summary>
''' The main entry point for the application.
''' </summary>
<STAThread>
Sub Main()
Application.EnableVisualStyles()
Application.SetCompatibleTextRenderingDefault(False)
ClipboardAccessPolicy.ThrowAlways()
AddHandler ClipboardAccessPolicy.Failed, AddressOf ClipboardAccessPolicy_Failed
Application.Run(New Form1())
End Sub
Private Sub ClipboardAccessPolicy_Failed(ByVal sender As Object, ByVal e As ClipboardAccessPolicy.FailedEventArgs)
e.Throw = False
XtraMessageBox.Show(String.Format("{0} operation is not allowed.", e.Operation.ToString()), "Warning")
End Sub
End Module
End Namespace