Reputation: 625
I am writing a chat application in C#, and I would like also to display the time of the arrival of the message, just after the message, starting from the right?
How can I write in a textBox or a richTextBox starting from the right?
This is how my code looks for now:
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");
Upvotes: 1
Views: 776
Reputation: 85
Instead of appendtext, I would suggest string.format
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);
You could also append the string after using a length function of the main text, and adding the date after.
Upvotes: 0
Reputation: 625
Thanks :) It worked with the alignment property :
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;
}
Upvotes: 0
Reputation: 12858
Use the TextBox.TextAlignment Property
textbox1.TextAlignment = TextAlignment.Right;
Otherwise, if it is a fixed size and there are no line breaks, you could do something like this
string time = "12:34PM";
string text = "Hello".PadRight(100) + time;
textBox1.AppendText(text + "\n");
or using your existing code... maybe something like this?
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");
Upvotes: 1