What is the C# equivalent of ChrW(e.KeyCode)?
Asked Answered
Z

5

14

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?

Zampino answered 19/5, 2011 at 14:52 Comment(0)
P
9

Looks like the C# equivalent would be

var MyKeyChr = char.ConvertFromUtf32((int) e.KeyCode)

However, e.KeyCode does not contain a Unicode codepoint, so this conversion is meaningless.

Panache answered 19/5, 2011 at 14:59 Comment(1)
It returns different value for code 140. In Vb Chrw(140) returns Œ but in C# it returns empty. What should be used to get same value?Ageold
B
20

The quick-and-dirty equivalent of ChrW in C# is simply casting the value to char:

char MyKeyChr = (char)e.KeyCode;

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 the ChrW method under the hood, ones you probably shouldn't be counting on anyway. That code would look something like this:

char MyKeyChr = Microsoft.VisualBasic.Strings.ChrW(e.KeyCode);

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, casting Keys.NumPad1 et. al. to char would not produce the correct value.

Blades answered 19/5, 2011 at 14:58 Comment(2)
it's not the direct equivalent. The VB.Net version handles certain boundary cases that a direct cast won't (outside the range of char for example). Boundary cases to be sure but a cast is not a direct equivalentFeola
@Jared: A fair point, I always forget about some of that extra behavior. Answer fixed.Blades
P
9

Looks like the C# equivalent would be

var MyKeyChr = char.ConvertFromUtf32((int) e.KeyCode)

However, e.KeyCode does not contain a Unicode codepoint, so this conversion is meaningless.

Panache answered 19/5, 2011 at 14:59 Comment(1)
It returns different value for code 140. In Vb Chrw(140) returns Œ but in C# it returns empty. What should be used to get same value?Ageold
F
4

The most literal way to translate the code is to use the VB.Net runtime function from C#

MyKeyChr = Microsoft.VisualBasic.Strings.ChrW(e.KeyCode);

If you'd like to avoid a dependency on the VB.Net runtime though you can use this trimmed down version

MyKeyChr = Convert.ToChar((int) (e.KeyCode & 0xffff));
Feola answered 19/5, 2011 at 14:58 Comment(3)
The way you phrase that makes it sound like the VB.NET runtime is an entirely separate runtime environment from the C# runtime. Microsoft.VisualBasic is just a class in the BCL. Using it from a C# program doesn't really add an extra dependency; the compiler will fully quality all imported references anyway.Blades
@Cody talking about Microsoft.VisualBasic.dll often involves walking a fine line of terms. From VB.Net's perspective it is a runtime DLL (in the docs and users minds). The language has heavy runtime dependencies on behavior in that DLL. Parts of it are simply inseparable from the underlying language semantics. But yes it can be confusing when considering terms like "the .Net runtime". Note though that C#'s 4.0 assembly for dynamic support is also referred to as the C# runtime DLL in many docs.Feola
@Cody and yes it is just another DLL but it has a set of dependencies that many people simply don't expect from what is usually known as a language runtime. For example it depends on System.Windows.Forms and System.Management.Feola
F
1

The C# equivalent of ChrW(&H[YourCharCode]) is Strings.ChrW(0x[YourCharCode])

You can use https://converter.telerik.com/ do convert between VB & C#.

Fiorenze answered 4/2, 2021 at 10:29 Comment(0)
C
0

This worked for me to convert VB:

e.KeyChar = Microsoft.VisualBasic.ChrW(13)

To C#:

e.KeyChar == Convert.ToChar(13)

Callosity answered 15/12, 2022 at 15:5 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.