Hi, I am making a richtextbox to html convertor, I have added features like bold italic underline left align right align center align etc. I found a code to convert richtextbox to html but i am not sure how to use that function.

Public Shared Function FromRtf(ByVal rtf As RichTextBox) As String
        Dim b, i, u As Boolean
        b = False : i = False : u = False
        Dim fontfamily As String = "Arial"
        Dim fontsize As Integer = 12
        Dim htmlstr As String = String.Format("<html>{0}<body>{0}<div style=""text-align: left;""><span style=""font-family: Arial; font-size: 12pt;"">", vbCrLf)
        Dim x As Integer = 0
        While x < rtf.Text.Length
            rtf.Select(x, 1)
            If rtf.SelectionFont.Bold AndAlso (Not b) Then
                htmlstr &= "<b>"
                b = True
            ElseIf (Not rtf.SelectionFont.Bold) AndAlso b Then
                htmlstr &= "</b>"
                b = False
            End If
            If rtf.SelectionFont.Italic AndAlso (Not i) Then
                htmlstr &= "<i>"
                i = True
            ElseIf (Not rtf.SelectionFont.Italic) AndAlso i Then
                htmlstr &= "</i>"
                i = False
            End If
            If rtf.SelectionFont.Underline AndAlso (Not u) Then
                htmlstr &= "<u>"
                u = True
            ElseIf (Not rtf.SelectionFont.Underline) AndAlso u Then
                htmlstr &= "</u>"
                u = False
            End If
            If fontfamily <> rtf.SelectionFont.FontFamily.Name Then
                htmlstr &= String.Format("</span><span style=""font-family: {0}; font-size: {0}pt;"">", rtf.SelectionFont.FontFamily.Name, fontsize)
                fontfamily = rtf.SelectionFont.FontFamily.Name
            End If
            If fontsize <> rtf.SelectionFont.SizeInPoints Then
                htmlstr &= String.Format("</span><span style=""font-family: {0}; font-size: {0}pt;"">", fontfamily, rtf.SelectionFont.SizeInPoints)
                fontsize = rtf.SelectionFont.SizeInPoints
            End If
            Dim curchar As String = rtf.SelectedText
            Select Case curchar
                Case vbCr, vbLf : curchar = "<br />"
                Case "&" : curchar = "&amp;" : x += "&amp;".Length - 1
                Case "<" : curchar = "&lt;" : x += "&lt;".Length - 1
                Case ">" : curchar = "&gt;" : x += "&gt;".Length - 1
                Case " " : curchar = "&nbsp;" : x += "&nbsp;".Length - 1
            End Select
            rtf.SelectedText = curchar
            x += 1
        End While
        Return htmlstr & String.Format("</span>{0}</body>{0}</html>", vbCrLf)
    End Function

Where do I call this function? How do use this function? Please help,its urgent!

You can copy and paste this code into the form class but outside of any sub or function.

To call it you could try something like this:

Dim HDoc as New HtmlDocument
Hdoc.Write(FromRtf(RichTextBox1))

I want to do something like this:
Take the text of the richtextbox, pass it to the function which recognizes where the text is bold,italic etc and inserts all the required tags and returns the convertedtext in the form of a variable.
Can it be done?

Public Class mycnverterclass


#Region "Private Members"

    ' A RichTextBox control to use to help with parsing.
    Private rtb As New System.Windows.Forms.RichTextBox



#End Region

#Region "Read/Write Properties"

    ''' <summary>
    ''' Returns/Sets The RTF formatted text to parse
    ''' </summary>
    Public Property rtf() As String
        Get
            Return rtb.Rtf
        End Get
        Set(ByVal value As String)
            rtb.Rtf = value
        End Set
    End Property

#End Region

#Region "ReadOnly Properties"

    ''' <summary>
    ''' Returns the HTML code for the provided RTF
    ''' </summary>
    Public ReadOnly Property html() As String
        Get
            Return GetHtml()
        End Get
    End Property

#End Region

