Binding to RichTextBox

Using VS 2005 Beta 2, VB, SQL Server 2000

I have an existing SQL table with a field 'Comments' of SQL datatype 'text'. The data stored in the field was created using the RichTextBox under VB6.

I use a Data Source/Data Set and set the Comments field to RichTextBox. When I drag the control onto the form a RichTextBox is created but the control displays the existing RTF as text. I expected a RTF under DataBindings but did find it there.

Is it possible to display/generate RTF in a RichTextBox and store the RTF in a bound field?

Here is a typical RTF string:
{\rtf1\ansi\deff0\deftab720{\fonttbl{\f0\fswiss MS Sans Serif;}{\f1\froman\fcharset2 Symbol;}{\f2\fswiss MS Sans Serif;}{\f3\froman Times New Roman;}{\f4\fswiss Arial;}}
{\colortbl\red0\green0\blue0;}
\deflang1033\pard\plain\f3\fs24 periodic compliance testing required annually and triennially}

I can set the copntrol's RTF property to the text above and it is decoded OK but the resulting plain text is stored in the DB.

Also the control's RichTextShortCutsEnabled = True but the typical Alt-B type commands do not function.

Has anyone gotten this control to function properly?

[1171 byte] By [BobHeitzman] at [2008-3-1]
# 1

Unfortunately, we do not provide in the box support for binding to the Rtf property on RichTextBox. One way you can enable this it so sub-class the RichTextBox to expose the Rtf property as Bindable. You can then make your RichTextBox control available to the DataSources window and bind to it. To do this, in the DataSource window, click on the property you want to bind to (e.g. Comments) and then click the drop down arrow by the property and select "Customize...". From the "Associated Controls" list, select your sub-classed RichTextBox (see below for sample code for the sub-classed RichTextBox).

As for "Alt-B", this is not a supported short cut key on RichTextBox. Look here for the available short cut keys: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/commctls/richedit/richeditcontrols/aboutricheditcontrols.asp

Joe

Sample sub-classed RichTextBox:


<System.ComponentModel.DefaultBindingProperty("Rtf")> _
Public Class MyRichTextBox
Inherits System.Windows.Forms.RichTextBox

Public Event RtfChanged As EventHandler

Protected Overrides Sub OnTextChanged(ByVal e As System.EventArgs)
MyBase.OnTextChanged(e)
Me.OnRtfChanged(e)
End Sub

Protected Overridable Sub OnRtfChanged(ByVal e As System.EventArgs)
RaiseEvent RtfChanged(Me, e)
End Sub

<System.ComponentModel.Browsable(False), System.ComponentModel.DesignerSerializationVisibility(System.ComponentModel.DesignerSerializationVisibility.Hidden)> _
Public Shadows Property Rtf() As String
Get
Return MyBase.Rtf
End Get
Set(ByVal value As String)
MyBase.Rtf = value
End Set
End Property
End Class

JoeStegman at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 2
>Unfortunately, we do not provide in the box support for binding to the Rtf property on RichTextBox.

That's strange - hasn't binding been available for several generations of the RichTextControl?

I know I have used it lots of times in VB6 and Access. Actually I guess it was only RTF that was supported and you had to force the control to display text only.

Why stop supporting RTF as the default now?

BobHeitzman at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 3
I agree this is the primary property you'd want to use when binding to a RichTextControl and VB6 did provide a better experience. Unfortunately, this is something that was never fully supported in any version of the .NET Framework. I am tracking this as something we'll look at post VS 2005.

Thanks for the feedback,

Joe

JoeStegman at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 4

Look here for the available short cut keys: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/commctls/richedit/richeditcontrols/aboutricheditcontrols.asp

Intersesting - only a subset of the keystrokes are working. For example Ctrl-A does not select all. Nor do the clipboard shortcuts function.

I would have thought maybe none would function, but some, like Ctrl-1, 2, 5 do work.

I'd guess the link above is not the correct reference for the RichTextControl in VS05.

Another ommission seems to be the Version property.

Has anyone seen a good example of how to work with the RichTextControl in VB.Net or VS05?

BobHeitzman at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 5
There must be something else going on - the edit commands do work. Do you have a MenuStrip or MainMenu on your Form that has shortcuts keys for edit commands? If so, these will trump the shortcut key handling for the RichTextBox.

Joe

JoeStegman at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 6

I decided to try a work around. I launch WordPad to do the editing.

I set the File Type .tmp to invoke WordPad.
I removed the RichTextBox's data binding.
As the row changes I stuff the value of a field "Memo", which contains RTF text, into the RichTextControl's .rtf property.
On double click I stash the rtf in .tmp file and launch WordPad and after user OK save the results.

Private Sub StaffBindingSource_CurrentChanged(ByVal sender As Object...
mrow = Me.StaffBindingSource.Current ' DataRowView
Me.MemoRichTextBox.Rtf = mrow("Memo").ToString
End Sub

Private Sub MemoRichTextBox_DoubleClick(ByVal sender As Object....
Dim sFilename As String = My.Computer.FileSystem.GetTempFileName()
Me.MemoRichTextBox.SaveFile(sFilename)
System.Diagnostics.Process.Start(sFilename) 'ShellEx-like
If MsgBox("Click OK to accept changes:", MsgBoxStyle.OKCancel) = MsgBoxResult.OK Then
Me.MemoRichTextBox.LoadFile(sFilename, RichTextBoxStreamType.RichText)
mrow("Memo") = Me.MemoRichTextBox.Rtf
End If
If My.Computer.FileSystem.FileExists(sFilename) Then My.Computer.FileSystem.DeleteFile(sFilename)
End Sub

There is polish need to prevent user from making direct changes on the form, undo, etc.
Anyone see any technical issues with the above?

BobHeitzman at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...
# 7
Good example, this has only problem if the text I try to assign to Rtf value is not in RTF format (eg PlainText). RichTextBox can not deal with this. It is necessary to make the Rtf property similar to this:

[System.ComponentModel.Browsable(false), System.ComponentModel.DesignerSerializationVisibility(System.ComponentModel.DesignerSerializationVisibility.Hidden), Bindable(true)]

public new string Rtf

{

get

{

return base.Rtf;

}

set

{

if ((value != null) && (value.StartsWith("{\\rtf", true, System.Globalization.CultureInfo.CurrentCulture)))

{

base.Rtf = value;

}

else

{

base.Text = value;

}

}

}

MarekMergl at 2007-9-8 > top of Msdn Tech,Windows Forms,Windows Forms Data Controls and Databinding...