Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to insert a Symbol (Pound, Euro, Copyright) into a Textbox

I can use the Alt Key with the Number Pad to type symbols, but how do I programmatically insert a Symbol (Pound, Euro, Copyright) into a Textbox?

I have a configuration screen so I need to dynamically create the \uXXXX's.

enter image description here

like image 800
Jeremy Thompson Avatar asked Sep 05 '13 04:09

Jeremy Thompson


People also ask

How do I type the euro pound sign?

Simply hold the Alt key and then type the four-digit code on the numeric keypad. To type a £ sign, you would hold the Alt key and them type 0163. To type a € sign, press Alt and type 0128 and it will appear in the text box or document.

How do I insert a euro symbol in Google Sheets?

Place the cursor where you want the Euro Symbol (if it's a blank cell, skip this step). Hold the ALT key and then press 0128 (in succession, one key after the other) Leave the ALT key.


2 Answers

In C#, the Unicode character literal \uXXXX where the X's are hex characters will let you specify Unicode characters. For example:

  • \u00A3 is the Pound sign, £.
  • \u20AC is the Euro sign, €.
  • \u00A9 is the copyright symbol, ©.

You can use these Unicode character literals just like any other character in a string.

For example, "15 \u00A3 per item" would be the string "15 £ per item".

You can put such a string in a textbox just like you would with any other string.

Note: You can also just copy (Ctrl+C) a symbol off of a website, like Wikipedia (Pound sign), and then paste (Ctrl+V) it directly into a string literal in your C# source code file. C# source code files use Unicode natively. This approach completely relieves you from ever even having to know the four hex digits for the symbol you want.

To parallel the example above, you could make the same string literal as simply "15 £ per item".

Edit: If you want to dynamically create the Unicode character from its hex string, you can use this:

public static char HexToChar(string hex)
{
    return (char)ushort.Parse(hex, System.Globalization.NumberStyles.HexNumber);
}

For example, HexToChar("20AC") will get you the Euro sign.

If you want to do the opposite operation dynamically:

public static string CharToHex(char c)
{
    return ((ushort)c).ToString("X4");
}

For example CharToHex('€') will get you "20AC".

The choice of ushort corresponds to the range of possible char values, shown here.

like image 127
Timothy Shields Avatar answered Oct 10 '22 00:10

Timothy Shields


I cant believe this was difficult to find on the internet!

For future developers,if you have the unicode character its easy to do. eg:

C#:

var selectionIndex = txt.SelectionStart;

string copyrightUnicode = "00A9";
int value = int.Parse(copyrightUnicode, System.Globalization.NumberStyles.HexNumber);
string symbol = char.ConvertFromUtf32(value).ToString();

txt.Text = txt.Text.Insert(selectionIndex, symbol);
txt.SelectionStart = selectionIndex + symbol.Length;

VB.Net

Dim selectionIndex = txt.SelectionStart

Dim copyrightUnicode As String = "00A9"
Dim value As Integer = Integer.Parse(copyrightUnicode, System.Globalization.NumberStyles.HexNumber)
Dim symbol As String = Char.ConvertFromUtf32(value).ToString()

txt.Text = txt.Text.Insert(selectionIndex, symbol)
txt.SelectionStart = selectionIndex + symbol.Length
like image 11
Jeremy Thompson Avatar answered Oct 10 '22 00:10

Jeremy Thompson