I know it's possible to add the square operator to a label using its unicode value (How can I show a superscript character in .NET GUI labels?). Is there a way to add any power to a label? My application needs to display polynomial functions, i.e. x^7 + x^6 etc.
-
Do you need the text to be editable or not ? – digEmAll Feb 23 '13 at 16:17
-
@digEmAll No, it won't need changing – mikeythemissile Feb 23 '13 at 16:22
4 Answers
You can use the (great) HtmlRenderer and build you own label control supporting html.
Here's an example :
public class HtmlPoweredLabel : Control
{
protected override void OnPaint(PaintEventArgs e)
{
string html = string.Format(System.Globalization.CultureInfo.InvariantCulture,
"<div style=\"font-family:{0}; font-size:{1}pt;\">{2}</div>",
this.Font.FontFamily.Name,
this.Font.SizeInPoints,
this.Text);
var topLeftCorner = new System.Drawing.PointF(0, 0);
var size = this.Size;
HtmlRenderer.HtmlRender.Render(e.Graphics, html, topLeftCorner, size);
base.OnPaint(e);
}
}
Usage example:
// add an HtmlPoweredLabel to you form using designer or programmatically,
// then set the text in this way:
this.htmlPoweredLabel.Text = "y = x<sup>7</sup> + x<sup>6</sup>";
Result :
Note that this code wraps your html into a div section that sets the font family and size to the one used by the control. So you can change the size and font by changing the Font
property of the label.

- 56,430
- 9
- 115
- 140
You could also use the power of the natively supported UTF strings and do something like and extension method that converts ints (or uints even) to strings like:
public static class SomeClass {
private static readonly string superscripts = @"⁰¹²³⁴⁵⁶⁷⁸⁹";
public static string ToSuperscriptNumber(this int @this) {
var sb = new StringBuilder();
Stack<byte> digits = new Stack<byte>();
do {
var digit = (byte)(@this % 10);
digits.Push(digit);
@this /= 10;
} while (@this != 0);
while (digits.Count > 0) {
var digit = digits.Pop();
sb.Append(superscripts[digit]);
}
return sb.ToString();
}
}
and then use that extension method somehow like this:
public class Etc {
private Label someWinFormsLabel;
public void Foo(int n, int m) {
// we want to write the equation x + x^N + x^M = 0
// where N and M are variables
this.someWinFormsLabel.Text = string.Format(
"x + x{0} + x{1} = 0",
n.ToSuperscriptNumber(),
m.ToSuperscriptNumber()
);
}
// the result of calling Foo(34, 2798) would be the label becoming: x + x³⁴+ x²⁷⁹⁸ = 0
}
Following this idea, and with a few extra tweaks, (like hooking into a textbox's TextChange and whatnot event handlers) you could even allow users to edit such "superscript compatible" strings (by toggling "superscript mode" on and off from some other button on your user interface).

- 3,242
- 17
- 31
you can convert unicode into string for superscript, subscript and for any other symbol and add to the string. For example:If you want 10^6 you can write code as follows in C# or other..
unicode for power 6 is U+2076 and for power 7 is U+2077, so you can write x^6+x^7 as
label1.Text = "X"+(char)0X2076+"X"+(char)0x2077;

- 1
- 2
I don't think there is a exact or proper way of doing this. But one way you can do this is by entering the digit that you want to be a exponent into this website https://lingojam.com/SuperscriptGenerator.
And then copy the converted version. So for example I put a 3 in there, and the converted version I got was ³. Then you just connect it together.
m³
Now you can just add it to the label...
mylabel.Text="m³";
or anyway you want to.

- 176
- 1
- 16