Results 1 to 10 of 10

Thread: Colored words in textbox

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2008
    Posts
    91

    Colored words in textbox

    hey guys, well I was wondering if there's anyway to color stuff in a textbox?
    example:
    I have a big textbox and if I type hi in that textbox I want that text to become grey, and if I type bye I want the text to become red, any ideas?

    btw, I took your advice from my latest thread and installed a stable version of 2005

    I did some thinking and come up with the idea "if textbox1.text ="hej" then" set a color but then I realized the whole text would become to coded text, hmm..
    Last edited by macbrutal; Jan 28th, 2010 at 05:11 AM. Reason: Thoughts

  2. #2

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Feb 2008
    Posts
    91

    Re: Colored words in textbox

    Cicatrix, thank you for the reply!

    Please define your idea, so if I replace my textbox to a RTF what should I do next? I've tried to google this, but can't find any codes/explainations

  4. #4
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Colored words in textbox

    vb.net Code:
    1. ' Put a RichTextBox control on a form and name it RichTextBox1
    2. Public Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    3.         RichTextBox1.Text = "My text. This is a blue text. This is a red text."
    4.  
    5.         Dim pos As Integer = RichTextBox1.Find("blue text")
    6.         With RichTextBox1
    7.             .Select(pos, "blue text".Length)
    8.             .SelectionColor = Color.Blue
    9.             pos = .Find("red text")
    10.             .Select(pos, "red text".Length)
    11.             .SelectionColor = Color.Red
    12.         End With
    13.  
    14.     End Sub

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Feb 2008
    Posts
    91

    Re: Colored words in textbox

    Cicatrix, thank you for the reply once again but I don't think we get each other
    If I type "hello" in this RTF I want all the hello's to be red and if I type "bye" I want all the "bye's" to be blue if you know what I mean?

  6. #6
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Colored words in textbox

    Quote Originally Posted by macbrutal View Post
    Cicatrix, thank you for the reply once again but I don't think we get each other
    If I type "hello" in this RTF I want all the hello's to be red and if I type "bye" I want all the "bye's" to be blue if you know what I mean?
    It can't be done automatically. I showed you the principles involved.
    In your case you should catch a TextChanged event of your richtextbox and do the search for your hello and bye strings and change their colors.

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Feb 2008
    Posts
    91

    Re: Colored words in textbox

    I see.. Thank you cicatrix

  8. #8
    Lively Member
    Join Date
    Jun 2010
    Location
    Spain
    Posts
    92

    Re: Colored words in textbox

    Quote Originally Posted by cicatrix View Post
    vb.net Code:
    1. ' Put a RichTextBox control on a form and name it RichTextBox1
    2. Public Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    3.         RichTextBox1.Text = "My text. This is a blue text. This is a red text."
    4.  
    5.         Dim pos As Integer = RichTextBox1.Find("blue text")
    6.         With RichTextBox1
    7.             .Select(pos, "blue text".Length)
    8.             .SelectionColor = Color.Blue
    9.             pos = .Find("red text")
    10.             .Select(pos, "red text".Length)
    11.             .SelectionColor = Color.Red
    12.         End With
    13.  
    14.     End Sub
    please how to change to between bold/normal?
    because .SelectionFont.Bold = True doesn't work (read only)

  9. #9
    PowerPoster cicatrix's Avatar
    Join Date
    Dec 2009
    Location
    Moscow, Russia
    Posts
    3,654

    Re: Colored words in textbox

    You can't change it this way, you must assign a new font.
    Code:
    With RichTextBox1
      ' ...
      .SelectionFont = New Font(.Font.FontFamily, .Font.Size, FontStyle.Bold)
      ' ...
    End With

  10. #10
    Lively Member
    Join Date
    Jun 2010
    Location
    Spain
    Posts
    92

    Re: Colored words in textbox

    thanks.
    wonder why is not implemented in an easy way.
    if I can change only bold or italic.

    oh! sure! an language with easy sentences are not so proffesional...

    (sorry I can't give you the rate because maybe my last was also for you)

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