Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

winforms

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.

like image 463
mikeythemissile Avatar asked Feb 23 '13 15:02

mikeythemissile


People also ask

How do I type a superscript?

Select the character that you want to format as superscript or subscript. On the Home tab, in the Font group, pick the Font Dialog Box Launcher. On the Font tab, under Effects, select the Superscript or Subscript check box.


2 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 :

enter image description here

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.

like image 55
digEmAll Avatar answered Oct 04 '22 21:10

digEmAll


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).

like image 28
Eduard Dumitru Avatar answered Oct 04 '22 23:10

Eduard Dumitru