Thread: Multilined RichTextBox

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    385

    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;
    }

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    could you get selection text and look for newline character in it - and if found - exit event handler?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    385
    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);
    	}
    }
    Last edited by Coding; 03-21-2008 at 06:31 AM.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    385
    I did find the problem, I think. To change this line:

    Code:
    this->richTextBox1->Select((positionen - 5), 5);

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Disable RichTextBox mouse wheel
    By theoobe in forum C# Programming
    Replies: 2
    Last Post: 05-08-2009, 11:27 AM
  2. Change RichTextBox Color
    By C_ntua in forum C# Programming
    Replies: 8
    Last Post: 12-18-2008, 03:44 PM
  3. RichTextBox
    By Coding in forum Windows Programming
    Replies: 5
    Last Post: 02-03-2008, 11:23 AM
  4. RichTextBox and syntax highlighting
    By Rune Hunter in forum C# Programming
    Replies: 0
    Last Post: 06-12-2007, 02:16 PM
  5. searching text in richtextbox
    By SuperNewbie in forum C# Programming
    Replies: 4
    Last Post: 08-02-2002, 09:44 AM