RichTextBox

1 question. -
Ok. I really dont know how to ask a question. Basically, I have two rich textboxs. In one I have html code, and i want to the other read the code that is in the first rich textbox and display it as if it were in a web browser.

2 question -
Now, I have a rich textbox, and a web browser. I want the web browser to be able to read the code in the rich textbox. Can someone help


Answer this question

RichTextBox

  • Decima

    I assume that you are talking about a Windows Form and that the controls reside on the same form.  In this case you can access the controls via the underlying fields created on the form to managed the controls.  Thus your form may look like this:

    RichTextBox richTextBox1;
    WebBrowser webBrowser1;

    These would have been created automatically by the designer and hidden away.  To access the text of the RTB use richTextBox1.Text to get the entire text in the control.

    As far as automating this you will probably have to manage this on the form itself.  You need to decide when the contents of the RTB get flushed to the WB.  It can be either whenever the text changes or when the user clicks a button.  In either case handle the event on the form, get the text from the RTB and set the WB's DocumentText property.  You'll need to make sure the string you pass it is a well-formed HTML document first so you might need to do some preprocessing.  Here is some sample code (assumes the text in the RTB is properly formed):

    private void OnTextChanged ( object sender, EventArgs e )
    {
      //Get the text
      string text = richTextBox1.Text;

      //Do any processing on the text such as making it well-formed
      ...

      //Send to the web browser
      webBrowser.DocumentText = text;
    }

    The above code assumes that the WB is already set to handle the document text.  You may need to preload a dummy HTML page or do other setup work first.  I'm not sure.

    Hope this helps,
    Michael Taylor - 10/8/05


  • nitamedi

    Ok. Thank you for your response. You've been the first person to even give me a little insite on that. Ok, I am going to try and explain further. I get what your saying. But here it goes. I put a "tabControl" on the form. It has two tabs. On the first tab it has the richtextbox, and on the second it has the web browser. So this is what I want. While the user is inputing the html code, i want him/her to be able to see what she is writing in the web browser. I am sorry because what you told me I didnt completely understand, and maybe you didnt understand what I need. Thanks again, I hope you respond.

    P.S. I also didnt get what you meant by making the text "well-formed".

  • Stiner

    yes sir, I am building an HTML editor along with many other features, and you are also correct, this is simply for the experience. I thank you VERY much for your help, but I still have another question. I figured out how to show the input in the richtextbox to the output in the web browser. All I need to know is, how to I make the web browser "editable". You know, be able to move objects around without having to look at it in HTML

  • Xamedes

    It sounds like you're writing an HTML editor with real-time preview. Please correct me if that's not your goal.

    The thing that confuses me is that you're using a tab control. Tab controls are designed to group  related controls and hide them from the user if they're not needed for the task at hand. It sounds like you want to have both the reich text box and the web browser visable simultaneously. If so, just put both those controls side-by-side on the form.

    Now, as the userr types, you can examine the text and update the browser control by handling the TextChanged event as the previous poster indicated.

    By "well formed", the previous poster means that the HTML syntax needs to be checked. Any opened tags should be closed, tags can't overlap, etc. See any HTML guide for details.

    As a final tip, I hope you're just doing this as a learning exercise. There are many commercial, open source and free products that do this. Reinventing the wheel may not be the best use of your time.

  • RichTextBox