Turn your Mobile Apps into m-commerce apps – Learn More!

You're Browsing As A Guest! Register Now...
Become a VB.NET Expert!

Join 415,716 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 774 people online right now.Registration is fast and FREE... Join Now!



Page 1 of 1
  • You cannot start a new topic
  • Reply Reply

Convert a rich text into html conversion I going to send a mail with a formatted text in richtext box, in that Rate Topic: -----

#1 gmbalaa  Icon User is offline

  • New D.I.C Head
  • Icon

Reputation: 2
  • View blog
  • Posts: 24
  • Joined: 09-August 07


Dream Kudos: 25

Share |

Convert a rich text into html conversion

Post icon  Posted 09 August 2007 - 10:11 PM

Private Sub TSB_Send_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TSB_Send.Click
		Me.Status.Items.Clear()
		Dim strHTML As String
		If Me.RTE_Body.Text = Nothing Then
			Me.RichTextBox1.Text = ""
		Else
			strHTML = ConvertToHTML(RTE_Body)
			RichTextBox1.Text = strHTML.ToString
		End If
		'Call send_mail()
		Dim t1 As Thread = Nothing
		t1 = New Thread(AddressOf send_mail)
		t1.Start()
End Sub
---------------------
Public Function ConvertToHTML(ByVal Box As RichTextBox) As String
		' Takes a RichTextBox control and returns a
		' simple HTML-formatted version of its contents
		Dim strHTML As String
		Dim strColour As String
		Dim blnBold As Boolean
		Dim blnItalic As Boolean
		Dim strFont As String
		Dim shtSize As Short
		Dim lngOriginalStart As Long
		Dim lngOriginalLength As Long
		Dim intCount As Integer
		' If nothing in the box, exit
		If Box.Text.Length = 0 Then
			Exit Function
		End If
		' Store original selections, then select first character
		lngOriginalStart = 0
		lngOriginalLength = Box.TextLength
		Box.Select(0, 1)
		' Add HTML header
		strHTML = "<html>"
		' Set up initial parameters
		strColour = Box.SelectionColor.ToKnownColor.ToString
		blnBold = Box.SelectionFont.Bold
		blnItalic = Box.SelectionFont.Italic
		strFont = Box.SelectionFont.FontFamily.Name
		shtSize = Box.SelectionFont.Size
		' Include first 'style' parameters in the HTML
		strHTML += "<span style=""font-family: " & strFont & _
		  "; font-size: " & shtSize & "pt; color: " _
						  & strColour & """>"
		' Include bold tag, if required
		If blnBold = True Then
			strHTML += "<b>"
		End If
		' Include italic tag, if required
		If blnItalic = True Then
			strHTML += "<i>"
		End If
		' Finally, add our first character
		strHTML += Box.Text.Substring(0, 1)
		' Loop around all remaining characters
		For intCount = 2 To Box.Text.Length
			' Select current character
			Box.Select(intCount - 1, 1)
			' If this is a line break, add HTML tag
			If Box.Text.Substring(intCount - 1, 1) = _
				   Convert.ToChar(10) Then
				strHTML += "<br>"
			End If
			' Check/implement any changes in style
			If Box.SelectionColor.ToKnownColor.ToString <> _
			   strColour Or Box.SelectionFont.FontFamily.Name _
			   <> strFont Or Box.SelectionFont.Size <> shtSize _
			   Then
				strHTML += "</span><span style=""font-family: " _
				  & Box.SelectionFont.FontFamily.Name & _
				  "; font-size: " & Box.SelectionFont.Size & _
				  "pt; color: " & _
				  Box.SelectionColor.ToKnownColor.ToString & """>"
			End If
			' Check for bold changes
			If Box.SelectionFont.Bold <> blnBold Then
				If Box.SelectionFont.Bold = False Then
					strHTML += "</b>"
				Else
					strHTML += "<b>"
				End If
			End If
			' Check for italic changes
			If Box.SelectionFont.Italic <> blnItalic Then
				If Box.SelectionFont.Italic = False Then
					strHTML += "</i>"
				Else
					strHTML += "<i>"
				End If
			End If
			' Add the actual character
			strHTML += Mid(Box.Text, intCount, 1)
			' Update variables with current style
			strColour = Box.SelectionColor.ToKnownColor.ToString
			blnBold = Box.SelectionFont.Bold
			blnItalic = Box.SelectionFont.Italic
			strFont = Box.SelectionFont.FontFamily.Name
			shtSize = Box.SelectionFont.Size
		Next
		' Close off any open bold/italic tags
		If blnBold = True Then strHTML += ""
		If blnItalic = True Then strHTML += ""
		' Terminate outstanding HTML tags
		strHTML += "</span></html>"
		' Restore original RichTextBox selection
		Box.Select(lngOriginalStart, lngOriginalLength)
		' Return HTML
		Return strHTML
	End Function


MOD EDIT: added [code] tags ~ jayman9
Was This Post Helpful? 0
  • +
  • -


#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • Icon

Reputation: 1398
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07


Dream Kudos: 12950

Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net, jQuery

Re: Convert a rich text into html conversion

Posted 10 August 2007 - 01:24 AM

First -> You need to put your code in code blocks (see below image for button location), this makes it much easier to read

Attached Image

Second -> You never say if you're getting any errors (if so what is the error), you never say what isnt working right, what the output is or anything. If you are having an issue I cannot help if I don't know what it is.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1
  • You cannot start a new topic
  • Reply Reply


Fast Reply

  

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users