Results 1 to 13 of 13

Thread: VB - RichTextBox to HTML

Threaded View

  1. #1

    Thread Starter
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    VB - RichTextBox to HTML

    been playing around with this:
    seems to work ok, but wondering if it could be done faster
    1 RichTextBox (RTB1)
    1 Webbrowser Control (Default Name: Webbrowser1)
    1 command btn (cmdCreate)

    Paste Colored * Text into the RTB

    (*this is designed to change VB code to HTML for display.. so there is other code that colors it correctly first.... it will not pick up bold/font/etc.. just color...)

    VB Code:
    1. Dim txtHTML As String
    2.  
    3. 'Convert to HTML Color
    4. Private Function GetHTMLColor(lColor As Long) As String
    5.  
    6.     Dim sColor As String
    7.     Dim sR As String
    8.     Dim sG As String
    9.     Dim sB As String
    10.    
    11.     sColor = Right(String(6, "0") & Hex(lColor), 6)
    12.    
    13.     sR = Mid(sColor, 1, 2)
    14.     sG = Mid(sColor, 3, 2)
    15.     sB = Mid(sColor, 5, 2)
    16.     GetHTMLColor = "#" & sB & sG & sR
    17.  
    18. End Function
    19.  
    20.  
    21. 'Code to convert
    22. Private Sub cmdCreate_Click()
    23.     Dim clr As String
    24.     RTB1.SelStart = 0
    25.     clr = GetHTMLColor(RTB1.SelColor)
    26.     txtHTML = "<PRE><font face=" & Chr(34) & "courier new" & Chr(34) & " size=2><font color=" & clr & ">"
    27.     For x = 1 To Len(RTB1.Text)
    28.         RTB1.SelStart = x
    29.         If GetHTMLColor(RTB1.SelColor) <> clr And Mid(RTB1.Text, x, 1) <> " " Then
    30.             clr = GetHTMLColor(RTB1.SelColor)
    31.             txtHTML = txtHTML & "</font><font color=" & clr & ">"
    32.         End If
    33.         Select Case Mid(RTB1.Text, x, 1)
    34.             Case "<"
    35.                 txtHTML = txtHTML & "&lt"
    36.             Case ">"
    37.                 txtHTML = txtHTML & "&gt"
    38.             Case "&"
    39.                 txtHTML = txtHTML & "&amp;"
    40.             Case Else
    41.                 txtHTML = txtHTML & Mid(RTB1.Text, x, 1)
    42.         End Select
    43.        
    44.     Next
    45.     txtHTML = txtHTML & "</font></font></PRE>"
    46.     WebBrowser1.Navigate "about:blank"
    47. End Sub
    48.  
    49.  
    50. 'To Display it to the user
    51. Private Sub WebBrowser1_DocumentComplete(ByVal pDisp As Object, URL As Variant)
    52.     If (pDisp Is WebBrowser1.Application) Then
    53.         WebBrowser1.Document.write txtHTML
    54.    
    55.     End If
    56. End Sub
    Last edited by Static; Jun 21st, 2006 at 03:19 PM. Reason: Updated code... ;)
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "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