I'm trying to build a WPF application to assist with some math revision and I'm having an issue with displaying the index values as superscript. I am trying to format the generated question strings to the desired output from the codebehind. I was originally trying to display the generated questions simply as strings in a textblock. (Marking index values with a '#' at the start and end of each value in the inputted string.)
private char ConvertSingleToSuperscript(char input)
{
switch (input)
{
case '0':
return '⁰';
case '1':
return '¹';
case '2':
return '²';
case '3':
return '³';
case '4':
return '⁴';
case '5':
return '⁵';
case '6':
return '6';
case '7':
return '⁷';
case '8':
return '⁸';
case '9':
return '⁹';
case ' ':
return '⁺';
case '-':
return '⁻';
case '=':
return '⁼';
case '/':
return char.Parse(char.ConvertFromUtf32(0x2044));
default:
throw new Exception("Incorrect character entered as superscript, change entered indice value.");
}
}
public string ProcessSuperscript(string input)
{
char[] tempArray = input.ToCharArray();
string outputString = "";
bool processingIndice = false;
foreach(char c in tempArray)
{
if(c == '#')
{
processingIndice = !processingIndice;
}
if (processingIndice)
{
outputString = ConvertSingleToSuperscript(c);
}
else
{
outputString = c;
}
}
return outputString;
}
Using this, or having my 'ConvertSingleToSuperScript' method return char values generated through the relevant superscript unicode would always return string where 1, 2, and 3 are displayed as superscript items and the remainder would all return text the same size as the rest of the 'non-indice' values in the string.
Recently I have instead tried using textblock inline runs to achieve the same result:
private void FormatQuestionSuperScriptDisplay(TextBlock displayBlock, string variableText)
{
char[] tempArray = variableText.ToCharArray();
string processString = "";
bool processingIndice = false;
foreach (char c in tempArray)
{
if (c == '#')
{
if (!processingIndice)
{
displayBlock.Inlines.Add(processString);
Console.WriteLine(processString " as normal");
}
else
{
displayBlock.Inlines.Add(new Run(processString) { BaselineAlignment = BaselineAlignment.Superscript });
Console.WriteLine(processString " as indice.");
}
processString = "";
processingIndice = !processingIndice;
continue;
}
processString = c;
}
displayBlock.Inlines.Add(processString);
}
}
I must have messed up here somewhere as well as now my output will come out as in the image below from the string: "abcdefghijklmnopqrstuvwxyz 12#32# 15#4589# test"
One more question I have is that all the numerical values once ran through the Inlines.Add() method are processed as superscript characters as well, how can process them (I'm not sure what the correct term is so I'll say) at a normal size matching the rest of the string?
Any assistance or pointers on what I should be reading up on would be mega helpful. Thanks for your time.
CodePudding user response:
It's work for me. Check function ProcessSuperscript.
Code behind
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
namespace SuperScript
{
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
FormatQuestionSuperScriptDisplay(tbText, "abcdefghijklmnopqrstuvwxyz 12#32# 15#4589# test");
}
private char ConvertSingleToSuperscript(char input)
{
switch (input)
{
case '0':
return '⁰';
case '1':
return '¹';
case '2':
return '²';
case '3':
return '³';
case '4':
return '⁴';
case '5':
return '⁵';
case '6':
return '6';
case '7':
return '⁷';
case '8':
return '⁸';
case '9':
return '⁹';
case ' ':
return '⁺';
case '-':
return '⁻';
case '=':
return '⁼';
case '/':
return char.Parse(char.ConvertFromUtf32(0x2044));
default:
throw new Exception("Incorrect character entered as superscript, change entered indice value.");
}
}
public string ProcessSuperscript(string input)
{
char[] tempArray = input.ToCharArray();
string outputString = "";
bool processingIndice = false;
foreach (char c in tempArray)
{
if (c == '#')
{
processingIndice = !processingIndice;
continue; //<- This code added.
}
if (processingIndice)
{
outputString = ConvertSingleToSuperscript(c);
}
else
{
outputString = c;
}
}
return outputString;
}
private void FormatQuestionSuperScriptDisplay(TextBlock displayBlock, string variableText)
{
char[] tempArray = variableText.ToCharArray();
string processString = "";
bool processingIndice = false;
foreach (char c in tempArray)
{
if (c == '#')
{
if (!processingIndice)
{
displayBlock.Inlines.Add(processString);
Console.WriteLine(processString " as normal");
}
else
{
displayBlock.Inlines.Add(new Run(processString) { BaselineAlignment = BaselineAlignment.Superscript });
Console.WriteLine(processString " as indice.");
}
processString = "";
processingIndice = !processingIndice;
continue;
}
processString = c;
}
displayBlock.Inlines.Add(processString);
}
}
}