C # richtextbox right and left alignment

I want to create a box in which I can display a specific left oriented text and a specific C # oriented text. For example,

code

If (msg from admin)
   richTextBox.Append(rightAligned(msg))
else
   richTextBox.Append(leftAligned(msg))

      

I tried the SelectionAlignment

function richTextBox

, but applied a specific format to all the text in the field. How can I achieve the desired result? Any help would be greatly appreciated.

+3


source to share


4 answers


You can use Environment.Newline

and RichTextBox.SelectionAlignment

for your richTextBox.

Example:



if (msg from admin) {
    richTextBox.AppendText(Environment.NewLine + msg);
    richTextBox.SelectionAlignment = HorizontalAlignment.Right;
} else {
    richTextBox.AppendText(Environment.NewLine + msg);
    richTextBox.SelectionAlignment = HorizontalAlignment.Left;
}

      

+3


source


You can do that too :)



 If (...)
    {
       textBox1.TextAlign = HorizontalAlignment.Left;
       textBox1.Text = " Blah Blah ";
    }
else
   {
       textBox1.TextAlign = HorizontalAlignment.Right;
       textBox1.Text = " Blah Blah Right";
   }

      

+1


source


To just set the alignment of the attached text, you only need to select the attached text and then use the property SelectionAlignment

:

    public static void AppendLineAndAlignText(this RichTextBox richTextBox, string text, HorizontalAlignment alignment)
    {
        if (string.IsNullOrEmpty(text))
            return;
        var index = richTextBox.Lines.Length;                      // Get the initial number of lines.
        richTextBox.AppendText("\n" + text);                       // Append a newline, and the text (which might also contain newlines).
        var start = richTextBox.GetFirstCharIndexFromLine(index);  // Get the 1st char index of the appended text
        var length = richTextBox.Text.Length;     
        richTextBox.Select(start, length - index);                 // Select from there to the end
        richTextBox.SelectionAlignment = alignment;                // Set the alignment of the selection.
        richTextBox.DeselectAll();
    }

      

After testing the bit, it seems that the setting SelectionAlignment

will work as long as the line text

contains no newlines, but if there are inline newlines, only the last added line is aligned correctly.

    public static void AppendLineAndAlignText(this RichTextBox richTextBox, string text, HorizontalAlignment alignment)
    {
        // This only works if "text" contains no newline characters.
        if (string.IsNullOrEmpty(text))
            return;
        richTextBox.AppendText("\n" + text);                       // Append a newline, and the text (which must not also contain newlines).
        richTextBox.SelectionAlignment = alignment;                // Set the alignment of the selection.
    }

      

+1


source


You want to use RichTextBox.SelectionAlignment . Shamelessly stolen from another SO answer .

You seem to have to add text, select it, and then change the SelectionAlignment.

0


source







All Articles