Skip to content

Commit abbfbd2

Browse files
authored
Merge pull request #703 from telerik/new-kb-maskededitbox-prevent-losing-focus-validation-failure-3a09183394204d78965bdde373fb8335
Added new kb article maskededitbox-prevent-losing-focus-validation-failure
2 parents 4622718 + a267b85 commit abbfbd2

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
---
2+
title: Preventing RadMaskedEditBox from Losing Focus on Validation Failure
3+
description: Learn how to keep the focus on a RadMaskedEditBox for WinForms when the Email mask validation fails.
4+
type: how-to
5+
page_title: How to Keep Focus on RadMaskedEditBox with Email MaskType on Validation Failure
6+
slug: maskededitbox-prevent-losing-focus-validation-failure
7+
tags: editors, maskededitbox, winforms, validation, focus
8+
res_type: kb
9+
ticketid: 1677692
10+
---
11+
12+
## Environment
13+
14+
|Product Version|Product|Author|
15+
|----|----|----|
16+
|2025.1.211|RadMaskedEditBox for WinForms|[Dinko Krastev](https://www.telerik.com/blogs/author/dinko-krastev)|
17+
18+
## Description
19+
20+
In scenarios where a RadMaskedEditBox is used with an Email MaskType, it's crucial to ensure the input is valid before allowing the user to move focus away from the control. This article demonstrates how to prevent focus loss from a RadMaskedEditBox when email validation fails.
21+
22+
## Solution
23+
24+
To achieve this, handle the `Validating` event of the RadMaskedEditBox. Within the event handler, invoke the `Validate()` method of the `EMailMaskTextBoxProvider`, which is accessible via the `Provider` property of the `MaskedEditBoxElement`. If the validation fails, set the `Cancel` property of the `System.ComponentModel.CancelEventArgs` parameter to `true` to prevent focus change.
25+
26+
````C#
27+
// Subscribe to the Validating event
28+
this.radMaskedEditBox1.Validating += RadMaskedEditBox1_Validating;
29+
30+
// Validating event handler
31+
private void RadMaskedEditBox1_Validating(object sender, System.ComponentModel.CancelEventArgs e)
32+
{
33+
// Perform the validation
34+
bool isValid = ((EMailMaskTextBoxProvider)this.radMaskedEditBox1.MaskedEditBoxElement.Provider).Validate(this.radMaskedEditBox1.MaskedEditBoxElement.Text);
35+
// If validation fails, cancel the event to prevent focus loss
36+
if (!isValid)
37+
{
38+
e.Cancel = true;
39+
}
40+
}
41+
````
42+
43+
44+
## See Also
45+
46+
- [RadMaskedEditBox Overview](https://docs.telerik.com/devtools/winforms/controls/editors/maskededitbox/maskededitbox)

0 commit comments

Comments
 (0)