#Region "Private Functions"

    ''' <summary>
    ''' Returns an HTML Formated Color string for the style from a system.drawing.color
    ''' </summary>
    ''' <param name="clr">The color you wish to convert</param>
    Private Function HtmlColorFromColor(ByRef clr As System.Drawing.Color) As String
        Dim strReturn As String = ""
        If clr.IsNamedColor Then
            strReturn = clr.Name.ToLower
        Else
            strReturn = clr.Name
            If strReturn.Length > 6 Then
                strReturn = strReturn.Substring(strReturn.Length - 6, 6)
            End If
            strReturn = "#" & strReturn
        End If
        Return strReturn
    End Function

    ''' <summary>
    ''' Provides the font style per given font
    ''' </summary>
    ''' <param name="fnt">The font you wish to convert</param>
    Private Function HtmlFontStyleFromFont(ByRef fnt As System.Drawing.Font) As String
        Dim strReturn As String = ""
        'style
        If fnt.Italic Then
            strReturn &= "italic "
        Else
            strReturn &= "normal "
        End If
        'variant
        strReturn &= "normal "
        'weight
        If fnt.Bold Then
            strReturn &= "bold "
        Else
            strReturn &= "normal "
        End If
        'size
        strReturn &= fnt.SizeInPoints & "pt/normal "
        'family
        strReturn &= fnt.FontFamily.Name
        Return strReturn
    End Function

    ''' <summary>
    ''' Parses the given rich text and returns the html.
    ''' </summary>
    Public Function GetHtml() As String
        Dim strReturn As String = "<div>"
        Dim clrForeColor As System.Drawing.Color = Color.Black
        Dim clrBackColor As System.Drawing.Color = Color.Black
        Dim fntCurrentFont As System.Drawing.Font = rtb.Font
        Dim altCurrent As System.Windows.Forms.HorizontalAlignment = HorizontalAlignment.Left
        Dim intPos As Integer = 0
        For intPos = 0 To rtb.Text.Length - 1
            rtb.Select(intPos, 1)
            'Forecolor
            If intPos = 0 Then
                strReturn &= "<span style=""color:" & HtmlColorFromColor(rtb.SelectionColor) & """>"
                clrForeColor = rtb.SelectionColor
            Else
                If rtb.SelectionColor <> clrForeColor Then
                    strReturn &= "</span>"
                    strReturn &= "<span style=""color:" & HtmlColorFromColor(rtb.SelectionColor) & """>"
                    clrForeColor = rtb.SelectionColor
                End If
            End If
            'Background color
            If intPos = 0 Then
                strReturn &= "<span style=""background-color:" & HtmlColorFromColor(rtb.SelectionBackColor) & """>"
                clrBackColor = rtb.SelectionBackColor
            Else
                If rtb.SelectionBackColor <> clrBackColor Then
                    strReturn &= "</span>"
                    strReturn &= "<span style=""background-color:" & HtmlColorFromColor(rtb.SelectionBackColor) & """>"
                    clrBackColor = rtb.SelectionBackColor
                End If
            End If
            'Font
            If intPos = 0 Then
                strReturn &= "<span style=""font:" & HtmlFontStyleFromFont(rtb.SelectionFont) & """>"
                fntCurrentFont = rtb.SelectionFont
            Else
                If rtb.SelectionFont.GetHashCode <> fntCurrentFont.GetHashCode Then
                    strReturn &= "</span>"
                    strReturn &= "<span style=""font:" & HtmlFontStyleFromFont(rtb.SelectionFont) & """>"
                    fntCurrentFont = rtb.SelectionFont
                End If
            End If
            'Alignment
            If intPos = 0 Then
                strReturn &= "<p style=""text-align:" & rtb.SelectionAlignment.ToString & """>"
                altCurrent = rtb.SelectionAlignment
            Else
                If rtb.SelectionAlignment <> altCurrent Then
                    strReturn &= "</p>"
                    strReturn &= "<p style=""text-align:" & rtb.SelectionAlignment.ToString & """>"
                    altCurrent = rtb.SelectionAlignment
                End If
            End If
            strReturn &= rtb.Text.Substring(intPos, 1)
        Next
        'close all the spans
        strReturn &= "</span>"
        strReturn &= "</span>"
        strReturn &= "</span>"
        strReturn &= "</p>"
        strReturn &= "</div>"
        strReturn = strReturn.Replace(Convert.ToChar(10), "<br />")
        Return strReturn
    End Function

#End Region


End Class

how do i use this class?

If you change your class slightly like this:

Public Class myconverterclass
#Region "Private Members"
    ' A RichTextBox control to use to help with parsing.
    Private _rtb As New System.Windows.Forms.RichTextBox
#End Region
    Public Sub New(ByVal rtb As RichTextBox)
        _rtb = rtb
    End Sub

#Region "Read/Write Properties"
    ''' <summary>
    ''' Returns/Sets The RTF formatted text to parse
    ''' </summary>
    Public Property rtf() As String
        Get
            Return _rtb.Rtf
        End Get
        Set(ByVal value As String)
            _rtb.Rtf = value
        End Set
    End Property
