ShaneEP
(MM club member)
2011-05-06 08:18 PM
Kixforms RichTextBox

Does anyone have any experience with the richtextbox control in kixforms? I'm trying to use it to generate a formatted report type document. Not sure how to set the formatting of the text line by line, or if it's even possible.

JochenAdministrator
(KiX Supporter)
2011-05-06 09:33 PM
Re: Kixforms RichTextBox

Classic or .net ?

ShaneEP
(MM club member)
2011-05-06 09:41 PM
Re: Kixforms RichTextBox

Classic. I found that you can use HTML in the richtextbox which would work...except that it doesnt recognize < pre > tags or & nbsp so it's becoming difficult to get columns to line up lol.

ShaneEP
(MM club member)
2011-05-06 09:46 PM
Re: Kixforms RichTextBox

Also tried the < table > route but its not recognized either.

It_took_my_meds
(Hey THIS is FUN)
2011-05-07 12:48 AM
Re: Kixforms RichTextBox

You can do it. You just need to select the text you want with .SelectionStart and .SelectionLength. Then you can format the selection with properties such as .SelectionFontUnderline, .SelectionFontBold, etc, or even .SelectedText.

Here's some example code that adjusts a RichTextBox so that pasted HTML or a typed URL is formatted correctly. (I use this as part of an email body editor that allows people to create scheduled mail merges with information from a database. It is triggered by the OnKeyDown event or pressing a paste button.)

 Code:
Function FixURL(Optional $txtBody)

	; This regex fixes pasted URL's so they are converted back to the way they are displayed in HTML
	; Richard.von.Mallesch@csiro.au <mailto:Richard.von.Mallesch@csiro.au>  -> Richard.von.Mallesch@csiro.au
	; www.csiro.au <http://www.csiro.au/>  -> www.csiro.au
	; https://www.csiro.au:443/foo/bar.htm <https://www.csiro.au:443/foo/bar.htm>  -> https://www.csiro.au:443/foo/bar.htm
	; <\\server1-syd\share$\file.text>  -> \\server1-syd\share$\file.text
	; It also checks for URLS and formats them as such.

	Dim $KeyCode, $Paste, $regEx, $regEx2, $ss, $match, $diff, $text, $matched

	If VarType($txtBody)
		$Paste = 1
	Else
		$txtBody = $System.Sender
		$KeyCode = $txtBody.KeyCode
	EndIf

	$ss = $txtBody.SelectionStart
	$text = Replace($txtBody.Text, @CRLF, Chr(10))
	If Len($text) = $ss
		$text = $text + " "
	Else
		$text = Left($text, $ss + 1)
	EndIf
	If $KeyCode = 32 Or $KeyCode = 13 Or ($KeyCode = 86 And $txtBody.LastKey = 17) Or $Paste ;Space, Enter, Ctrl+V, or pasted
		$regEx = CreateObject("VBscript.RegExp")
		$regEx.Pattern = "<?((\\\\[\w-.\\$]+)|((([\w.%+-]+@)|(\w{3,6}:(//)?)|(\w+\.))\w+(\.\w+)+(:\d+)?[^\s>]*))>?( <\w{0,6}:?(//)?\1/?>)?"
		$regEx.Global = 1
		$regEx2 = CreateObject("VBscript.RegExp")
		$regEx2.Pattern = "^\w+(\.\w+){2,}$"
		If $KeyCode = 32 Or $KeyCode = 13 $txtBody.LastSelectionStart = InStrRev(Replace(Left($text, -2), Chr(10), " "), " ") EndIf
		For Each $match in $regEx.Execute(SubStr($text, $txtBody.LastSelectionStart + 1, $ss - $txtBody.LastSelectionStart))
			If Not ($match.Value = $match.SubMatches(0) And $RegEx2.Test($match.Value))
				$txtBody.SelectionStart = $txtBody.LastSelectionStart + $match.FirstIndex + $diff
				$txtBody.SelectionLength = $match.Length
				$txtBody.SelectionColor = "Blue"
				$txtBody.SelectionFontUnderline = 1
				$txtBody.SelectedText = $match.SubMatches(0)
				$diff = $diff + Len($match.SubMatches(0)) - $match.Length
			EndIf
			$matched = 1
		Next
		If $matched And $ss + $diff = $txtBody.SelectionStart + $txtBody.SelectionLength
			$txtBody.SelectionStart = $ss + $diff
			$txtBody.SelectionColor = $txtBody.LastColour
			$txtBody.SelectionFontUnderLine = 0
			$txtBody.SelectedText = $txtBody.SelectedText + " "
			$txtBody.SelectionStart = $ss + $diff + 1
		Else
			$txtBody.SelectionStart = $ss + $diff
		EndIf
	Else
		If Right($text, 1) = " " And $KeyCode = 8 ; BackSpace
			$regEx = CreateObject("VBScript.RegExp")
			$regEx.Pattern = "((\\\\[\w-.\\$]+)|((([\w.%+-]+@)|(\w{3,6}:(//)?)|(\w+\.))\w+(\.\w+)+(:\d+)?[^\s>]*))"
			If $regEx.Test($txtBody.LastWord) And $txtBody.SelectionFontUnderLine = 1
				$txtBody.SelectionStart = $txtBody.LastSelectionStart - Len($txtBody.LastWord)
				$txtBody.SelectionLength = Len($txtBody.LastWord) - 1
				$txtBody.SelectedText = $txtBody.LastWord
				$txtBody.SelectionColor = $txtBody.LastColour
				$txtBody.SelectionStart = $txtBody.LastSelectionStart
				$text = $text + " "
			EndIf
		Else
			$txtBody.LastKey = $KeyCode
			$txtBody.LastColour = $txtBody.SelectionColor
		EndIf
	EndIf
	$text = Left($text, -1)
	$ = InStrRev(Replace($text, Chr(10), " "), " ")
	$txtBody.LastWord = SubStr($text, IIf($, $, 1))
	$txtBody.LastSelectionStart = $txtBody.SelectionStart

EndFunction

If you can't get what you want that way, you could also generate the RichText code and set .rtf to it.

Cheers,

Richard


JochenAdministrator
(KiX Supporter)
2011-05-07 08:21 AM
Re: Kixforms RichTextBox

Hmm .. I was quite sure that I did used .RichTextBox() in one of my older apps like PostPrep .. but the Preview was inside an ie Form..

Then I thought I had one in another project of mine .. but that was 'only' a .GridView()..

So, nope, no experience with RichTextBoxes \:\(


ShaneEP
(MM club member)
2011-05-07 04:46 PM
Re: Kixforms RichTextBox

I think I've decided on how I'm going to go about it. It's a little overkill probably. But I'm gonna generate the text in the Richtextbox using the normal rtf formatting, but in the background I'm gonna write my own HTML file with the same info. This way it's easier to display the info in nicely formatted rows/columns in the rtb...and then I can still use the IE.application to print the .html file in the background when they want to print the report. I was having problems getting IE to print .rtf files silently.