如何根据字符列表阻止用户输入?
我有一个字符列表:
var l = new List<char>();
l.AddRange(Path.GetInvalidFileNameChars());
l.AddRange(Path.GetInvalidPathChars());
我想检测用户何时按下其中一个被阻止的字符并将 SupressKeyPress
设置为 true
。
我有 e.KeyCode
、e.KeyData
和 e.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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以通过将
Handled
值设置为 true 来抑制KeyPress
:You can suppress the
KeyPress
by setting theHandled
value to true:似乎不起作用,至少对我来说不起作用。
与 KeyDown 事件一起使用的属性:
由于 KeyDown 发生在 KeyPress 或 KeyUp 事件之前,因此我“过滤”了想要在附加 KeyDown 事件中处理的键。
SupressKeyPress 属性 MSDN< /a>
Doesn't seem to work, at least not for me.
The property that did work with a KeyDown Event with:
Since KeyDown occurs before KeyPress or KeyUp Events, I "filtered" keys I wanted to handle in an additional KeyDown event.
SupressKeyPress Property MSDN