Results 1 to 5 of 5

Thread: Richttextbox

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2011
    Posts
    118

    Richttextbox

    Hello,

    I want to make a richtextbox what can show you diffrent colors.
    I've already managed how to give 1 line a special color, but I want to know how to give everyword in a sentence a diffrent color.

    as example in a line on my textbox:

    Hello World, I want to show this diffrent colors in my richtextbox - who knows how to make it?


    This is the code I have to color just once line of the textbox.
    Code:
        Private Sub AddText(ByVal Text As String, ByVal Color As Drawing.Color)
    
            With txtChat
    
                .SelectionColor = Color
                .AppendText(Text)
    
            End With
    
        End Sub
    How do I give everyword in a richttextbox a diffrent color?

  2. #2
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: Richttextbox

    Try this
    vb Code:
    1. Private Sub AddText(ByVal Text As String, ByVal Color As Drawing.Color)
    2.         Dim strWords() As String = Text.Split(" "c)
    3.         Dim colors(3) As Drawing.Color
    4.  
    5.         colors(0) = Drawing.Color.Red
    6.         colors(1) = Drawing.Color.Blue
    7.         colors(2) = Drawing.Color.Green
    8.         colors(3) = Drawing.Color.Brown
    9.         ' add more colors if you need
    10.  
    11.         With txtChat
    12.             For j As Integer = 0 To strWords.Length - 1
    13.                 .SelectionColor = colors(j Mod colors.Length)
    14.                 .AppendText(strWords(j) & " ")
    15.             Next
    16.         End With
    17.  
    18.     End Sub



  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jan 2011
    Posts
    118

    Re: Richttextbox

    Quote Originally Posted by 4x2y View Post
    Try this
    vb Code:
    1. Private Sub AddText(ByVal Text As String, ByVal Color As Drawing.Color)
    2.         Dim strWords() As String = Text.Split(" "c)
    3.         Dim colors(3) As Drawing.Color
    4.  
    5.         colors(0) = Drawing.Color.Red
    6.         colors(1) = Drawing.Color.Blue
    7.         colors(2) = Drawing.Color.Green
    8.         colors(3) = Drawing.Color.Brown
    9.         ' add more colors if you need
    10.  
    11.         With txtChat
    12.             For j As Integer = 0 To strWords.Length - 1
    13.                 .SelectionColor = colors(j Mod colors.Length)
    14.                 .AppendText(strWords(j) & " ")
    15.             Next
    16.         End With
    17.  
    18.     End Sub

    hi,

    Actually it is giving every word a diffrent color, but the first word gets a red color, the second word a blue color, the third word a green color and the fourth word a darkgreen color. I cant give everyword the color I want. The colors are now the same always, I need to change the colors every new sentence

  4. #4
    PowerPoster
    Join Date
    Sep 2006
    Location
    Egypt
    Posts
    2,579

    Re: Richttextbox

    You said
    I want to know how to give everyword in a sentence a diffrent color.
    and this is what i gave you.

    I need to change the colors every new sentence
    How can you specify the beginning and the end of the sentence?

    Try to pass colors of each text you add, like this
    vb Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         Dim colors(3) As Drawing.Color
    3.  
    4.         colors(0) = Drawing.Color.Red
    5.         colors(1) = Drawing.Color.Blue
    6.         colors(2) = Drawing.Color.Green
    7.         colors(3) = Drawing.Color.Brown
    8.         ' add more colors if you need
    9.  
    10.         AddText("this is first line" & vbNewLine, colors)
    11.  
    12.  
    13.         colors(0) = Drawing.Color.CadetBlue
    14.         colors(1) = Drawing.Color.Coral
    15.         colors(2) = Drawing.Color.Gray
    16.         colors(3) = Drawing.Color.HotPink
    17.         ' add more colors if you need
    18.  
    19.         AddText("this is the second line with different colors", colors)
    20.     End Sub
    21.  
    22.     Private Sub AddText(ByVal Text As String, ByVal Colors() As Drawing.Color)
    23.         Dim strWords() As String = Text.Split(" "c)
    24.  
    25.         With txtChat
    26.             For j As Integer = 0 To strWords.Length - 1
    27.                 .SelectionColor = colors(j Mod colors.Length)
    28.                 .AppendText(strWords(j) & " ")
    29.             Next
    30.         End With
    31.  
    32.     End Sub



  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jan 2011
    Posts
    118

    Re: Richttextbox

    Quote Originally Posted by 4x2y View Post
    You said

    and this is what i gave you.


    How can you specify the beginning and the end of the sentence?

    Try to pass colors of each text you add, like this
    vb Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2.         Dim colors(3) As Drawing.Color
    3.  
    4.         colors(0) = Drawing.Color.Red
    5.         colors(1) = Drawing.Color.Blue
    6.         colors(2) = Drawing.Color.Green
    7.         colors(3) = Drawing.Color.Brown
    8.         ' add more colors if you need
    9.  
    10.         AddText("this is first line" & vbNewLine, colors)
    11.  
    12.  
    13.         colors(0) = Drawing.Color.CadetBlue
    14.         colors(1) = Drawing.Color.Coral
    15.         colors(2) = Drawing.Color.Gray
    16.         colors(3) = Drawing.Color.HotPink
    17.         ' add more colors if you need
    18.  
    19.         AddText("this is the second line with different colors", colors)
    20.     End Sub
    21.  
    22.     Private Sub AddText(ByVal Text As String, ByVal Colors() As Drawing.Color)
    23.         Dim strWords() As String = Text.Split(" "c)
    24.  
    25.         With txtChat
    26.             For j As Integer = 0 To strWords.Length - 1
    27.                 .SelectionColor = colors(j Mod colors.Length)
    28.                 .AppendText(strWords(j) & " ")
    29.             Next
    30.         End With
    31.  
    32.     End Sub
    Yes Im sorry for that but my english isnt very good.
    Thank you for the code!

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