0

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

user437899
  • 8,879
  • 13
  • 51
  • 71

2 Answers2

1

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.

Andrei Pana
  • 4,484
  • 1
  • 26
  • 27
  • Hi Andrei,can i get the html-text from the richtextbox? (I also want to display images, is this possible?) – user437899 Dec 07 '10 at 10:01
  • You cannot get html from richtextbox with what .Net offers, you can obtain html from richtextbox only if you write a converter of your own (which will require some work). But, you can easily get html if you use the WebBrowser control instead of the RichTextBox, as i suggested above. You can put images (including animated gifs in the WebBrowser). – Andrei Pana Dec 07 '10 at 10:05
  • With WebBrowser control you can only display text, or can WebBrowser function as textbox too? – user437899 Dec 07 '10 at 11:07
  • In my chat application, i used a regular TextBox for input and the webbrowser only to render the conversation (much like yahoo messenger does). In WinForms, i once did an html editor based on the webbrowser. I never did a project in WPF with WebBrowser as an editor, but it seems possible (see this link: http://stackoverflow.com/questions/1766084/wpf-webbrowser-control-doesnt-enter-design-mode-when-the-document-property-is-ch , although that guy has some sort of a problem with it, but it seems editing is possible the way he makes it in the SetDesginMode method). – Andrei Pana Dec 07 '10 at 11:32
1

Richtextbox works like a charm for a instant chat app I've made, just make sure you study the behavior of the Flowdocument and expand her base class. So you can use stuff like the property changed c.q. dependency property, to write clean code. Indeed you've to be careful about filling a paragraph, it has to be done on a property in the controller not in the code behind of the form. If you want to keep your code clean. Just have a look at: http://www.lebroitsolutions.nl/en/projects-chat.aspx. Where you can download the code and I've provided more info. The overhead for streaming a flowdocument (not the richtextbox as an whole) is minimal.

Leo
  • 11
  • 1