开发者

How to block user input based on a list of chars?

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.开发者_StackOverflow

How can I validate it?


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);
};


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