Back to Devexpress

SpreadsheetControl.CellEditorOpened Event

wpf-devexpress-dot-xpf-dot-spreadsheet-dot-spreadsheetcontrol-d87d147d.md

latest10.1 KB
Original Source

SpreadsheetControl.CellEditorOpened Event

Occurs after a cell editor was activated by a user.

Namespace : DevExpress.Xpf.Spreadsheet

Assembly : DevExpress.Xpf.Spreadsheet.v25.2.dll

NuGet Package : DevExpress.Wpf.Spreadsheet

Declaration

csharp
public event CellEditorOpenedEventHandler CellEditorOpened
vb
Public Event CellEditorOpened As CellEditorOpenedEventHandler

Event Data

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

PropertyDescription
CustomEditorTypeReturns the type of the custom cell editor.
EditorReturns the active cell editor.
IsCustomIndicates 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:

  • Access the active cell editor (Editor) and determine its type (IsCustom, CustomEditorType).

  • Handle the editor’s keyboard events to execute custom actions or implement custom shortcuts.

  • Spell check text in the editor. Refer to the following example for implementation details:

  • Handle the editor’s TextChanged event to track text changes and validate user input, as shown in the example below.

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 pink. 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.

xaml
<dxsps:SpreadsheetControl 
    CommandBarStyle="Ribbon" 
    ShowStatusBar="True" 
    ShowFormulaBar="True" 
    Name="spreadsheetControl1"
    DocumentSource="pack://application:,,,/WpfSpreadsheet;component/Documents/Employees.xlsx"
    CellEditorOpened="OnCellEditorOpened">
</dxsps:SpreadsheetControl>
csharp
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace WpfSpreadsheet
{
    public partial class MainWindow : DevExpress.Xpf.Core.ThemedWindow
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        Brush editorBackground;
        // Specify the regular expression to check a phone number.
        Regex phoneNumber = new Regex(@"^\(?([0-9]{3})\)?[-.]?([0-9]{3})[-.]?([0-9]{4})$");

        void OnCellEditorOpened(object sender, DevExpress.Xpf.Spreadsheet.CellEditorOpenedEventArgs e)
        {
            // If a user activates an editor for a cell in the "Phone Number" column,
            // the editor's TextChanged event fires. This example assumes
            // that the target cell contains the default editor
            // (a TextBox control).
            if (spreadsheetControl1.ActiveCell.ColumnIndex == 5 &&
                spreadsheetControl1.ActiveCell.RowIndex > 3)
            { 
                // Access the active cell editor.
                TextBox textEditor = e.Editor as TextBox;
                if (textEditor == null)
                    return;
                this.editorBackground = textEditor.Background;
                textEditor.TextChanged += OnTextChanged;
                textEditor.IsVisibleChanged += OnIsVisibleChanged;
            }
        }

        void OnTextChanged(object sender, TextChangedEventArgs e)
        {
            TextBox textEditor = sender as TextBox;
            // 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.Background = editorBackground;
            }
            else
                // Otherwise, change the editor's background color to pink
                // to indicate that the user input is invalid.
                textEditor.Background = Brushes.LightPink;
        }
        void OnIsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            TextBox textEditor = sender as TextBox;
            if (!textEditor.IsVisible)
            {
                textEditor.TextChanged -= OnTextChanged;
                textEditor.IsVisibleChanged -= OnIsVisibleChanged;
            }
        }
    }
}
vb
Imports System.Text.RegularExpressions
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Media

Public Class MainWindow
    Inherits DevExpress.Xpf.Core.ThemedWindow

    Public Sub New()
        InitializeComponent()
    End Sub

    Private editorBackground As Brush
    ' Specify the regular expression to check a phone number.
    Private phoneNumber As New Regex("^\(?([0-9]{3})\)?[-.]?([0-9]{3})[-.]?([0-9]{4})$")

    Private Sub OnCellEditorOpened(sender As Object, e As DevExpress.Xpf.Spreadsheet.CellEditorOpenedEventArgs)
        ' If a user activates an editor for a cell in the "Phone Number" column,
        ' the editor's TextChanged event fires. This example assumes
        ' that the target cell contains the default editor
        ' (a TextBox control).
        If spreadsheetControl1.ActiveCell.ColumnIndex = 5 AndAlso
            spreadsheetControl1.ActiveCell.RowIndex > 3 Then
            ' Access the active cell editor.
            Dim textEditor As TextBox = TryCast(e.Editor, TextBox)
            If textEditor Is Nothing Then
                Return
            End If
            Me.editorBackground = textEditor.Background
            AddHandler textEditor.TextChanged, AddressOf OnTextChanged
            AddHandler textEditor.IsVisibleChanged, AddressOf OnIsVisibleChanged
        End If
    End Sub

    Private Sub OnTextChanged(sender As Object, e As TextChangedEventArgs)
        Dim textEditor As TextBox = TryCast(sender, TextBox)
        ' If the entered number matches the specified regular expression,
        ' convert this number to the standard phone number format.
        If phoneNumber.IsMatch(textEditor.Text) Then
            textEditor.Text = phoneNumber.Replace(textEditor.Text, "($1) $2-$3")
            textEditor.Background = editorBackground
        Else
            ' Otherwise, the editor's background color to pink
            ' to indicate that the user input is invalid.
            textEditor.Background = Brushes.LightPink
        End If
    End Sub

    Private Sub OnIsVisibleChanged(sender As Object, e As DependencyPropertyChangedEventArgs)
        Dim textEditor As TextBox = TryCast(sender, TextBox)
        If Not textEditor.IsVisible Then
            RemoveHandler textEditor.TextChanged, AddressOf OnTextChanged
            RemoveHandler textEditor.IsVisibleChanged, AddressOf OnIsVisibleChanged
        End If
    End Sub

End Class

SpreadsheetControl.CellBeginEditOccurs before a cell editor is opened.SpreadsheetControl.CellEndEditOccurs before a cell editor is closed.

The following code snippet (auto-collected from DevExpress Examples) contains a reference to the CellEditorOpened 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.

wpf-spreadsheet-spell-check-cell-text/CS/SpreadsheetSpellChecker.cs#L22

csharp
void SubscribeToEvents() {
    Spreadsheet.CellEditorOpened += OnSpreadsheet_CellEditorOpened;
    SpellChecker.CheckCompleteFormShowing += OnChecker_CheckCompleteFormShowing;

wpf-spreadsheet-spell-check-cell-text/VB/SpreadsheetSpellChecker.vb#L31

vb
Private Sub SubscribeToEvents()
    AddHandler Me.Spreadsheet.CellEditorOpened, AddressOf OnSpreadsheet_CellEditorOpened
    AddHandler SpellChecker.CheckCompleteFormShowing, AddressOf OnChecker_CheckCompleteFormShowing

See Also

SpreadsheetControl Class

SpreadsheetControl Members

DevExpress.Xpf.Spreadsheet Namespace