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

前端 未结 2 495
感动是毒
感动是毒 2020-12-09 04:42

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

2条回答
  •  暖寄归人
    2020-12-09 05:03

    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
    

提交回复
热议问题