Back to Devexpress

RepositoryItemTokenEdit.ValidateToken Event

windowsforms-devexpress-dot-xtraeditors-dot-repository-dot-repositoryitemtokenedit-cf1ed8ec.md

latest4.9 KB
Original Source

RepositoryItemTokenEdit.ValidateToken Event

Allows you to manually validate text entered by the user.

Namespace : DevExpress.XtraEditors.Repository

Assembly : DevExpress.XtraEditors.v25.2.dll

NuGet Package : DevExpress.Win.Navigation

Declaration

csharp
[DXCategory("Events")]
public event TokenEditValidateTokenEventHandler ValidateToken
vb
<DXCategory("Events")>
Public Event ValidateToken As TokenEditValidateTokenEventHandler

Event Data

The ValidateToken event's data class is DevExpress.XtraEditors.TokenEditValidateTokenEventArgs.

Remarks

Important

The ValidateToken event does not raise if the Token Editor operates in data bound mode. In this instance, handle the ProcessNewValue event to validate user input.

The Token Editor raises the ValidateToken event when the editor control loses focus. The Token Editor splits the input text into blocks based on separator positions. Each block is then validated in the ValidateToken event handler.

Use the e.IsValid event parameter to indicate whether the processed block is valid. If the block passes validation (with e.IsValid set to true ), it becomes a token (a clickable UI element that can display custom icons).

The following code snippet validates user input to accept only email addresses:

csharp
using System.Text.RegularExpressions;

Regex MailRegex =
    new Regex(@"^[-a-z0-9!#$%&'*+/=?^_`{|}~]+(?:\.[-a-z0-9!#$%&'*+/=?^_`{|}~]+)*" +
    "@(?:[a-z0-9]([-a-z0-9]{0,61}[a-z0-9])?\\.)*" +
    "(?:aero|arpa|asia|biz|cat|com|coop|edu|gov|" +
    "info|int|jobs|mil|mobi|museum|name|net|org|pro|tel|travel|[a-z][a-z])$",
    RegexOptions.Compiled);

private void ValidateToken(object sender, TokenEditValidateTokenEventArgs e) {
    e.IsValid = MailRegex.IsMatch(e.Description);
}
vb
Imports System.Text.RegularExpressions

Dim MailRegex As New Regex("^[-a-z0-9!#$%&'*+/=?^_`{|}~]+(?:\.[-a-z0-9!#$%&'*+/=?^_`{|}~]+)*" &
"@(?:[a-z0-9]([-a-z0-9]{0,61}[a-z0-9])?\.)*" &
"(?:aero|arpa|asia|biz|cat|com|coop|edu|gov|" &
"info|int|jobs|mil|mobi|museum|name|net|org|pro|tel|travel|[a-z][a-z])$", RegexOptions.Compiled)

Private Sub ValidateToken(ByVal sender As Object, ByVal e As TokenEditValidateTokenEventArgs)
    e.IsValid = MailRegex.IsMatch(e.Description)
End Sub

Use e.Value and e.Description event parameters to read and modify the token’s value and description as needed.

Read the following help topic for additional information: Token Edit Control.

Note

In Manual mode (EditMode), the Token Editor raises the ValidateToken event only when the user types the text in the edit box. In TokenList mode, the ValidateToken event does not raise.

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

winforms-tokenedit-checked-tokens/CS/TokenEdit_Glyph_InEndOfToken/Form1.cs#L22

csharp
{
    tokenEdit1.Properties.ValidateToken += tokenEdit1_ValidateToken;
    tokenEdit1.EditValue = "[email protected],[email protected],[email protected]";

See Also

Token Edit Control

ProcessNewValue

RepositoryItemTokenEdit Class

RepositoryItemTokenEdit Members

DevExpress.XtraEditors.Repository Namespace