当用户在 textbox1 和 textbox2 中键入无效字符时,会出现 "invalid character message" 并且 textbox1 的文本变为红色
When user types invalid characters in textbox1 and in textbox2 an "invalid character message" is appeared and the textbox1's text is made red
我想做一个花哨的无效字符检测,就像我们在一些在线网站或移动应用程序中看到的那样。我使用 WPF (.NET Framework) 和 C# 代码。
下面是我的textbox1
(用户输入)和textbox2
(无效字符检测器的XAML代码).
Note that I use the Material Design themes.
<StackPanel VerticalAlignment="Center" Margin="10,20,10,30">
<TextBox Name="CreatedSQLDatabase"
BorderBrush="Black"
materialDesign:HintAssist.Hint="Add New Database Name"
Style="{StaticResource MaterialDesignFloatingHintTextBox}"
Margin="0,0,0,0"
FontFamily="Champagne & Limousines"
FontSize="12"
MaxLength="25"
KeyDown="OnKeyDownHandler"/>
</StackPanel>
<TextBox Name="InvalidCharacterDetection"
materialDesign:HintAssist.Hint="Invalid character"
Style="{StaticResource MaterialDesignFloatingHintTextBox}"
Margin="10,100,10,40"
FontFamily="Champagne & Limousines"
FontSize="12"
MaxLength="25"
IsReadOnly="True"/>
下面是无效字符事件处理程序检测器的 C# 代码:
private void OnKeyDownHandler(object sender, KeyEventArgs e)
{
var regex = new Regex(@"[^a-zA-Z0-9-()/\s\p{IsGreekandCoptic}]");
if (regex.IsMatch(e.Key.ToString()))
{
InvalidCharacterDetection.Text = "You Entered an invalid character";
CreatedSQLDatabase.Foreground = Brushes.Red;
}
else if (String.IsNullOrEmpty(CreatedSQLDatabase.Text))
{
InvalidCharacterDetection.Text = "Database name cannot be empty";
}
else if (CreatedSQLDatabase.Text.Length > 25)
{
InvalidCharacterDetection.Text = "Database name cannot exceed 25 characters";
}
}
输出不正确(应用了 none 个正则表达式):
如何让 KeyEvent
处理程序捕获 if
语句并适当更改 textbox1
的颜色和 [=] 中出现的消息14=]?
如果还有关于此问题的任何其他重复问题,请在评论中通知我。到目前为止,我发现了以下问题:
- SO question 1
- SO question 2
我不知道正则表达式,但我想你想检查它是否 (!
) 匹配,是吗?另外,尝试 TextChanged
而不是 KeyDown
并验证 CreatedSQLDatabase.Text
:
的当前值
private void OnTextChanged(object sender, TextChangedEventArgs e)
{
var regex = new Regex(@"...");
if (!regex.IsMatch(CreatedSQLDatabase.Text))
{
InvalidCharacterDetection.Text = "You Entered an invalid character";
CreatedSQLDatabase.Foreground = Brushes.Red;
}
else if (string.IsNullOrEmpty(CreatedSQLDatabase.Text))
{
InvalidCharacterDetection.Text = "Database name cannot be empty";
}
else if (CreatedSQLDatabase.Text.Length > 25)
{
InvalidCharacterDetection.Text = "Database name cannot exceed 25 characters";
}
else
{
CreatedSQLDatabase.Foreground = Brushes.Black;
InvalidCharacterDetection.Text = "The database name is valid";
}
}
我想做一个花哨的无效字符检测,就像我们在一些在线网站或移动应用程序中看到的那样。我使用 WPF (.NET Framework) 和 C# 代码。
下面是我的textbox1
(用户输入)和textbox2
(无效字符检测器的XAML代码).
Note that I use the Material Design themes.
<StackPanel VerticalAlignment="Center" Margin="10,20,10,30">
<TextBox Name="CreatedSQLDatabase"
BorderBrush="Black"
materialDesign:HintAssist.Hint="Add New Database Name"
Style="{StaticResource MaterialDesignFloatingHintTextBox}"
Margin="0,0,0,0"
FontFamily="Champagne & Limousines"
FontSize="12"
MaxLength="25"
KeyDown="OnKeyDownHandler"/>
</StackPanel>
<TextBox Name="InvalidCharacterDetection"
materialDesign:HintAssist.Hint="Invalid character"
Style="{StaticResource MaterialDesignFloatingHintTextBox}"
Margin="10,100,10,40"
FontFamily="Champagne & Limousines"
FontSize="12"
MaxLength="25"
IsReadOnly="True"/>
下面是无效字符事件处理程序检测器的 C# 代码:
private void OnKeyDownHandler(object sender, KeyEventArgs e)
{
var regex = new Regex(@"[^a-zA-Z0-9-()/\s\p{IsGreekandCoptic}]");
if (regex.IsMatch(e.Key.ToString()))
{
InvalidCharacterDetection.Text = "You Entered an invalid character";
CreatedSQLDatabase.Foreground = Brushes.Red;
}
else if (String.IsNullOrEmpty(CreatedSQLDatabase.Text))
{
InvalidCharacterDetection.Text = "Database name cannot be empty";
}
else if (CreatedSQLDatabase.Text.Length > 25)
{
InvalidCharacterDetection.Text = "Database name cannot exceed 25 characters";
}
}
输出不正确(应用了 none 个正则表达式):
如何让 KeyEvent
处理程序捕获 if
语句并适当更改 textbox1
的颜色和 [=] 中出现的消息14=]?
如果还有关于此问题的任何其他重复问题,请在评论中通知我。到目前为止,我发现了以下问题:
- SO question 1
- SO question 2
我不知道正则表达式,但我想你想检查它是否 (!
) 匹配,是吗?另外,尝试 TextChanged
而不是 KeyDown
并验证 CreatedSQLDatabase.Text
:
private void OnTextChanged(object sender, TextChangedEventArgs e)
{
var regex = new Regex(@"...");
if (!regex.IsMatch(CreatedSQLDatabase.Text))
{
InvalidCharacterDetection.Text = "You Entered an invalid character";
CreatedSQLDatabase.Foreground = Brushes.Red;
}
else if (string.IsNullOrEmpty(CreatedSQLDatabase.Text))
{
InvalidCharacterDetection.Text = "Database name cannot be empty";
}
else if (CreatedSQLDatabase.Text.Length > 25)
{
InvalidCharacterDetection.Text = "Database name cannot exceed 25 characters";
}
else
{
CreatedSQLDatabase.Foreground = Brushes.Black;
InvalidCharacterDetection.Text = "The database name is valid";
}
}