-
Multilined RichTextBox
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:
Code:
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;
}
-
could you get selection text and look for newline character in it - and if found - exit event handler?
-
Yes I can get the selected text like this.
I am not sure how or if the newline character could be indicated and how it could be a solution.
I have put a messagebox to show what text that is selected. If putting this code inside a richTextBox1 eventhandler and after that write these 2 lines in the box and important start with 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 blue:
hello
12345
I wonder how this can be prevented as I only trying to select ´hello´ ?
Code:
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::Blue;
MessageBox::Show(this->richTextBox1->SelectedText);
}
}
-
I did find the problem, I think. To change this line:
Code:
this->richTextBox1->Select((positionen - 5), 5);