tags:

views:

43

answers:

1

Guys,

I have a RichTextBox control on my form. The control is setup in such a way that it will wrap to the next line after 32 lines of text are input. The problem I'm having is I want to be able to retreive an array of strings representing the lines in my control. I know there is a Lines property attached to the RichTextBox, but I am experiencing 2 issues with it:

1) I ONLY want an array of strings showing the lines that are visible on the screen only. Right now the Lines array returns every single line in the RichTextBox. I only want the lines visible on the screen returned.

2) The Lines property is not giving me a true representation of my lines. It counts a "line" as a line of text ended by a carriage return or \n. So in other words, if I type 64 characters and none of them are a carriage return, then it should return 2 lines (because there are 32 characters per line). Instead, it doesn't return any lines until I hit enter. Even then, it only returns 1 line, not 2. Its acting more like a Paragraph property, if there was such a thing.

Anyone know a way around these 2 issues?I am using C# btw

+1  A: 

You have to do a few tricks to achieve this which have to do with querying for the position of the actual lines according to the character index. The following program shows one way of doing this. You might have to harden it a bit, but it should get you started:

public partial class Form1 : Form
{        
    public Form1()
    {
        InitializeComponent();                        
    }        

    private void richTextBox1_TextChanged(object sender, EventArgs e)
    {
        if (richTextBox1.Text == "")
            return; 

        // Lines corresponding to the first and last characters:
        int firstLine = richTextBox1.GetLineFromCharIndex(0); 
        int lastLine = richTextBox1.GetLineFromCharIndex(richTextBox1.Text.Length);

        // Get array of lines:
        List<string> lines = new List<string>();
        for (int i = firstLine; i <= lastLine; i++)
        {
            int firstIndexFromLine = richTextBox1.GetFirstCharIndexFromLine(i);
            int firstIndexFromNextLine = richTextBox1.GetFirstCharIndexFromLine(i + 1);

            if (firstIndexFromNextLine == -1)
            {
                // Get character index of last character in this line:
                Point pt = new Point(richTextBox1.ClientRectangle.Width, richTextBox1.GetPositionFromCharIndex(firstIndexFromLine).Y);
                firstIndexFromNextLine = richTextBox1.GetCharIndexFromPosition(pt);
                firstIndexFromNextLine += 1;
            }

            lines.Add(richTextBox1.Text.Substring(firstIndexFromLine, firstIndexFromNextLine - firstIndexFromLine));
        }

        // Print to richTextBox2 while debugging:
        richTextBox2.Text = "";
        foreach (string line in lines)
        {
            richTextBox2.AppendText(">> " + line + Environment.NewLine);
        }                        
    }
}
steinar
This seems to work. There are some wierd bugs when the richtextbox is scrolled, but I think I can fix it. Thanks for the help!
icemanind