Results 1 to 4 of 4

Thread: richtextbox color coding

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2003
    Posts
    6

    Question richtextbox color coding

    Does anyone know how to dynamically color words while typing in rtb? for example as soon as i type word function it should turn the word function blue... only selected words will be color coded (just like code editors).

    i am not asking about coloring the words in rtb at a time (thru find method) but am looking for dynamic color while typing.

    thanks for any help on this.
    Last edited by nanko; May 5th, 2003 at 02:48 PM.

  2. #2
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    518
    rtxtEditor.SelectionColor = FontDialog1.Color

    Or if you don't want to use fontdialog,

    rtxtEditor.SelectionColor = System.Drawing.Color.Yellow

  3. #3
    Fanatic Member
    Join Date
    Sep 2002
    Posts
    518
    Seeing your edit - if that's what you're trying to do (like IntelliSense in the development IDE? right?) then you will want some code in the KeyPress event (or maybe TextChanged) for the richtextbox that will examine the previously typed word. I spent a little time trying to puzzle out LastIndexOf to see if I could get this to work but I can't seem to get my head around the overloaded syntax (keeps giving me a exception when I specify a search length of more than 2 and I don't see why). Logically what you want to do on each keystroke is:

    1 Get whole word previously typed, measuring from insertion point to the start of the document | crlf | space | tab (actually you would want to use LastIndexOfAny)
    2 if that word is "foo", then
    3 record where the insertion point is
    4 select that word
    5 set selectioncolor to red (or blue or whatever)
    6 put the insertion point back where it was
    7 set selectionlength to zero
    8 set selectioncolor to black (or empty or whatever)


    Good luck!

  4. #4

    Thread Starter
    New Member
    Join Date
    May 2003
    Posts
    6
    i tried similar strategy as you have mentioned but instead of using indexof property i used a different way.

    basically i am saving the wordstart position as soon as i press spacebar. the next spacebar char value is saved as the end of word. then all i do is rtb.select(start, diff) and do the coloring.
    this works fine only in keypress only if you type in 1 direction and continuous. cursor jumps invoked by mouse messes it up partially.

    the next challenge is how to unformat a word if backspace is used. what i am thinking is if we can select or read the word by a cursor position before or after or inside then this maywork out.

    for example if the word is say Action then there could be 3 possibilities of changing word Action-
    by positioning cursor
    (1) before A or
    logic could be unformat the word till the next space
    (2) after n or
    logic could be unformat the word till the previous space
    (3) between A and n.
    logic could be unformat the word from previous till the next space

    i dont know how one could solve this case of unformatting...
    i need to research on indexof property... looks promising

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