Add text (push) to textbox or richtextbox?

631 Views Asked by At

I'm trying to create a chat application like msn. When i do "textBox.Text = textBox.Text+text" it updates the textbox and the text i got selected is no longer selected. In MSN you can have selected text and still recieve messages in different colors etc.. How do they do it? I figure its something like push messages, maybe they create a new textbox under another textbox? Any clues?

I hope you guys know what i'm talking about here. I just want my text to behave like MSN used to do, not update the whole textbox, just push a new message under the current message etc.

1

There are 1 best solutions below

2
user1100269 On

If I understand your question, you just want text to stay selected when you append messages to a RichTextBox?

int selectionStart = textBox.SelectionStart;
int selectionLength = textBox.SelectionLength;
int carat = textBox.TextLength;

textBox.Text += Environment.NewLine;
textBox.Text += newText;

//optional styling code for newly appended text
textBox.Select(carat, newText.Length);
textBox.SelectionColor = //value;
//etc.

//reapply original selection
if(selectionStart >= 0 && selectionLength > 0)
{
    textBox.Select(selectionStart, selectionLength);
}