Results 1 to 11 of 11

Thread: [2005] How To Change the Color of a Typed Word in a RTB

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2006
    Posts
    247

    [2005] How To Change the Color of a Typed Word in a RTB

    I am making a coding assistant program, and I want to make it so that after a user presses the space bar, if the last word typed is one that the program recognizes, it will turn it to the color specified.

    Basically, I want to do syntax highlighting. If there is any easier way than this please tell me, or else post some code.



  2. #2
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [2005] How To Change the Color of a Typed Word in a RTB

    I don't know how, but I did a search and found a sample for something similar. Perhaps this can guide you.

    http://pietschsoft.com/Blog/Post.aspx?PostID=491

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jan 2006
    Posts
    247

    Re: [2005] How To Change the Color of a Typed Word in a RTB

    Great, but, How do I use it?



  4. #4
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170

    Re: [2005] How To Change the Color of a Typed Word in a RTB

    That person there is creating an inherited control. The control has been inherited from the existing RTB control, he's just adding his own routines to it and overriding a few existing routines.

    You could start by... copy-pasting that code into a windows control project class library, compiling it, and then adding that control to another project ( a test project). Then start hacking away at the code.

  5. #5
    PowerPoster sparrow1's Avatar
    Join Date
    May 2005
    Location
    Globetrotter
    Posts
    2,820

    Re: [2005] How To Change the Color of a Typed Word in a RTB

    Quote Originally Posted by Seraphino
    I am making a coding assistant program, and I want to make it so that after a user presses the space bar, if the last word typed is one that the program recognizes, it will turn it to the color specified.

    Basically, I want to do syntax highlighting. If there is any easier way than this please tell me, or else post some code.
    Hi,

    I think you can use the ColorDialog for that, here's an exa^mple with a RichTextBox and a Button.

    VB Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         If ColorDialog1.ShowDialog <> Windows.Forms.DialogResult.Cancel Then
    3.             rtxtEditor.ForeColor = ColorDialog1.Color
    4.         End If
    5.     End Sub

    Hope it helps,

    sparrow1
    Wkr,
    sparrow1

    If I helped you, don't forget to Rate my post. Thank you

    I'm using Visual Studio.Net 2003 and
    2005
    How to learn VB.Net Create setup with VB 2005 Drawing for beginners VB.Net Tutorials GDI+ Tutorials
    Video's for beginners

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Jan 2006
    Posts
    247

    Re: [2005] How To Change the Color of a Typed Word in a RTB

    Nope, this isn't what I wanted, but thanks for the tip!



  7. #7
    Addicted Member
    Join Date
    Mar 2006
    Posts
    235

    Re: [2005] How To Change the Color of a Typed Word in a RTB

    Try

    Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            If ColorDialog1.ShowDialog <> Windows.Forms.DialogResult.Cancel Then
                rtxtEditor.SelectionColor = ColorDialog1.Color
            End If
        End Sub

  8. #8
    Frenzied Member
    Join Date
    Mar 2005
    Location
    Sector 001
    Posts
    1,577

    Re: [2005] How To Change the Color of a Typed Word in a RTB

    Ken B is right, there is no other way to change the color of only some letters in a generic richtextbox unless you select them first and then use "richtextbox.SelectionColor = Color.some color"

    (I'd be gladly wrong, since the whole select business is kind of fishy, one unintented click from the user can wreak havoc in your formatting logic)
    VB 2005, Win Xp Pro sp2

  9. #9
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: [2005] How To Change the Color of a Typed Word in a RTB

    (I'd be gladly wrong, since the whole select business is kind of fishy, one unintented click from the user can wreak havoc in your formatting logic)
    The click won’t change the selection property of the Rich Tex Box. For example the “SelectionText” of the RichTextBox would always be the last word in the control unless it is changed manually.

  10. #10
    Frenzied Member
    Join Date
    Mar 2005
    Location
    Sector 001
    Posts
    1,577

    Re: [2005] How To Change the Color of a Typed Word in a RTB

    Quote Originally Posted by VBDT
    The click won’t change the selection property of the Rich Tex Box. For example the “SelectionText” of the RichTextBox would always be the last word in the control unless it is changed manually.
    I just checked it, RichTextBox1.Select(0, 8) -> user clicks inside -> RichTextBox1.SelectionColor = Color.Red doesn't change the color of the previous selection to red.
    VB 2005, Win Xp Pro sp2

  11. #11
    PowerPoster VBDT's Avatar
    Join Date
    Sep 2005
    Location
    CA - USA
    Posts
    2,922

    Re: [2005] How To Change the Color of a Typed Word in a RTB

    Yes you are right! On the click event “SelectionText’s” index is set to 0. Never mind my previous comment.

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