Saleh Bagheri Saleh Bagheri - 5 months ago 50
C# Question

How to show variable character codes in a button c#

I've saved icon font character codes in database:

f130, f150, ...

How can I show this codes as Icon?

Button.Text = "\u" + IconEntity.Value; // Error: invalid escape character!

if i use string formating, the button's text did not show icon correctly!

Button.Text = @"\u" + IconEntity.Value; //Button Text = \uf130

Answer Source

When you write strings with an escape charecter, c# treats the following text as a single char. For example, writing the string "\u0041" Will result in the string printing as the single char A.

When you start writing "\u" and then break the string, it'll treat it as a sequence of charecters, rather than a single one.

What you can do, however is create a char variable from it's hex value. You can do it by simply casting an int variable or literal to char.

Try implementing something similar to this:

static void Main(string[] args)
    var str = "41";
    var i = int.Parse(str, NumberStyles.HexNumber);

    // Prints the char "A"
    Console.WriteLine((char) i);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download