【问题标题】:Right and Left alignment richtextbox c#左右对齐richtextbox c#
【发布时间】:2015-01-19 04:54:00
【问题描述】:

我想在 C# 中制作一个可以在其中显示某些左向和右向文本的框。 例如,

代码

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

我尝试了richTextBoxSelectionAlignment 功能,但它对框中的所有文本都应用了特定格式。我怎样才能达到预期的效果?任何帮助将不胜感激。

【问题讨论】:

  • 用Panel代替RichTextBox会更好...

标签: c# winforms alignment richtextbox


【解决方案1】:

您想使用RichTextBox.SelectionAlignment。无耻地从another SO answer盗取。

看来您必须附加文本,选择它,然后更改 SelectionAlignment。

【讨论】:

    【解决方案2】:

    您可以将Environment.NewlineRichTextBox.SelectionAlignment 用于您的richTextBox。

    例如:

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

    【讨论】:

    • 非常感谢,这非常适合我的示例
    【解决方案3】:

    这个也可以:)

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

    【讨论】:

      【解决方案4】:

      要设置附加文本的对齐方式,您需要只选择附加文本,然后使用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();
          }
      

      经过一番测试,似乎只要 text 字符串不包含换行符,只需设置 SelectionAlignment 就可以工作,但如果有嵌入的换行符,则只有最后附加的行正确对齐。

          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.
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-11-22
        • 2013-05-22
        • 1970-01-01
        • 1970-01-01
        • 2021-02-21
        • 2012-03-27
        • 2017-03-07
        • 2011-11-25
        相关资源
        最近更新 更多