我想把我的文本框限制为波斯字母表,我该怎么做?在C#中



我想要一个文本框只接受波斯字母,不接受C#中的任何符号。有人能帮我写代码吗?

这可能就是您想要的:

// Only allows "Persian characters" and "Space".
private void TextBox_KeyPress(object sender, KeyPressEventArgs e)
{
    if (!Regex.IsMatch(e.KeyChar.ToString(), @"p{IsArabic}")
        && !string.IsNullOrWhiteSpace(e.KeyChar.ToString()))
        e.Handled = true;
}
// Only allows "Persian characters", "Space" and "Numbers".
private void TextBox_KeyPress(object sender, KeyPressEventArgs e)
{
    if (!Regex.IsMatch(e.KeyChar.ToString(), @"p{IsArabic}")
        && !string.IsNullOrWhiteSpace(e.KeyChar.ToString())
        && !char.IsDigit(e.KeyChar))
        e.Handled = true;
}

Unicode标准中的特定字符集占据特定范围或连续代码点块。例如,从u0000u007F可以找到基本拉丁字符集,而从u0600u06FF可以找到阿拉伯字符集。正则表达式构造

p{ name }

匹配属于Unicode常规类别或命名块的任何字符。

您可以在此处阅读有关Unicode块的更多信息。

您必须为文本框添加事件。您想要的是KeyPress事件。

请参阅DotNet Perls中的本教程。

C#TextBox教程:TextChanged和KeyDown

private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{
    e.Handled = !e.KeyChar <= 'BiggestPersianChar' && !e.KeyChar >= 'SmallesPersianChar';
}

目前我不知道最大的波斯焦和最小的波斯焦。但我希望你知道。

编辑:

我猜这些是最大和最小的炭。我所说的大和小是指它的Unicode。

e.Handled = !e.KeyChar <= 'ی' && !e.KeyChar >= 'ا';

相关内容

最新更新