CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Mar 2007
    Posts
    274

    [RESOLVED] Appending colored text to a richtextbox?

    The thread title is pretty self explanitory. I'd think this would be easy, but my approaches havent worked. Any ideas?

    I have an RTB that is filled line by line via a console application's standard output stream. I want to color parts of the line of text before it gets appended.

    Here is the line of code I have that appends the stream output line to the RTB:

    Dictionary_RTB(sender).Text += Dictionary_Output(sender);

    That line of code is essentially the same as:

    RichTextBox1.Text += (string) OuputString;


    Any ideas here?


    If I can get the character position of the first character in the last line of the RTB, that would be perfect.
    Last edited by Traps; February 13th, 2009 at 01:51 AM.

  2. #2
    Join Date
    Sep 2008
    Location
    Netherlands
    Posts
    865

    Re: Appending colored text to a richtextbox?


  3. #3
    Join Date
    Mar 2007
    Posts
    274

    Re: Appending colored text to a richtextbox?

    Didnt see your post. Until now, I think that might do it. At least its not losing the formatting now by using the append... Thanks, this should take care of it. I'll be sure to rate ya for it, and mark the post answered once I confirm it.
    Last edited by Traps; February 13th, 2009 at 03:21 AM.

  4. #4
    Join Date
    Mar 2007
    Posts
    274

    Re: Appending colored text to a richtextbox?

    Well, the formatting sticks, IF it gets applied, so its still not working correctly. Here's my code:

    Code:
    private void UpdateText(clsServer sender) 
    { 
        try { 
            Dictionary_RTB(sender).AppendText(Dictionary_Output(sender)); 
            
            Dictionary_RTB(sender).SelectionStart = Dictionary_RTB(sender).GetFirstCharIndexFromLine(Dictionary_RTB(sender).Lines.Count - 1); 
            Debug.Print(Dictionary_RTB(sender).SelectionStart); 
            MessageBox.Show(Dictionary_RTB(sender).TextLength); 
            Dictionary_RTB(sender).SelectionLength = 5; 
            Dictionary_RTB(sender).SelectionColor = Color.Red; 
            
            Dictionary_RTB(sender).SelectionStart = Dictionary_RTB(sender).TextLength - 2; 
            Dictionary_RTB(sender).ScrollToCaret(); 
        } 
        catch (Exception ex) { 
            MessageBox.Show(ex.ToString()); 
        } 
    }

    This line:

    Dictionary_RTB(sender).SelectionStart = Dictionary_RTB(sender).GetFirstCharIndexFromLine(Dictionary_RTB(sender).Lines.Count - 1);

    Returns the character position at the end of the textbox, or in otherwords, equals the .TextLength value..... Thats not what I would infer would be the return result for something that says GetFirstCharIndexFromLine....... This is at least part of the problem, if not the whole problem..... Why doesnt that line work like it should?


    I tried this (much nicer code):

    Dictionary_RTB(sender).SelectionStart = Dictionary_RTB(sender).GetFirstCharIndexOfCurrentLine()

    but it returns the same values. For example, the first time I hit that line when running my program, it returns an index of 37. There is only 1 line in the RTB, and that line is 37 characters long. It should be returning 1!
    Last edited by Traps; February 13th, 2009 at 03:44 AM.

  5. #5
    Join Date
    Mar 2007
    Posts
    274

    Re: Appending colored text to a richtextbox?

    OMG, why doesnt this work, please help me.

    Dictionary_RTB(sender).AppendText(Dictionary_Output(sender));
    string tmp0 = Dictionary_Output(sender);

    // This text formatting code is currently under testing. IT DOES NOT WORK.
    Dictionary_RTB(sender).SelectionStart = Dictionary_RTB(sender).GetFirstCharIndexOfCurrentLine();
    int tmp = Dictionary_RTB(sender).SelectionStart;
    Debug.Print(tmp); <<<<<< BREAKPOINT HERE!!!
    tmp0 = "Microsoft Windows [Version 6.0.6001]"
    tmp = 37

    this is the FIRST time that this code is hit when I run my program. Prior to that, the RTB text is empty.

    NOOOOO! tmp should = 1 or 0 ..... but not 37!!!!


    PLEASE, WHY?
    Last edited by Traps; February 14th, 2009 at 12:26 AM.

  6. #6
    Join Date
    Mar 2007
    Posts
    274

    Re: Appending colored text to a richtextbox?

    I dont know why, but I GOT IT!

    Code:
        Dictionary_RTB(sender).AppendText(Dictionary_Output(sender)); 
        string tmp0 = Dictionary_Output(sender); 
        
        // This text formatting code is currently under testing. IT DOES NOT WORK. 
        Dictionary_RTB(sender).SelectionStart = Dictionary_RTB(sender).TextLength - 2; 
        Dictionary_RTB(sender).SelectionStart = Dictionary_RTB(sender).GetFirstCharIndexOfCurrentLine(); 
        int tmp = Dictionary_RTB(sender).SelectionStart; 
        Debug.Print(tmp);

    Whatever it works, thats all the counts for me right now.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured