i want my own Instant Messager (Chat). And the normal textbox doesn’t support formatted/colored text.
I read some articles about the Richtextbox in WPF and the new concept with Blocks, Paragraphs and Runs is pretty interesting.
Is it a good idea to serialize these objects and send them to the other chat-clients? (The text should be formatted, like the author’s orginal text)
If i want to add the blocks from the input-textbox to the output-textbox (only for testing), i get a exception that the blocks/paragraphs are used by an other richtextbox.
Then i saved the reference from these objects, removed it from the first textbox and added them to the second textbox.
For example:
FlowDocument oldTextDocument = richTextBoxMessageBox.Document;
richTextBoxMessageBox.Document = new FlowDocument();
while(oldTextDocument.Blocks.Count > 0)
{
richTextBoxChatHistory.Document.Blocks.Add(oldTextDocument.Blocks.FirstBlock);
}
(I cant do it with for-each because this will cause a exception.)
I don’t think it’s a good idea to send serialized objects to the other clients, because they will have some (considerable) overhead.
I did some time ago a chat application and i used (successfully) the WebBrowser as the main control in the chat windows (which supports a lot of formating, rich media, etc), and I only send over the network html text (encrypted).
Regarding your code, you cannot have the same instance of a Paragraph for example in two different controls, because it is a ContentElement – like you cannot have the same Label in two different panels.