How to insert a Symbol (Pound, Euro, Copyright) into a Textbox
Solution 1
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.
Solution 2
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
![Jeremy Thompson](https://i.stack.imgur.com/b0VBC.jpg?s=256&g=1)
Jeremy Thompson
Hello fellow SO'r, I am a developer with a long professional history of work with web and Microsoft technologies. I am passionate about C#, VB.Net, Winforms, Web (jQuery, ASP.Net, IIS), SQL and am into a lot of tools for DevOps, Debugging, Performance, Threading, IDE and etc. My background in coding started with VB and C++, then classic asp and onto .Net. At various times I have worked for Datacom, Massive Interactive, Macquarie Bank, GraysOnline.com, MLC, SKM/Jacobs Engineering, the Reserve Bank of Australia, National Australia Bank and more. The highlight of my career was working for Microsoft Australia in Professional Developer PSS. Solving developer support cases for Microsoft Gold and Premiere customers has helped me to answer on here. I like answering the bounty questions and helping people out who are really stuck, "as if your job depends on it". I currently work at the NAB Cloud Guild as a trainer, best practice engineer and escalation engineer across NAB for cloud matters. Dip. Business, MCP, MCAD.Net, MCSD.Net, MCT (06/07), AWS CP, AWS CSA, AWS SOA, AZ900, AZ204, AZ303, AZ304
Updated on September 22, 2020Comments
-
Jeremy Thompson almost 4 years
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.
-
Jeremy Thompson almost 11 yearsLet me try this. Nah, I need to dynamically create it \uXXXX. When I try this:
symbolSyntax = "\u" + symbolSyntax
it needs me to escape the \u -
Timothy Shields almost 11 years@JeremyThompson Do you really want to create the character from the symbol hex dynamically like that? If so I can update the answer to include that as well.
-
NPSF3000 almost 11 years@JeremyThompson - Dynamically creating symbols from hex wasn't asked in the question, if that's what you want be more clear next time.
-
NPSF3000 almost 11 yearsI don't have time to test, but try: (Char)Convert.ToInt32("20AC",16)
-
Jeremy Thompson almost 11 yearsThanks for your help guys, I'm sure this will help others in the future