locked
center text in a rictextbox RRS feed

  • Question

  • Is there a way to center text in a richtextbox. I know the width.  Example width is 250, font is MS San Serif, size 10 unit is point.

    "Center This"

    I'm lost on this one.

    Thanks much

    cyberal

    Saturday, July 1, 2017 12:35 AM

Answers


  • Hi cyberal1043,

    You can try to use TextAlignment.Center to set the rictextbox alignment.

    The following sample for your reference.

       <RichTextBox HorizontalAlignment="Left" Margin="30,170,0,0" Name="RichTextBox1" VerticalAlignment="Top" Height="95" Width="330"  Background="#FFFFFF69">
                <Paragraph FontFamily="Arial" FontSize="12" TextAlignment="Justify">
                    This 
                    <Run Text=" Center This " FontStyle="Italic" FontWeight="ExtraBold" />
                    <LineBreak/>
                </Paragraph>
            </RichTextBox>
            <Button x:Name="button" Content="Button" HorizontalAlignment="Left" Margin="255,270,0,0" VerticalAlignment="Top" Width="75" Click="button_Click_2"/>
        private void button_Click_2(object sender, RoutedEventArgs e)
            {
                BlockCollection MyBC = RichTextBox1.Blocks;
                foreach (Block b in MyBC)
                    b.TextAlignment = TextAlignment.Center;
            }


    The following article for you reference.

    RichTextBox in Silverlight 4:
    http://www.c-sharpcorner.com/uploadfile/e1317f/richtextbox-in-silverlight-4/

    Version Information: This feature is supported in Silverlight 4, 5.

    Note: This response contains a reference to a third party World Wide Web site. Microsoft is providing this information as a convenience to you. Microsoft does not control these sites and has not tested any software or information found on these sites; therefore, Microsoft cannot make any representations regarding the quality, safety, or suitability of any software or information found there. There are inherent dangers in the use of any software found on the Internet, and Microsoft cautions you to make sure that you completely understand the risk before retrieving any software from the Internet.

    Best Regards,

    Yohann Lu




    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.


    Monday, July 3, 2017 2:17 AM