Results 1 to 21 of 21

Thread: Colouring RTF Boxes

  1. #1

    Thread Starter
    New Member
    Join Date
    Nov 2005
    Posts
    9

    Question Colouring RTF Boxes

    I was wondering if there was a way to make it so that anything following certain criteria would colour it a certain way for instance making any text within brackets green or the word following '/' red. I'm just starting out with VB so forgive me if this is a noobish question :P

  2. #2
    Hyperactive Member eranfox's Avatar
    Join Date
    May 2001
    Posts
    492

    Re: Colouring RTF Boxes

    Hello Solg,
    Take a look at the link called The Project Here
    Best Regards,
    ERAN
    Eran Fox
    ASSEMBLER,C,C++,VB6,SQL...

  3. #3
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Colouring RTF Boxes

    Welcome to Forums, Solg!

    Yes, you can do that - that's the basic purpose of RTB: formatting text.
    Here is a small routine that underlines selected text. You can modify it as you wish:
    VB Code:
    1. Public Sub UnderlineText(objRTB As Object, strFind As String)
    2. '=============================================================
    3. Dim intPos As Integer, i As Integer
    4.  
    5.     With objRTB
    6.         For i = 1 To Len(.Text)
    7.             If intPos = 0 Then
    8.                 intPos = InStr(1, LCase(objRTB.Text), LCase(strFind))
    9.             ElseIf intPos > 0 Then
    10.                 intPos = InStr(intPos + Len(strFind), LCase(objRTB.Text), LCase(strFind))
    11.             End If
    12.             If Not intPos = -1 Then
    13.                 If Not intPos = 0 Then .SelStart = intPos - 1
    14.                 .SelLength = Len(strFind)
    15.                 .SelBold = True
    16.                 .SelUnderline = True
    17.             End If
    18.         Next i
    19.     End With
    20.  
    21. End Sub
    22.  
    23. 'usage:
    24. Private Sub Command1_Click()
    25. '============================
    26. Dim strFind As String
    27.  
    28.     strFind = InputBox("Find Text To Highlight", "Find Text", "text")
    29.     UnderlineText RichTextBox1, LCase(strFind)
    30.  
    31. End Sub

  4. #4

    Thread Starter
    New Member
    Join Date
    Nov 2005
    Posts
    9

    Re: Colouring RTF Boxes

    Hi guys, thanks for the responses but thats not quite what I was looking for. I was thinking that it would be for instance /text and (text) and it would occur with any text not just preprogrammed words or ones that need users to enter anything.

  5. #5
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Colouring RTF Boxes

    What would the colors be based on? If you can provide instances that work in all conditions, then you can program it in. If you have a / up to a ), then you could colorize the first and last word.

  6. #6

    Thread Starter
    New Member
    Join Date
    Nov 2005
    Posts
    9

    Re: Colouring RTF Boxes

    The colours would be set by a variable I guess I'd like them to be customizable and it'd just be one word within so coloring the first and last word wouldn't be necasary as they'd be one and the same.

  7. #7
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Colouring RTF Boxes

    I don't know what first and last would be though. That's what I was asking. I just used an example.

  8. #8

    Thread Starter
    New Member
    Join Date
    Nov 2005
    Posts
    9

    Re: Colouring RTF Boxes

    I'm still not entirely sure what you mean, the beginning would be the symbol / then next would be any word that the user chooses to put in and it would be a color I define in a variable and then a space and then the following text would be black again or something to that effect.


    Example: Hello I am Solg, how /are you?
    Last edited by Solg; Nov 26th, 2005 at 12:32 AM. Reason: Forgot Example

  9. #9
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Colouring RTF Boxes

    So, you want to start at the first "/" and then change the color of everything up to the first space? That could cause problems, but it could be done. If you add a closing tag, it would be more generic.

  10. #10

    Thread Starter
    New Member
    Join Date
    Nov 2005
    Posts
    9

    Re: Colouring RTF Boxes

    I could add a closing tag using "\" or something if it would make the program better


    Like /this?\

  11. #11
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Colouring RTF Boxes

    That would be much better. Use the .Find method, which will return the location of a string in a rtb. Change the "\" to "", and then Set .SelStart to the position. Then do the same for the "\". Then use .SelStart to the original location, and .SelLength to the difference between "\" and "/".
    This will highlight the word you want colored. Once it's highlighted, you can use .SelColor to change it.

  12. #12

    Thread Starter
    New Member
    Join Date
    Nov 2005
    Posts
    9

    Re: Colouring RTF Boxes

    Could you explain how I'd use .Find I never have before

  13. #13
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Colouring RTF Boxes

    Take a look at this sample. I didn't write it all, but it does colorize any word that you choose, and also tells you what line you click on.
    Attached Files Attached Files

  14. #14
    Hyperactive Member eranfox's Avatar
    Join Date
    May 2001
    Posts
    492

    Re: Colouring RTF Boxes

    Hello dglienna,
    I tried your code but had to add few things:
    VB Code:
    1. [B]Private Const EM_LINEINDEX = &HBB[/B]
    2.  
    3. Private Sub RichTextBox1_Click()
    4.  
    5.  
    6.     Dim strBuffer As String
    7.     Dim lngLength As Long
    8.     Dim intCurrentLine As Integer
    9.     Dim lngLineNumber As Long
    10.     [B]Dim lngLineIndex As Long[/B]
    11.  
    12.         With RichTextBox1
    13.         intCurrentLine = .GetLineFromChar(.SelStart)
    14.         [B]lngLineIndex = SendMessage(.hwnd, EM_LINEINDEX, intCurrentLine, 0)[/B]
    15.         'get line length
    16.         lngLength = SendMessage(.hwnd, EM_LINELENGTH, [B]lngLineIndex[/B], 0)
    17.         'resize buffer
    18.         strBuffer = Space(lngLength)
    19.         'get line text
    20.         Call SendMessage(.hwnd, EM_GETLINE, intCurrentLine, ByVal strBuffer)
    21.        
    22.         lngLineNumber = .GetLineFromChar(.SelStart)
    23.         MsgBox "You selected line number " & lngLineNumber + 1 & " which says " & strBuffer
    24.     End With
    25. End Sub
    I had to add the bold parts to your code to get the whole line...

    Best Regards,
    ERAN
    Eran Fox
    ASSEMBLER,C,C++,VB6,SQL...

  15. #15
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Colouring RTF Boxes

    My code ran fine for me. I just tested it again. It selects the whole line in the rtb when you click on a line, and the replace works on all instances of a phrase.

  16. #16

    Thread Starter
    New Member
    Join Date
    Nov 2005
    Posts
    9

    Re: Colouring RTF Boxes

    Okay I'm sorry to keep doing this and I thank you guys for being so helpful but I've been messing with this for a couple days or so and I can't seem to get it to work right. Its for a script editor for a game I play if anyone is wondering I want it to change it as its being typed out not just when a button is hit and thats where I run into problems.

    Example for script when done again if anyone is wondering:
    "When someone moves over here"
    "Set variable /variablename\ to 3"

    Also should be red for /v, /va, /var etc. etc. though Its giving me LOADS of headaches all help is appreciated

  17. #17
    Banned dglienna's Avatar
    Join Date
    Jun 2004
    Location
    Center of it all
    Posts
    17,901

    Re: Colouring RTF Boxes

    I think you could use the keypress event of a TEXTBOX that colorizes words and then puts them into an RTB, but it is likely to be kind of slow.

  18. #18

    Thread Starter
    New Member
    Join Date
    Nov 2005
    Posts
    9

    Re: Colouring RTF Boxes

    What do you suggest as I do for that codewise?

  19. #19

    Thread Starter
    New Member
    Join Date
    Nov 2005
    Posts
    9

    Talking Re: Colouring RTF Boxes

    Okay I have had to put down this project due to school but have picked it up again. I have fiddled with it and come up with this so far..
    VB Code:
    1. Private Sub rtfMain_Change()
    2.          Call SyntaxHighlight
    3. End Sub
    4.  
    5. Private Function SyntaxHighlight()
    6. Dim lngHighlight As Long
    7. Dim strVariableStart As String
    8. Dim lngLength As Long
    9. Dim strSpace As String
    10. Dim lngEndHighlight As Long
    11.  
    12. strVariableStart = "/"
    13. strSpace = " "
    14. lngHighlight = (rtfMain.Find(strVariableStart) + 1)
    15.  
    16. lngEndHighlight = rtfMain.Find(strSpace, lngHighlight)
    17.  
    18. lngLength = (lngHighlight - lngEndHighlight)
    19.  
    20. rtfMain.SelStart = lngHighlight
    21. rtfMain.SelLength = lngLength
    22. rtfMain.SelColor = vbRed
    23. rtfMain.SelStart = Len(rtfMain.Text)
    24. rtfMain.SelLength = 0
    25. End Function

    This actually works for the colouring part but when I press space to continue on with the text it gives me an error I go into debugging and it says that lngLength is actually the negative of whatever it should be. So I tried multiplying it by -1 but that didn't work, any suggestions?

  20. #20
    New Member
    Join Date
    Dec 2005
    Posts
    1

    Re: Colouring RTF Boxes

    hello, i m using rtf control in my program vb6. i want its backcolor transparent like label. plz tell me how is it possible

  21. #21
    Hyperactive Member eranfox's Avatar
    Join Date
    May 2001
    Posts
    492

    Re: Colouring RTF Boxes

    Hello waqqas,
    Welcome to VBForums

    I think you had to open a new Thread for you question but never mind that...

    How about this?
    set the rtb BackColor to &H8000000F&

    if you want to do it on run time just put this line on your Form_Load event:
    VB Code:
    1. Private Sub Form_Load()
    2.     RichTextBox1.BackColor = &H8000000F
    3. End Sub

    Or use this :
    VB Code:
    1. Private Sub Form_Load()
    2.     RichTextBox1.BackColor = Label1.BackColor
    3. End Sub

    Edit:
    you can just use this:
    VB Code:
    1. Private Sub Form_Load()
    2.     RichTextBox1.BackColor = Form1.BackColor
    3. End Sub

    If you want the rtb to look exactly like a label you can do this:
    VB Code:
    1. Private Sub Form_Load()
    2.     RichTextBox1.BackColor = Form1.BackColor
    3.     RichTextBox1.Appearance = rtfFlat
    4.     RichTextBox1.BorderStyle = rtfNoBorder
    5. End Sub


    Best Regards,
    ERAN
    Last edited by eranfox; Dec 24th, 2005 at 06:32 AM.
    Eran Fox
    ASSEMBLER,C,C++,VB6,SQL...

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