none
SuperScript and SubScript is not working properly in richtextbox RRS feed

  • Question

  • I have created richtextbox editor using richtextbox. SuperScript and subscript is a part of functionality.

    But if i select and give SuperScript or subscript for selected content on the richtextbox, Content has been superscripted for subscript operation and vice versa. 

    Below logic i used,

    TextRange tr = new TextRange(richTextBox.Document.ContentEnd, richTextBox.Document.ContentEnd);

    tr.ApplyPropertyValue(Inline.BaselineAlignmentProperty, BaselineAlignment.Subscript);

    Please give help on this

    Monday, May 6, 2019 12:31 PM

All replies

  • Hello,

     I am not familiar with WPF projects. However, the following link

    should guide you to a proper solution;

     

     https://docs.microsoft.com/en-us/dotnet/api/system.windows.baselinealignment?view=netframework-4.8

     Hope this helps :)

    Monday, May 6, 2019 2:21 PM
  • Hi,

    What is the type of your project? A Winforms or a WPF? Before handling the issue, we need to confirm it.

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, May 7, 2019 3:10 AM
  • Hi,

    Its WPF application

    Thursday, May 9, 2019 10:06 AM
  • Hi,

    According to your description, your issue is more related about WPF. And this forum is discussing and asking questions about the Windows Forms and ClickOnce. So I will move this thread to corresponding forum: Windows Presentation Foundation (WPF) for dedicated information.

    Thank you for your understanding.

    Regards,

    Kyle


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, May 10, 2019 2:23 AM
  • I have created richtextbox editor using richtextbox. SuperScript and subscript is a part of functionality.

    But if i select and give SuperScript or subscript for selected content on the richtextbox, Content has been superscripted for subscript operation and vice versa. 

    Below logic i used,

    TextRange tr = new TextRange(richTextBox.Document.ContentEnd, richTextBox.Document.ContentEnd);

    tr.ApplyPropertyValue(Inline.BaselineAlignmentProperty, BaselineAlignment.Subscript);

    Please give help on this

    Hello,

    According to your description, you want to apply SuperScript and SubScript for RichTextBox.selection. I do one sample that you can take a look:

    <StackPanel>
            <RichTextBox Name="richtextbox" Margin="10">
                <FlowDocument>
                    <Paragraph FontSize="36">Hello, world!</Paragraph>
                    <Paragraph
                        FontSize="14"
                        FontStyle="Italic"
                        Foreground="Gray"
                        TextAlignment="Left">
                        Thanks to the RichTextBox control, this FlowDocument is completely editable!
                    </Paragraph>
                </FlowDocument>
            </RichTextBox>
    
            <Button
                Name="btn1"
                Width="200"
                Height="30"
                Margin="10"
                Click="Btn1_Click"
                Content="SuperScript" />
            <Button
                Name="btn2"
                Width="200"
                Height="30"
                Margin="10"
                Click="Btn2_Click"
                Content="SubScript" />
        </StackPanel>


     private void Btn1_Click(object sender, RoutedEventArgs e)
            {
                var currentAlignment = richtextbox.Selection.GetPropertyValue(Inline.BaselineAlignmentProperty);
    
                BaselineAlignment newAlignment = ((BaselineAlignment)currentAlignment == BaselineAlignment.Superscript) ? BaselineAlignment.Baseline : BaselineAlignment.Superscript;
                richtextbox.Selection.ApplyPropertyValue(Inline.BaselineAlignmentProperty, newAlignment);
            }
    
            private void Btn2_Click(object sender, RoutedEventArgs e)
            {
                var currentAlignment = richtextbox.Selection.GetPropertyValue(Inline.BaselineAlignmentProperty);
    
                BaselineAlignment newAlignment = ((BaselineAlignment)currentAlignment == BaselineAlignment.Subscript) ? BaselineAlignment.Baseline : BaselineAlignment.Subscript;
                richtextbox.Selection.ApplyPropertyValue(Inline.BaselineAlignmentProperty, newAlignment);
            }

    Best Regards,

    Cherry


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, May 10, 2019 8:51 AM
    Moderator