【问题标题】:When user types invalid characters in textbox1 and in textbox2 an "invalid character message" is appeared and the textbox1's text is made red当用户在 textbox1 和 textbox2 中键入无效字符时,会出现“无效字符消息”并且 textbox1 的文本变为红色
【发布时间】:2020-10-01 07:04:51
【问题描述】:

我想做一个花哨的无效字符检测,就像我们在一些在线网站或移动应用程序中看到的那样。我使用 WPF (.NET Framework) 和 C# 代码。

下面是我的textbox1用户输入)和textbox2无效字符检测器)的 XAML 代码。

请注意,我使用的是 Material Design 主题。

<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 &amp; 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 &amp; 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";
    }
}

输出不正确(没有应用任何正则表达式):

如何使KeyEvent 处理程序捕获if 语句并对textbox1 的颜色和textbox2 中出现的消息进行适当的更改?

如果关于此问题还有任何其他重复问题,请在 cmets 中通知我。到目前为止,我发现了以下问题:

【问题讨论】:

标签: c# wpf textbox


【解决方案1】:

我不知道正则表达式,但我想你想检查它是否 (!) 不匹配,不是吗?另外,尝试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";
    }
}

【讨论】:

  • mm8 您的回答按要求工作。关于字符串的正确状态,我想要一种稍微不同的方法。因此,例如在我的示例中,如果我写 Nikos@ 不正确并且文本将变为红色,则您的答案。但是,如果下一个确切的时刻我写了正确的 Nikos,文本仍然是红色的,并且不会变回黑色。你的答案是绝对正确的,但你知道如果我删除无效字符“@”并且文本说这是有效名称,我怎么能恢复到黑色字体状态?
  • 我根据我的评论进行了编辑。我想通了:)非常感谢您的回答!
猜你喜欢
  • 1970-01-01
  • 2018-02-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多