In VB.NET 2008, I used the following statement:
MyKeyChr = ChrW(e.KeyCode)
Now I want to convert the above statement into C#.
Any Ideas?
In VB.NET 2008, I used the following statement:
MyKeyChr = ChrW(e.KeyCode)
Now I want to convert the above statement into C#.
Any Ideas?
Looks like the C# equivalent would be
However,
e.KeyCode
does not contain a Unicode codepoint, so this conversion is meaningless.The quick-and-dirty equivalent of
ChrW
in C# is simply casting the value tochar
:The longer and more expressive version is to use one of the conversion classes instead, like
System.Text.ASCIIEncoding
.Or you could even use the actual VB.NET function in C# by importing the
Microsoft.VisualBasic
namespace. This is really only necessary if you're relying on some of the special checks performed by theChrW
method under the hood, ones you probably shouldn't be counting on anyway. That code would look something like this:However, that's not guaranteed to produce exactly what you want in this case (and neither was the original code). Not all the values in the
Keys
enumeration are ASCII values, so not all of them can be directly converted to a character. In particular, castingKeys.NumPad1
et. al. tochar
would not produce the correct value.The most literal way to translate the code is to use the VB.Net runtime function from C#
If you'd like to avoid a dependency on the VB.Net runtime though you can use this trimmed down version