xtrareports-devexpress-dot-xtrareports-dot-ui-dot-xrcontrol-440d531c.md
Occurs before an XRControl object creates its image in a report being generated.
Namespace : DevExpress.XtraReports.UI
Assembly : DevExpress.XtraReports.v25.2.dll
NuGet Package : DevExpress.Reporting.Core
public virtual event BeforePrintEventHandler BeforePrint
Public Overridable Event BeforePrint As BeforePrintEventHandler
The BeforePrint event's data class is CancelEventArgs. The following properties provide information specific to this event:
| Property | Description |
|---|---|
| Cancel | Gets or sets a value indicating whether the event should be canceled. |
The BeforePrint event occurs when the report’s CreateDocument method is called (internally form other methods or explicitly).
You can handle this event to change the control’s properties. The event argument’s Cancel property allows you to cancel control rendering.
Note
This event does not have any relation to the actual printing process. To manage print settings, handle the PrintingSystemBase.StartPrint event instead.
The BeforePrint is not raised when you export the document to any available format because the export operation does not recreate the report document.
The BeforePrint event is raised with other events in the following order:
See Report Events for more information.
This example demonstrates how to handle a label’s XRControl.BeforePrint event, how to access an XRLabel object in this event handler, and how to customize its appearance based on a specific condition.
Tip
Online Example : How to change a label’s appearance in its BeforePrint event handler
using System;
using System.ComponentModel;
using DevExpress.XtraReports.UI;
// ...
private void xrLabel_BeforePrint(object sender, CancelEventArgs e) {
// Obtain the current label.
XRLabel label = (XRLabel)sender;
// Get the total value.
double total = Convert.ToDouble(GetCurrentColumnValue("Total"));
// Customize the label's appearance.
if (total < 100) {
label.ForeColor = Color.White;
label.BackColor = Color.Red;
}
else if (total > 1000) {
label.ForeColor = Color.White;
label.BackColor = Color.Green;
}
else {
label.ForeColor = Color.Black;
label.BackColor = Color.Transparent;
}
}
Imports System
Imports System.ComponentModel
Imports DevExpress.XtraReports.UI
' ...
Private Sub xrLabel_BeforePrint(ByVal sender As Object, ByVal e As CancelEventArgs)
' Obtain the current label.
Dim label As XRLabel = DirectCast(sender, XRLabel)
' Get the total value.
Dim total As Double = Convert.ToDouble(GetCurrentColumnValue("Total"))
' Customize the label's appearance.
If total < 100 Then
label.ForeColor = Color.White
label.BackColor = Color.Red
ElseIf total > 1000 Then
label.ForeColor = Color.White
label.BackColor = Color.Green
Else
label.ForeColor = Color.Black
label.BackColor = Color.Transparent
End If
End Sub
The following code snippets (auto-collected from DevExpress Examples) contain references to the BeforePrint event.
Note
The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.
reporting-winforms-create-table-at-runtime/CS/Form1.cs#L26
XtraReport report = CreateEmptyReport();
report.BeforePrint += Report_BeforePrint;
asp-net-mvc-grid-create-report-based-on-grid-layout/CS/E4755/Controllers/HomeController.cs#L52
};
control.BeforePrint += new BeforePrintEventHandler(control_BeforePrint);
e.IsModified = true;
asp-net-web-forms-grid-create-report-based-on-grid-layout/CS/WebApplication1/Default.aspx.cs#L35
control.Shape = new ShapeStar() { StarPointCount = 5, Concavity = 30 };
control.BeforePrint += new BeforePrintEventHandler(control_BeforePrint);
e.IsModified = true;
reporting-web-forms-handle-server-side-errors/CS/DXWebApplication1/Reports/XtraReport2.cs#L44
this.xrLabel1 = new DevExpress.XtraReports.UI.XRLabel();
this.BeforePrint += XtraReport2_BeforePrint;
((System.ComponentModel.ISupportInitialize)(this)).BeginInit();
reporting-winforms-create-table-at-runtime/VB/Form1.vb#L26
Dim report As XtraReport = CreateEmptyReport()
AddHandler report.BeforePrint, AddressOf Report_BeforePrint
asp-net-mvc-grid-create-report-based-on-grid-layout/VB/E4755/Controllers/HomeController.vb#L51
control.Shape = New ShapeStar() With {.StarPointCount = 5, .Concavity = 30}
AddHandler control.BeforePrint, AddressOf control_BeforePrint
e.IsModified = True
asp-net-web-forms-grid-create-report-based-on-grid-layout/VB/WebApplication1/Default.aspx.vb#L40
}
AddHandler control.BeforePrint, AddressOf control_BeforePrint
e.IsModified = True
See Also