How to add power operator operators in C # winforms

I know that adding a square operator to a label using its unicode value. ( How to show a superscript character in .NET GUI tables? ). Is there any way to add some power to the shortcut? My application should display polynomial functions, i.e. X ^ 7 + x ^ 6 etc.

Thanks Mike

+4
source share
3 answers

You can use the (excellent) HtmlRenderer and create your own html-enabled shortcut control.

Here is 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 in a div section that sets the font family and size of the one used by the control. Thus, you can change the size and font by changing the Font property of the label.

+9
source

You can also use the powerful UTF strings that are natively supported and do something like an extension method that converts int (or uint even) to strings, such as:

 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 this extension method 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 the help of several additional additional settings (for example, when entering TextChange and any event handlers in the text field), you can even allow users to edit such “supersorted” lines (by switching the “superscript mode” is turned on and off from some or another button on your user interface).

+3
source

you can convert unicode to string for superscript, index and for any other character and add to string. For example: if you want 10 ^ 6, you can write the code as follows in C # or another.

unicode for power 6 U + 2076, and for power 7 U + 2077, so you can write x ^ 6 + x ^ 7 as

label1.Text = "X" + (char) 0X2076 + "X" + (char) 0x2077;

0
source

Source: https://habr.com/ru/post/948230/


All Articles