这段代码在VB.Net中工作正常,我将它转换为C#。
Dim alpha As String = "./;'[]<>?:""{}\|~!@#$%^&*()_+-=abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ "
If InStr(alpha, e.KeyChar) Then e.Handled = True
答案 0 :(得分:4)
在C#中,这将是:
// Note the escaped string here: \\ instead of \, and \" for the quote
string alpha = "./;'[]<>?:\"{}\\|~!@#$%^&*()_+-=abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ";
if(alpha.Contains(e.KeyChar))
e.Handled = true;
答案 1 :(得分:0)
请注意在字符串开头使用&符号(@):
string alpha = @"./;'[]<>?:""{}\|~!@#$%^&*()_+-=abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ";
if (alpha.IndexOfAny(new[] { e.KeyChar }) > -1)
e.Handled = true;
或者更加时髦我们可以消除if
声明:
e.Handled = alpha.IndexOfAny(new[] { e.KeyChar }) > -1;
答案 2 :(得分:-1)
string alpha = "./;'[]<>?:\"{}\\|~!@#$%^&*()_+-=abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ";
if(alpha.Contains(e.KeyChar))
e.Handled = true;
除此之外,还有一个优秀的在线工具可以将C#转换为VB.NET,反之亦然 http://www.developerfusion.com/tools/convert/vb-to-csharp/