C# 如何在文本框中插入符号(英镑、欧元、版权)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18627694/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to insert a Symbol (Pound, Euro, Copyright) into a Textbox
提问by Jeremy Thompson
I can use the AltKey with the Number Pad to type symbols, but how do I programmatically insert a Symbol (Pound, Euro, Copyright) into a Textbox?
我可以使用Alt带有数字键盘的键来键入符号,但是如何以编程方式将符号(英镑、欧元、版权)插入文本框?
I have a configuration screen so I need to dynamically create the \uXXXX's.
我有一个配置屏幕,所以我需要动态创建 \uXXXX。
采纳答案by Timothy Shields
In C#, the Unicode character literal \uXXXX
where the X
's are hex characters will let you specify Unicode characters. For example:
在C#中的Unicode字符的文字\uXXXX
,其中X
的是十六进制字符将让您指定的Unicode字符。例如:
\u00A3
is the Pound sign, £.\u20AC
is the Euro sign, .\u00A9
is the copyright symbol, ?.
\u00A3
是英镑符号,£。\u20AC
是欧元符号,。\u00A9
是版权符号,?。
You can use these Unicode character literals just like any other character in a string.
您可以像使用字符串中的任何其他字符一样使用这些 Unicode 字符文字。
For example, "15 \u00A3 per item"
would be the string "15 £ per item".
例如,"15 \u00A3 per item"
将是字符串“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.
注意:您也可以从网站上复制 ( Ctrl+ C) 一个符号,例如Wikipedia (Pound sign),然后将 ( Ctrl+ V) 直接粘贴到 C# 源代码文件中的字符串中。C# 源代码文件本机使用 Unicode。这种方法使您完全不必知道所需符号的四个十六进制数字。
To parallel the example above, you could make the same string literal as simply "15 £ per item"
.
为了与上面的示例并行,您可以将相同的字符串文字作为简单的"15 £ per item"
.
Edit:If you want to dynamicallycreate the Unicode character from its hex string, you can use this:
编辑:如果你想从它的十六进制字符串动态创建 Unicode 字符,你可以使用这个:
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.
例如,HexToChar("20AC")
会给你欧元符号。
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"
.
例如CharToHex('')
会得到你"20AC"
。
The choice of ushort
corresponds to the range of possible char
values, shown here.
的选择ushort
对应于可能的范围内char
的值,示出在这里。
回答by Jeremy Thompson
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:
对于未来的开发人员,如果您拥有 unicode 字符,则很容易做到。例如:
C#:
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
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