I have a coming back problem that I dont know how to get out from.
If you put this code in the event handler for a richtextBox control and write this on the First line:
1234567890 //This will turn red when you press 0

If you instead right away press enter so you come to the second line in the multilined textBox and write this:
123456789 //Then this will turn red when you press 9

So it seems that it uses the /n next line character as a character.

What I wonder is how this is possible to to solve as I want the code to only count the characters on the current line where the cursor is, so it would be: 1234567890 for all lines.

I use this code in the richtextBox event handler:

int positionen = 0;
positionen = richtextBox1->SelectionStart;

if ( richtextBox1->SelectionStart >= 10 )
{
this->richtextBox1->Select((positionen - 10), positionen);
this->richtextBox1->SelectionColor = Color::Red;
this->richtextBox1->DeselectAll();
this->richtextBox1->SelectionStart = positionen;
}

This is a straightforward example of what is happening.
The text that gets selected, I can get the selected like this.
I have also put a messagebox to show what text that is selected.

If putting this ready code inside a richTextBox1 eventhandler and after that write these 2lines in the box and important , start writing the second line first and then write the first line.

123456 hello

123456789


When you have written the word: hello then this will be selected and turn red:
hello

12345

I wonder how this can be prevented as I only want to select ´hello´ ?

int positionen = 0;
positionen = richTextBox1->SelectionStart;
char one, two, three, four, five;
			
if ( richTextBox1->SelectionStart >= 1 ) { one = richTextBox1->Text[positionen - 1];}
if ( richTextBox1->SelectionStart >= 2 ) { two = richTextBox1->Text[positionen - 2];}
if ( richTextBox1->SelectionStart >= 3 ) { three = richTextBox1->Text[positionen - 3];}
if ( richTextBox1->SelectionStart >= 4 ) { four = richTextBox1->Text[positionen - 4];}
if ( richTextBox1->SelectionStart >= 5 ) { five = richTextBox1->Text[positionen - 5];}
			
	
if ( richTextBox1->SelectionStart >= 5 )
{

if( one == 'o' && two == 'l' && three == 'l' && four == 'e' && five == 'h' )
{
	this->richTextBox1->Select((positionen - 5), positionen);
	this->richTextBox1->SelectionColor = Color::Red;
			
	MessageBox::Show(this->richTextBox1->SelectedText);
}
}
if( one == 'o' && two == 'l' && three == 'l' && four == 'e' && five == 'h' )
{
    
    // Select only as many characters as in string 'hello'
    //  i.e. [B]five[/B] ...
    this->richTextBox1->Select((positionen - 5), [B]5[/B]);

    this->richTextBox1->SelectionColor = Color::Red;
    MessageBox::Show(this->richTextBox1->SelectedText);
}

Thank you very much, that was the catch.
Now it works fine.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.