How can I add superscript power operators in c# winforms?

c#, winforms

Solution

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.

Problem

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.

Original source

Related problems