Marco Acierno
Marco Acierno

Reputation: 14847

Select text from line A to line B in richtextbox


i'm looking for a way to select text between two lines (A and B) in a richtextbox. I tried something like this:

richTextBox1.Select
(
     richTextBox1.GetFirstCharIndexFromLine(parentesi_inizio[current_idx]),
     (
          richTextBox1.GetFirstCharIndexFromLine(parentesi_fine[current_idx]) -
          richTextBox1.GetFirstCharIndexFromLine(parentesi_inizio[current_idx]) + 1
     )
);

Inside parentesi_inizio and parentesi_fine i have the line number, i should select from line A (parentesi_inizio) to B (parentesi_fine).

After some tests i think the problem is this:

richTextBox1.GetFirstCharIndexFromLine(parentesi_fine[current_idx]) -
richTextBox1.GetFirstCharIndexFromLine(parentesi_inizio[current_idx]) + 1

This code works fine at first, but I noticed that after a while 'begins to show results stoner.

I did further testing and the lines are correct (ie, refer to the right spot) whereas "Select" does not select the entire portion or does it incorrectly (selecting parts that do not have to)

( I used google translate for the last part )

EDIT:

Imagine this text:

Hello
World || A points here (Line 1)
Guys!
This
is
a || B points here (Line 5)
line

I need to Select (not get text) this text:

World
Guys!
This
is
a

in the richtextbox.

Example images:

Case 1:

enter image description here

Case 2:

enter image description here

Thats is what i want and what the code i posted do, but after a while the code starts to bug as i said above (at the start).

EDIT 2:

I changed my code to this after varocarbas reply

richTextBox1.Select (
richTextBox1.GetFirstCharIndexFromLine(parentesi_inizio[current_idx]),

richTextBox1.GetFirstCharIndexFromLine(parentesi_inizio[current_idx])
+ count_length(parentesi_inizio[current_idx], parentesi_fine[current_idx]) );

where count_length is

private int count_length(int A, int B)
{
    // A => first line
    // B => last line

    int tot = 0;

    for (int i = A; i <= B; ++i)
    {
        // read the length of every line between A and B
        tot += richTextBox1.Lines[i].Length - 1;
    }

    // return it
    return tot;
}

but now the code not work in every case.. anyway here is a screen of a bugged case using the old code (the code posted at the start of the question)

wrong
(source: site11.com)

it select right from the first { but not reach the last } (i do some checks here and the problem is the subtraction not the lines num.)

EDIT 3:

I'm ready sorry varocarbas, i think i just wasted your time.. after see my screen i noticed the problem might be the word wrap i tried to disable it and seems now works ok.. sorry for your time.

Upvotes: 1

Views: 4598

Answers (1)

user2480047
user2480047

Reputation:

Why not relying on the lines[] array directly?

string line2 = richTextBox1.lines[1];

Bear in mind that it has its own indexing, that is, to get the first character in the third line you can do:

int firstChar3 = richTextBox1.lines[2].Substring(0, 1);

To refer to the whole richTextBox indexing system, you can rely also on GetFirstCharIndexFromLine. That is:

int startIndexLine2 = richTextBox1.GetFirstCharIndexFromLine(1); //Start index line2
int endIndexLine2 = startIndexLine2 + richTextBox1.lines[1].length - 1; //End index line2

-------- AFTER UPDATED QUESTION

Sorry about that, but I cannot see the code in the links you provided. But the code below should deliver the outputs you want:

int curStart = richTextBox1.GetFirstCharIndexFromLine(2); 
richTextBox1.Select(curStart, richTextBox1.Lines[2].Length);
string curText = richTextBox1.SelectedText;  -> "Guys!"

curStart = richTextBox1.GetFirstCharIndexFromLine(3); 
richTextBox1.Select(curStart, richTextBox1.Lines[3].Length);
curText = richTextBox1.SelectedText;  -> "This"

curStart = richTextBox1.GetFirstCharIndexFromLine(4); 
richTextBox1.Select(curStart, richTextBox1.Lines[4].Length);
curText = richTextBox1.SelectedText;  -> "is"

Upvotes: 2

Related Questions