3

I need to format some string in Superscript (code behind) in c#,

for example,

Input => 100 Output => 10^2

where Output<string> is a Clr Property

I have tried with the below code snippet,

 TextBlock _textBlock = new TextBlock();
    _textBlock.Text = "10";
    _textBlock.Inlines.Add(new Run() { BaselineAlignment  = BaselineAlignment.Superscript, Text = "2"});

As Typography is Read Only property in the above case, i can't set the Typography.Variants in code behind.

Note: Please do note that this question is not about rendering, this is about reading the Text as string. Expected value is 10^2. While suggesting duplicates please check other question addresses this.

Could any one guide me on this?

And also while set BaselineAlignment.Superscript like the above mentioned code snippet, it doesn't render like the superscript (small font) instead it renders in the same FontSize (instead it looks like setting the Margin property for the superscript content alone like new Thickness(0,0,0,FontSize) )

Joy Rex
  • 608
  • 7
  • 32
  • @Sriram Sakthivel, As the duplicated question "Set superscript and subscript in formatted text in wpf" doesn't have any solutions to resolve my scenario, could please help me in this query, before close this query as duplicate? – Joy Rex May 26 '14 at 06:54
  • I don't understand what is different in your scenario? The snippet you have in question currently works fine for me. I can see `10^2`. – Sriram Sakthivel May 26 '14 at 07:04
  • @SriramSakthivel , are you able to get `10^2` in `_textBlock.Text`? – Joy Rex May 26 '14 at 07:13
  • Yes, I just tried and got `10^2`. Trust me. – Sriram Sakthivel May 26 '14 at 07:14
  • @SriramSakthivel , i have check even with the `immediate Window` but still i'm getting `10` alone in the `_textBlock.Text` property for the provided code snippet. could you please share the code snippet you have used(in case of any modification)? – Joy Rex May 26 '14 at 07:17
  • Oops! you're asking about text to be `10^2`? No I don't think you can get it. What I'm talking about is `10^2` is rendered in the screen. – Sriram Sakthivel May 26 '14 at 07:20
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/54416/discussion-between-joy-oyiess-rex-and-sriram-sakthivel). – Joy Rex May 26 '14 at 07:33
  • I think he wants it formatted like this 10² – Steve May 26 '14 at 07:44
  • @DarrenYoung yes., i need to format like this 10² and need to store the formatted value in a string property. – Joy Rex Jun 02 '14 at 06:02

1 Answers1

3

You need to use the Unicode character for superscript 2. Have a look here: http://msdn.microsoft.com/en-us/library/aa664669%28v=vs.71%29.aspx

If you want it in a string literal you could use:

 var value = "10\xB2";

Live Demo

In WPF you can use Typeography variants.

Set superscript and subscript in formatted text in wpf

Community
  • 1
  • 1
Steve
  • 2,950
  • 3
  • 21
  • 32
  • 10^2 is not the only case, so that i can use the unicode for 2. But it can be any number (like 75^289). so in that case, is it possible to achieve through unicode support? – Joy Rex May 26 '14 at 08:45
  • Yes, look up the Typeography variants in the link above. – Steve May 26 '14 at 16:50
  • @DarrrenYoung , as mentioned earlier i can render directly in the UI using Typography. But my requirement is to set the Superscript value (like 10^2) to a String property. So i can't achieve it through typography in the code behind. can u provide me any code snippet? – Joy Rex May 27 '14 at 07:18
  • @JoyOyiessRex Did you get this sorted? Just edited my answer above. – Steve May 28 '14 at 20:58
  • Thanks for the provided link, but the unicode can used for smaller power values like 10²(10^2) , but can we use it for larger power values (like 10^63)? – Joy Rex Jun 02 '14 at 06:06
  • 1
    Yes. Console.WriteLine("10\x2076\xB3"); – Steve Jun 02 '14 at 17:37