#End Region
#Region "ReadOnly Properties"
    ''' <summary>
    ''' Returns the HTML code for the provided RTF
    ''' </summary>
    Public ReadOnly Property html() As String
        Get
            Return GetHtml()
        End Get
    End Property
#End Region
#Region "Private Functions"
    ''' <summary>
    ''' Returns an HTML Formated Color string for the style from a system.drawing.color
    ''' </summary>
    ''' <param name="clr">The color you wish to convert</param>
    Private Function HtmlColorFromColor(ByRef clr As System.Drawing.Color) As String
        Dim strReturn As String = ""
        If clr.IsNamedColor Then
            strReturn = clr.Name.ToLower
        Else
            strReturn = clr.Name
            If strReturn.Length > 6 Then
                strReturn = strReturn.Substring(strReturn.Length - 6, 6)
            End If
            strReturn = "#" & strReturn
        End If
        Return strReturn
    End Function
    ''' <summary>
    ''' Provides the font style per given font
    ''' </summary>
    ''' <param name="fnt">The font you wish to convert</param>
    Private Function HtmlFontStyleFromFont(ByRef fnt As System.Drawing.Font) As String
        Dim strReturn As String = ""
        'style
        If fnt.Italic Then
            strReturn &= "italic "
        Else
            strReturn &= "normal "
        End If
        'variant
        strReturn &= "normal "
        'weight
        If fnt.Bold Then
            strReturn &= "bold "
        Else
            strReturn &= "normal "
        End If
        'size
        strReturn &= fnt.SizeInPoints & "pt/normal "
        'family
        strReturn &= fnt.FontFamily.Name
        Return strReturn
    End Function
    ''' <summary>
    ''' Parses the given rich text and returns the html.
    ''' </summary>
    Public Function GetHtml() As String
        Dim strReturn As String = "<div>"
        Dim clrForeColor As System.Drawing.Color = Color.Black
        Dim clrBackColor As System.Drawing.Color = Color.Black
        Dim fntCurrentFont As System.Drawing.Font = _rtb.Font
        Dim altCurrent As System.Windows.Forms.HorizontalAlignment = HorizontalAlignment.Left
        Dim intPos As Integer = 0
        For intPos = 0 To _rtb.Text.Length - 1
            _rtb.Select(intPos, 1)
            'Forecolor
            If intPos = 0 Then
                strReturn &= "<span style=""color:" & HtmlColorFromColor(_rtb.SelectionColor) & """>"
                clrForeColor = _rtb.SelectionColor
            Else
                If _rtb.SelectionColor <> clrForeColor Then
                    strReturn &= "</span>"
                    strReturn &= "<span style=""color:" & HtmlColorFromColor(_rtb.SelectionColor) & """>"
                    clrForeColor = _rtb.SelectionColor
                End If
            End If
            'Background color
            If intPos = 0 Then
                strReturn &= "<span style=""background-color:" & HtmlColorFromColor(_rtb.SelectionBackColor) & """>"
                clrBackColor = _rtb.SelectionBackColor
            Else
                If _rtb.SelectionBackColor <> clrBackColor Then
                    strReturn &= "</span>"
                    strReturn &= "<span style=""background-color:" & HtmlColorFromColor(_rtb.SelectionBackColor) & """>"
                    clrBackColor = _rtb.SelectionBackColor
                End If
            End If
            'Font
            If intPos = 0 Then
                strReturn &= "<span style=""font:" & HtmlFontStyleFromFont(_rtb.SelectionFont) & """>"
                fntCurrentFont = _rtb.SelectionFont
            Else
                If _rtb.SelectionFont.GetHashCode <> fntCurrentFont.GetHashCode Then
                    strReturn &= "</span>"
                    strReturn &= "<span style=""font:" & HtmlFontStyleFromFont(_rtb.SelectionFont) & """>"
                    fntCurrentFont = _rtb.SelectionFont
                End If
            End If
            'Alignment
            If intPos = 0 Then
                strReturn &= "<p style=""text-align:" & _rtb.SelectionAlignment.ToString & """>"
                altCurrent = _rtb.SelectionAlignment
            Else
                If _rtb.SelectionAlignment <> altCurrent Then
                    strReturn &= "</p>"
                    strReturn &= "<p style=""text-align:" & _rtb.SelectionAlignment.ToString & """>"
                    altCurrent = _rtb.SelectionAlignment
                End If
            End If
            strReturn &= _rtb.Text.Substring(intPos, 1)
        Next
        'close all the spans
        strReturn &= "</span>"
        strReturn &= "</span>"
        strReturn &= "</span>"
        strReturn &= "</p>"
        strReturn &= "</div>"
        strReturn = strReturn.Replace(Convert.ToChar(10), "<br />")
        Return strReturn
    End Function
#End Region
End Class

The main thing missing was a new constructor(Public Sub New(ByVal rtb As RichTextBox))

You would use it like this:

Dim NewConverter As New myconverterclass(RichTextBox1)

and any public variables or methods will be available to NewConverter.

Dim HtmlString as String = NewConverter.GetHtml()

Edited 3 Years Ago by tinstaafl

This question has already been answered. Start a new discussion instead.