When I type Alt+1 I get back ☺ I would like some magic way in code to get the ☺ character in a string.
Something like this:
string smiley = AltCodes.GetAltCodeCharacter(1).ToString();
// Display the Smiley
richTextBoxHappy.Text = smiley;
Then in the rich text box there should be this: ☺
You can use Unicode Table (Decimal) to find out your code, then use it like this for example:
int i = 9786;
textBoxHappy.Text = ((char)i).ToString();
Result image:
Of course you can create your custom mapping, for example some Dictionary<string, int>
(or any custom type you want), and add codes to it like { "smiley", 9786 }
, then use it altCode["smiley"]
.
Or use enum
for that:
enum AltCodes
{
Smiley = 9786,
NextSmiley = 9787
}
and use it like:
textBoxHappy.Text = ((char)AltCodes.Smiley).ToString();
You can even create your custom decoder, then write your alt code (by holding alt and typing numbers) inside wpf TextBox
, click "Decode" and get your code values to int[]
for example:
private int[] DecodeCharCode(string str) =>
DecodeCharCode(str.ToArray());
private int[] DecodeCharCode(char[] chars)
{
int[] result = new int[chars.Length];
for (int i = 0; i < chars.Length; i++)
{
result[i] = (int)chars[i];
}
return result;
}
and usage (click event or command):
int[] decodedCodes = DecodeCharCode(decodingText.Text);
There is a finite number of these. You can just create a Dictionary<int, string>
to store all these:
IReadOnlyDictionary<int, string> altCodes = new Dictionary<int, string>() {
{1, "☺"},
{2, "\U0000263B"},
// and so on...
};
And access it like so:
altCodes[1]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With