Results 1 to 5 of 5

Thread: [RESOLVED] RichTextBox and Tags

  1. #1

    Thread Starter
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Resolved [RESOLVED] RichTextBox and Tags

    I am attempting to format certain text in a Richtextbox by using Tags. My problem is that when I attempt to remove the tags using the Replace Function I lose all of the prior formatting. Here is my code:

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Command1_Click()
    4.     Call TextBold(RichTextBox1)
    5.     Call TextColor(RichTextBox1, vbRed)
    6.     Call TextCenter(RichTextBox1)
    7.     Call TextRight(RichTextBox1)
    8.     Call TextItalics(RichTextBox1)
    9. End Sub
    10.  
    11. Private Sub Command2_Click()
    12.     'When I click on the button I lose all formatting
    13.     With RichTextBox1
    14.         .Refresh
    15.         .Text = Replace(.Text, "<", "")
    16.         .Text = Replace(.Text, ">", "")
    17.         .Text = Replace(.Text, "(", "")
    18.         .Text = Replace(.Text, ")", "")
    19.         .Text = Replace(.Text, "{", "")
    20.         .Text = Replace(.Text, "}", "")
    21.         .Refresh
    22.     End With
    23. End Sub
    24.  
    25. Private Sub Form_Load()
    26.     With RichTextBox1
    27.         .Text = "<1>" & vbNewLine 'Color these Characters Blue
    28.         .Text = .Text & "<(5-6-58)>" & vbNewLine 'Bold These Characters & Center Align
    29.         .Text = .Text & "{$1-23-88%}" & vbNewLine 'Italicizes These Characters & Right Align
    30.     End With
    31. End Sub
    32.  
    33. Private Sub TextBold(RTB As RichTextBox)
    34. Dim lngTag_Start As Long
    35. Dim lngTag_End As Integer
    36. Dim strText As String
    37.  
    38. lngTag_End = 1
    39.  
    40. With RTB
    41.     strText = .Text
    42.    
    43.     Do While InStr(lngTag_End, strText, "<") > 0
    44.         .SelLength = 0
    45.         lngTag_Start = InStr(lngTag_End, strText, "<")
    46.         lngTag_End = InStr(lngTag_Start, strText, ">")
    47.         .SelStart = lngTag_Start
    48.         .SelLength = (lngTag_End - lngTag_Start) - 1
    49.         .SelBold = True
    50.         lngTag_End = lngTag_End + 1
    51.     Loop
    52. End With
    53.  
    54. End Sub
    55.  
    56. Private Sub TextCenter(RTB As RichTextBox)
    57. Dim lngTag_Start As Long
    58. Dim lngTag_End As Integer
    59. Dim strText As String
    60.  
    61. lngTag_End = 1
    62.  
    63. With RTB
    64.     strText = .Text
    65.    
    66.     Do While InStr(lngTag_End, strText, "(") > 0
    67.         .SelLength = 0
    68.         lngTag_Start = InStr(lngTag_End, strText, "(")
    69.         lngTag_End = InStr(lngTag_Start, strText, ")")
    70.         .SelStart = lngTag_Start
    71.         .SelLength = (lngTag_End - lngTag_Start) - 1
    72.         .SelAlignment = rtfCenter
    73.         lngTag_End = lngTag_End + 1
    74.     Loop
    75. End With
    76.  
    77. End Sub
    78.  
    79. Private Sub TextColor(RTB As RichTextBox, lngColor As Long)
    80. Dim lngTag_Start As Long
    81. Dim lngTag_End As Integer
    82. Dim strText As String
    83.  
    84. lngTag_End = 1
    85.  
    86. With RTB
    87.     strText = .Text
    88.    
    89.     Do While InStr(lngTag_End, strText, "<") > 0
    90.         .SelLength = 0
    91.         lngTag_Start = InStr(lngTag_End, strText, "<")
    92.         lngTag_End = InStr(lngTag_Start, strText, ">")
    93.         .SelStart = lngTag_Start
    94.         .SelLength = (lngTag_End - lngTag_Start) - 1
    95.         .SelColor = lngColor
    96.         lngTag_End = lngTag_End + 1
    97.     Loop
    98. End With
    99.  
    100. End Sub
    101.  
    102. Private Sub TextRight(RTB As RichTextBox)
    103. Dim lngTag_Start As Long
    104. Dim lngTag_End As Integer
    105. Dim strText As String
    106.  
    107. lngTag_End = 1
    108.  
    109. With RTB
    110.     strText = .Text
    111.    
    112.     Do While InStr(lngTag_End, strText, "$") > 0
    113.         .SelLength = 0
    114.         lngTag_Start = InStr(lngTag_End, strText, "$")
    115.         lngTag_End = InStr(lngTag_Start, strText, "%")
    116.         .SelStart = lngTag_Start
    117.         .SelLength = (lngTag_End - lngTag_Start) - 1
    118.         .SelAlignment = rtfRight
    119.         lngTag_End = lngTag_End + 1
    120.     Loop
    121. End With
    122.  
    123. End Sub
    124.  
    125. Private Sub TextItalics(RTB As RichTextBox)
    126. Dim lngTag_Start As Long
    127. Dim lngTag_End As Integer
    128. Dim strText As String
    129.  
    130. lngTag_End = 1
    131.  
    132. With RTB
    133.     strText = .Text
    134.    
    135.     Do While InStr(lngTag_End, strText, "{") > 0
    136.         .SelLength = 0
    137.         lngTag_Start = InStr(lngTag_End, strText, "{")
    138.         lngTag_End = InStr(lngTag_Start, strText, "}")
    139.         .SelStart = lngTag_Start
    140.         .SelLength = (lngTag_End - lngTag_Start) - 1
    141.         .SelItalic = True
    142.         lngTag_End = lngTag_End + 1
    143.     Loop
    144. End With
    145.  
    146. End Sub
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


  2. #2
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: RichTextBox and Tags

    using replace "resets" it not sure why.. this works (although not "pretty")

    VB Code:
    1. Private Sub Command2_Click()
    2.     'When I click on the button I lose all formatting
    3.     With RichTextBox1
    4.         Do While .Find("<", 0) <> -1
    5.             .SelText = ""
    6.         Loop
    7.         Do While .Find(">", 0) <> -1
    8.             .SelText = ""
    9.         Loop
    10.         Do While .Find("(", 0) <> -1
    11.             .SelText = ""
    12.         Loop
    13.         Do While .Find(")", 0) <> -1
    14.             .SelText = ""
    15.         Loop
    16.         Do While .Find("{", 0) <> -1
    17.             .SelText = ""
    18.         Loop
    19.         Do While .Find("}", 0) <> -1
    20.             .SelText = ""
    21.         Loop
    22.     End With
    23. End Sub
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  3. #3

    Thread Starter
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Re: RichTextBox and Tags

    I though about doing what you posted but I thought I was doing something wrong. Thanks!
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


  4. #4

    Thread Starter
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Re: RichTextBox and Tags

    Well after hours of tweeking here is my completed project. I know that numerous other features can be added to this project but it does exactly what I need it to do:

    VB Code:
    1. Option Explicit
    2.  
    3. Private Sub Command1_Click()
    4.      Call TextFormat(RichTextBox1)
    5. End Sub
    6.  
    7. Private Sub Form_Load()
    8.     With RichTextBox1
    9.         .Text = "<Color=255><Right>1<\Right><\Color>" & vbNewLine 'Color these Characters Blue
    10.         .Text = .Text & "<B><Center>5-6-58<\Center><\B>" & vbNewLine 'Bold These Characters & Center Align
    11.         .Text = .Text & "<I><Left>1-23-88<\Left><\I>" & vbNewLine 'Italicizes These Characters & Right Align
    12.     End With
    13. End Sub
    14.  
    15. Private Sub TextFormat(RTB As RichTextBox)
    16. Dim lngTag_Start As Long
    17. Dim lngTag_End As Integer
    18. Dim strTag_Start As String
    19. Dim strTag_End As String
    20.  
    21. Dim strText As String
    22. Dim a As Long
    23.  
    24. For a = 1 To 10
    25.     strTag_Start = "<" & Choose(a, "B", "I", "U", "Center", "Left", _
    26.                                     "Right", "Color=255", "Color=16711680", _
    27.                                     "Color=65280", "Color=65535") & ">"
    28.     If a = 7 Then
    29.         a = a
    30.     End If
    31.    
    32.     strTag_End = "<\" & Choose(a, "B", "I", "U", "Center", "Left", _
    33.                                     "Right", "Color", "Color", _
    34.                                     "Color", "Color") & ">"
    35.    
    36.     lngTag_End = 1
    37.  
    38.     With RTB
    39.         strText = .Text
    40.        
    41.         Do While InStr(lngTag_End, strText, strTag_Start) > 0
    42.             .SelLength = 0
    43.             lngTag_Start = InStr(lngTag_End, strText, strTag_Start)
    44.             lngTag_End = InStr(lngTag_Start, strText, strTag_End)
    45.             .SelStart = lngTag_Start
    46.             .SelLength = (lngTag_End - lngTag_Start) - 1
    47.            
    48.             Select Case strTag_Start
    49.                 Case "<B>"
    50.                     .SelBold = True
    51.                 Case "<I>"
    52.                     .SelItalic = True
    53.                 Case "<U>"
    54.                     .SelUnderline = True
    55.                 Case "<Center>"
    56.                     .SelAlignment = rtfCenter
    57.                 Case "<Left>"
    58.                     .SelAlignment = rtfLeft
    59.                 Case "<Right>"
    60.                     .SelAlignment = rtfRight
    61.                 Case Else
    62.                     If Left(strTag_Start, 6) = "<Color" Then
    63.                        
    64.                         .SelColor = CLng(Mid(strTag_Start, 8, Len(strTag_Start) - 8))
    65.                     End If
    66.             End Select
    67.        
    68.             lngTag_End = lngTag_End + 1
    69.        
    70.         Loop
    71.     End With
    72.    
    73.     With RTB
    74.         Do While .Find(strTag_Start, 0) <> -1
    75.                 .SelText = ""
    76.         Loop
    77.        
    78.         Do While .Find(strTag_End, 0) <> -1
    79.             .SelText = ""
    80.         Loop
    81.     End With
    82. Next a
    83. End Sub
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


  5. #5

    Thread Starter
    Giants World Champs!!!! Mark Gambo's Avatar
    Join Date
    Sep 2003
    Location
    Colorado
    Posts
    2,965

    Re: [RESOLVED] RichTextBox and Tags

    Here is an updated version of my code that I posterd in Post # 4 Converting Text to a Long Value
    Regards,

    Mark

    Please remember to rate posts! Rate any post you find helpful. Use the link to the left - "Rate this Post". Please use [highlight='vb'] your code goes in here [/highlight] tags when posting code. When a question you asked has been resolved, please go to the top of the original post and click "Thread Tools" then select "Mark Thread Resolved."


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