Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Center Justify paragraph in RichTextBox

I use a RichTextBox, and I want to format all the lines in a paragraph with justify alignment, except the last line will be aligned to the center.

as this:

      sssssssssssssssssssssssss
      sssssssssssssssssssssssss
      sssssssssssssssssssssssss
           ssssssssssssss     

I use this code for justify alignment.

like image 845
user1543998 Avatar asked Aug 07 '12 10:08

user1543998


People also ask

How do I center text in RichTextBox?

For instance if you want the whole textbox centered, then you would do: richTextBox1. SelectAll(); richTextBox1. SelectionAlignment = HorizontalAlignment.

What is RichTextBox in Windows Form applications?

The Windows Forms RichTextBox control is used for displaying, entering, and manipulating text with formatting. The RichTextBox control does everything the TextBox control does, but it can also display fonts, colors, and links; load text and embedded images from a file; and find specified characters.


Video Answer


1 Answers

What you want is "center justify". Modify the enum, it's #5 below:

/// <summary>
/// Specifies how text in a <see cref="AdvRichTextBox"/> is
/// horizontally aligned.
/// </summary>
public enum TextAlign
{
    /// <summary>
    /// The text is aligned to the left.
    /// </summary>
    Left = 1,

    /// <summary>
    /// The text is aligned to the right.
    /// </summary>
    Right = 2,

    /// <summary>
    /// The text is aligned in the center.
    /// </summary>
    Center = 3,

    /// <summary>
    /// The text is justified.
    /// </summary>
    Justify = 4,

    /// <summary>
    /// The text is center justified.
    /// </summary>
    CenterJustify = 5
}

enter image description here

Sample code:

private void Form1_Load(object sender, EventArgs e)
{
    AdvRichTextBox tb = new AdvRichTextBox();

    tb.SelectionAlignment = TextAlign.CenterJustify;
    tb.SelectedText = "Here is a justified paragraph. It will show up justified using the new AdvRichTextBox control created by who knows.\n";

    tb.Width = 250;
    tb.Height = 450;

    this.Controls.Add(tb);
}
like image 111
Chris Gessler Avatar answered Oct 13 '22 04:10

Chris Gessler