Click here to Skip to main content
14,733,889 members
Please Sign up or sign in to vote.
0.00/5 (No votes)

I want to use Richtextbox to store text with format in SQL database. I love the way System.Windows.Forms.RichTextBox handle binding RTF format but i hate the way it change multi-font format. So I use flowdocument to handle multi-font format. Below is my code

Imports System.Windows.Forms
Imports System.Windows.Documents
Imports System.IO
Imports System.Text
Private Sub chkbBold_Click(sender As Object, e As EventArgs) Handles chkbBold.Click
Dim SelStart As Integer = rtbOtherInfo.SelectionStart
Dim SelLength As Integer = rtbOtherInfo.SelectionLength

Dim fdTmp As FlowDocument = New FlowDocument()
Dim inStream As MemoryStream = New MemoryStream(Encoding.ASCII.GetBytes(rtbOtherInfo.SelectedRtf))
Dim outStream As New MemoryStream
Dim textRangeTmp As TextRange = New TextRange(fdTmp.ContentStart, fdTmp.ContentEnd)

textRangeTmp.Load(inStream, DataFormats.Rtf)

'Toggle chkbBold state
If chkbItalic.Checked = True Then
textRangeTmp.ApplyPropertyValue(TextElement.FontWeightProperty, Windows.FontWeights.Bold)
textRangeTmp.ApplyPropertyValue(TextElement.FontWeightProperty, Windows.FontWeights.Normal)
End If
textRangeTmp.Save(outStream, DataFormats.Rtf)

rtbOtherInfo.SelectedRtf = System.Text.ASCIIEncoding.Default.GetString(outStream.ToArray())
rtbOtherInfo.SelectionStart = SelStart
rtbOtherInfo.SelectionLength = SelLength

Catch ex As Exception
End Try
End Sub

But i see some schange behavior of richtextbox:
- in some selected text in richtextbox, this code work well, but in some case, there's nothing change in richtextbox after chkbBold_Click.
- some time I can set selected text to Bold but cannot set the selected text back to normal (with the same selected text)

Please kindly help to show me what im I wrong?
Please also kindly share you ideas.

Thank you all
Not quite clear. It looks like you are trying to mix up System.Windows.Forms with WPF. Why? They don't leave together well... and FlowDocument is not RichText. RTF is majorly obsolete, by the way...
tamthoima 5-Nov-12 11:38am
Thank you very much for your comment. As I already said, I need to store simple text format in database, so I use RichTextBox for simple databinding (RTF binding). But RichTextBox is very clumsy in changing text format, ...
By the way, do you have any other suggestion to fullfill my need (simply please)? Thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900