如何在C#中从文本框的末尾开始编写文本?

如何在C#中从文本框的末尾开始编写文本?,c#,textbox,richtextbox,reverse,C#,Textbox,Richtextbox,Reverse,我正在用C#编写一个聊天应用程序,我还想显示消息到达的时间,就在消息之后,从右边开始? 如何在文本框或richTextBox中从右侧开始书写 这就是我的代码现在的样子: textBox1.SelectionFont = new Font("Arial", 12, FontStyle.Regular); textBox1.AppendText(text + "\n"); textBox1.SelectionFont = new Font("Arial

我正在用C#编写一个聊天应用程序,我还想显示消息到达的时间,就在消息之后,从右边开始?
如何在文本框或richTextBox中从右侧开始书写

这就是我的代码现在的样子:

        textBox1.SelectionFont = new Font("Arial", 12, FontStyle.Regular);
        textBox1.AppendText(text + "\n");
        textBox1.SelectionFont = new Font("Arial", 8, FontStyle.Italic);
        textBox1.AppendText("sent at " + DateTime.Now.ToString("h:mm") + "\n");
使用

否则,如果它是一个固定的大小,并且没有换行符,您可以这样做

string time = "12:34PM";
string text = "Hello".PadRight(100) + time;
textBox1.AppendText(text + "\n");
或者使用您现有的代码。。。也许是这样的

textBox1.SelectionFont = new Font("Arial", 12, FontStyle.Regular);
textBox1.AppendText(text + "\n");
textBox1.SelectionFont = new Font("Arial", 8, FontStyle.Italic);
textBox1.AppendText(("sent at " + DateTime.Now.ToString("h:mm")).PadLeft(100) + "\n");
谢谢:) 它使用“路线”属性:

        textBox1.SelectionFont = new Font("Arial", 12, FontStyle.Regular);
        textBox1.AppendText(text + "\n");
        textBox1.SelectionFont = new Font("Arial", 8, FontStyle.Italic);
        textBox1.SelectionAlignment = HorizontalAlignment.Right;
        textBox1.AppendText("sent at " + DateTime.Now.ToString("h:mm") + "\n");
        textBox1.SelectionAlignment = HorizontalAlignment.Left;

    }

我建议使用string.format而不是appendtext

string text = "This is the message \n";
string dt = "sent at " + DateTime.Now.ToString("h:mm") + "\n"
textBox1.Text = string.Format("{0} {1}", text, dt);

您还可以在使用主文本的长度函数后追加字符串,并在其后添加日期。

您是否询问如何右对齐文本的一半?这是使用Windows窗体还是WPF?
string text = "This is the message \n";
string dt = "sent at " + DateTime.Now.ToString("h:mm") + "\n"
textBox1.Text = string.Format("{0} {1}", text, dt);