Skip to main content
All docs
V23.2

SpreadsheetControl.CellEditorOpened Event

Occurs after a cell editor was activated by a user.

Namespace: DevExpress.XtraSpreadsheet

Assembly: DevExpress.XtraSpreadsheet.v23.2.dll

NuGet Package: DevExpress.Win.Spreadsheet

Declaration

public event CellEditorOpenedEventHandler CellEditorOpened

Event Data

The CellEditorOpened event's data class is CellEditorOpenedEventArgs. The following properties provide information specific to this event:

Property Description
CustomEditorType Returns the type of the custom cell editor.
Editor Returns the active cell editor.
IsCustom Indicates whether the active cell editor is custom.

Remarks

The CellEditorOpened event fires after a user activates a cell editor to edit a cell value. This event allows you to do the following:

Example. Use the CellEditorOpened Event to Validate User Input

The code sample below demonstrates how to use a regular expression to check that a user enters a US phone number in the correct format. If a user types an invalid number, the editor’s background color changes to Salmon. If a number in the correct format is entered, the editor’s background color resets to the original color and the phone number is converted to the following format: (123) 456-7890.

Handle the CellEditorOpened Event

using System;
using System.Drawing;
using System.Text.RegularExpressions;

namespace SpreadsheetApp
{
    public partial class Form1 : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        // Specify the regular expression to check a phone number.
        Regex phoneNumber = new Regex(@"^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$");
        Color editorBackColor = Color.Transparent;

        public Form1()
        {
            InitializeComponent();
            spreadsheetControl1.CellEditorOpened += spreadsheetControl1_CellEditorOpened;
            spreadsheetControl1.LoadDocument(@"Documents\Employees.xlsx");
        }

        private void spreadsheetControl1_CellEditorOpened(object sender,
            DevExpress.XtraSpreadsheet.CellEditorOpenedEventArgs e)
        {
            // If a user activates an editor for a cell in the "Phone Number" column,
            // the editor's TextChanged event fires.
            if (spreadsheetControl1.ActiveCell.ColumnIndex == 5 &&
                spreadsheetControl1.ActiveCell.RowIndex > 3)
            {
                var editor = e.Editor;
                this.editorBackColor = editor.BackColor;
                editor.TextChanged += Editor_TextChanged;
            }
        }

        private void Editor_TextChanged(object sender, EventArgs e)
        {
            // Access the active cell editor. This example assumes
            // that the target cell contains the default editor
            // (a Windows text box control).
            var textEditor = sender as System.Windows.Forms.TextBox;
            if (textEditor == null)
                return;
            // If the entered number matches the specified regular expression,
            // convert this number to the standard phone number format.
            if (phoneNumber.IsMatch(textEditor.Text))
            {
                textEditor.Text = phoneNumber.Replace(textEditor.Text, "($1) $2-$3");
                textEditor.BackColor = editorBackColor;
            }
            else
                // Otherwise, change the editor's background to the Salmon color
                // to indicate that the user input is invalid.
                textEditor.BackColor = Color.Salmon;
        }
    }
}
SpreadsheetControl.CellBeginEdit
Occurs before a cell editor is opened.
SpreadsheetControl.CellEndEdit
Occurs before a cell editor is closed and the entered value is committed.
See Also