如何根据字符列表阻止用户输入?

发布于 2024-10-07 23:31:03 字数 370 浏览 0 评论 0原文

我有一个字符列表:

var l = new List<char>();
l.AddRange(Path.GetInvalidFileNameChars());
l.AddRange(Path.GetInvalidPathChars());

我想检测用户何时按下其中一个被阻止的字符并将 SupressKeyPress 设置为 true

我有 e.KeyCodee.KeyDatae.KeyValue,但它们都不对应于 ?,例如。

我怎样才能验证它?

I have a list of chars:

var l = new List<char>();
l.AddRange(Path.GetInvalidFileNameChars());
l.AddRange(Path.GetInvalidPathChars());

I want to detect when the user press one of the blocked characters and set SupressKeyPress to true.

I have e.KeyCode, e.KeyData and e.KeyValue, but none of them corresponds to ?, for example.

How can I validate it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

抱猫软卧 2024-10-14 23:31:03

您可以通过将 Handled 值设置为 true 来抑制 KeyPress

var l = new List<char>();
l.AddRange(Path.GetInvalidFileNameChars());
l.AddRange(Path.GetInvalidPathChars());

this.KeyPress += (s, e) =>
{
    e.Handled = l.Any(x => x == e.KeyChar);
};

You can suppress the KeyPress by setting the Handled value to true:

var l = new List<char>();
l.AddRange(Path.GetInvalidFileNameChars());
l.AddRange(Path.GetInvalidPathChars());

this.KeyPress += (s, e) =>
{
    e.Handled = l.Any(x => x == e.KeyChar);
};
断舍离 2024-10-14 23:31:03
e.Handled = true;

似乎不起作用,至少对我来说不起作用。

与 KeyDown 事件一起使用的属性:

e.SupressKeyPress;

由于 KeyDown 发生在 KeyPress 或 KeyUp 事件之前,因此我“过滤”了想要在附加 KeyDown 事件中处理的键。

SupressKeyPress 属性 MSDN< /a>

e.Handled = true;

Doesn't seem to work, at least not for me.

The property that did work with a KeyDown Event with:

e.SupressKeyPress;

Since KeyDown occurs before KeyPress or KeyUp Events, I "filtered" keys I wanted to handle in an additional KeyDown event.

SupressKeyPress Property MSDN

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文