Using RichTextBox and Writing Mathematical Equations in Windows Phone

It’s always been a problem for the developers of Windows Phone applications that they are unable to write mathematical equations on Windows Phone when they are building applications for mathematics. Especially beginners encounter many problems doing it.

How can you write a mathematical equation? How can you view as it is?
Here is a simple way of doing it.

Let us have the following equation:

                                                function

I have a text block in XAML of my application that is “MathTB” , now in C# code I will write my equation.

Here I have used Unicode to display the super script \u00B2 to display the super script in Windows Phone in a text block.

In other cases you may use a rich text block to perform different properties and layouts of the text.

These may be:

  • Ø Bold Text
  • Ø Underlined Text
  • Ø Hyperlink
  • Ø Colored and Black text at same time
  • Ø Italics and Bold at same time

And many more you can use as in the following:

  1. MathTB.Text = "(x + 1)";  
  2.   
  3. MathTB.Text += String.Format(" {0,-3}"'\u00B2');  
  4.   
  5. MathTB.Text += " = ";  
  6.   
  7. MathTB.Text += "x";  
  8.   
  9. MathTB.Text += String.Format(" {0,-3}"'\u00B2');  
  10.   
  11. MathTB.Text += " + ";  
  12.   
  13. MathTB.Text += "2x + 1";  
In this case I have programmatically displayed a rich text box and applied a few features to it.
  1. Paragraph prgParagraph = new Paragraph();  
  2.   
  3. // create some text, and add it to the paragraph  
  4.   
  5. Bold bldText = new Bold();  
  6.   
  7. bldText.Inlines.Add(new Run() { Text = "(x + 1)" });  
  8.   
  9. Italic itlText = new Italic();  
  10.   
  11. itlText.Inlines.Add(new Run() { Text = String.Format(" {0,-3}"'\u00B2'), Foreground = new SolidColorBrush(Colors.Yellow) });  
  12.   
  13. Underline unText = new Underline();  
  14.   
  15. unText.Inlines.Add(new Run() { Text = "This is some example text, underlined" });  
  16.   
  17.   
  18. Bold bldTextWithItalic = new Bold();  
  19.   
  20. bldTextWithItalic.Inlines.Add(new Italic()    
  21. {  
  22.   
  23.    Inlines = { new Run()   
  24.   
  25.    { Text = "This is some example text, bold and italic" } }  
  26.   
  27. });  
  28.   
  29. prgParagraph.Inlines.Add(bldText);  
  30.   
  31. prgParagraph.Inlines.Add(itlText);  
  32.   
  33. prgParagraph.Inlines.Add(new LineBreak());  
  34.   
  35. prgParagraph.Inlines.Add(unText);  
  36.   
  37. prgParagraph.Inlines.Add(new LineBreak());  
  38.   
  39. prgParagraph.Inlines.Add(bldTextWithItalic);  
  40.   
  41. rbtMyRichTextBox.Blocks.Add(prgParagraph);  
Note: You can change the color of the text if you want by using SolidColorBrush(Colors.~your color~).

That is all you need to do to write a  mathematical equation in Windows Phone using a rich textbox or a simple text block.

Happy coding!

Personal Blog: Blend Expression.

 

Next Recommended Reading Using LINQ to SQL in Windows Phone 7