Skip to main content

DevExpress v24.2 Update — Your Feedback Matters

Our What's New in v24.2 webpage includes product-specific surveys. Your response to our survey questions will help us measure product satisfaction for features released in this major update and help us refine our plans for our next major release.

Take the survey Not interested

Handle Script Events at Runtime

  • 2 minutes to read

This example demonstrates how to handle script events at runtime.

Important

Report scripts are not secure and are disabled by default. We recommend that you use expression bindings instead.

To accomplish this task, it is first necessary to add the required code to the collection of all scripts, stored in the XtraReport.ScriptsSource property. Then, assign the name of the script to the control’s XRControlEvents.OnBeforePrint property, and this will result in executing this code when an appropriate event is raised by the report generation engine.

using System;
using System.Windows.Forms;
using DevExpress.XtraReports.UI;

namespace WindowsApplication1 {
    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e) {
            new ReportPrintTool(CreateReport()).ShowPreviewDialog();
        }

        private void button2_Click(object sender, EventArgs e) {
            new ReportDesignTool(CreateReport()).ShowDesignerDialog();
        }

        private XtraReport CreateReport() {
            // Create a new report.
            XtraReport1 report = new XtraReport1();

            // Add a script to a report's collection of scripts.
            report.ScriptsSource += "private void xrLabel1_BeforePrint(object sender, " +
                                    "System.ComponentModel.CancelEventArgs e) {\r\n  " +
                                    "((XRLabel)sender).BackColor = Color.Aqua;\r\n}";

            // Assign this script to a label's BeforePrint script.
            report.xrLabel1.Scripts.OnBeforePrint = "xrLabel1_BeforePrint";

            return report;
        }
    }
}
See Also