C#, RichTextBox using a textbox as a text container and showing a few lines from it on itself
<p>So what I want to do is have my RichTextBox hold another TextBox. This text box will hold all of the text available to be displayed on its parent RichTextBox. I have a function VisibleLines() </p>

<pre><code>private int VisibleLines()
    {
        //Get the height of the text area.
        int height = TextRenderer.MeasureText(this.Text, this.Font).Height;
        //rate = visible height / Total height.
        float rate = (1.0f * this.Height) / height;
        //Get visible lines.
        return (int)(this.Lines.Length * rate);
    }
</code></pre>

<p>Which tells me how many lines are visible to the user on the RTB (RichTextBox). So my few questions are...</p>

<p>~ How can I keep my TextBox up-to-date with what is being typed or deleted or pasted to the RTB? </p>

<ul>
<li>I plan to do with with keep track of the cavet position in the OnTextChanged method in the RTB. Doing so by keep a int which saves the last known cavet position when OnTextchanged was last called and comparing that to the current cavet position to get an offset. If the offset is larger than zero, append a substring of the text in the RTB to the TextBox, and if it is smaller than zero save the TextBox as a substring of itself from zero to its length plus the cavet offset.</li>
</ul>

<p>~ How do I update what text is shown in the RTB from the TextBox</p>

<ul>
<li>I plan to do this by trying to pull however many visible lines are on the RTB from the Textbox. But how can I implement scrolling on the RTB? Possibly if a line is not visible replace the line with nothing or just a space. Then when a line is visible replace it with what the TextBox has on that line into the RTB. So each box should have the same number of lines the difference being the RTB has blanks lines for the ones the user cannot see, while the TextBox keeps track of these lines. </li>
</ul>

<p>I am doing this because I am trying to color certain characters and sequences of characters the user sees, my problem is when a large file is open or a large amount of text is pasted it freezes up, trying to color each line even the ones not seen.. So the RTB will have colored lines that are only colored and shown when visible to the user.</p>

<p>Any potential problems anyone sees? Is this the easiest way to do this?</p>

<ul>
<li>I also this there will be a problem with pasting. Say a user is the the very end of the text, then clicks into a line or so above and pastes. My last known cavet position is the end of the text, now when I calculate my offset it will be negative even though I added a bunch of text, so I cannot really keep track of the text this way...</li>
</ul>
