TheSwamp
Code Red => .NET => Topic started by: SomeCallMeDave on February 05, 2009, 04:21:16 PM
-
OK, so I wanted to bind a property of my object (one of the report pages) to the Rtf property of a rich text box.
.Net didn't provided the data binding to the Rtf property so, after some web research, I created a little MyRichTextBox class that set the DefaultBindingProperty to "Rtf".
It works like a champ but seems WAY to easy. Is that all that I need to do? Or is there something that I am missing?
The whole new class is about 17 lines longs. So there must be something else that I need to do.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace NoneYa
{
[System.ComponentModel.DefaultBindingProperty("Rtf")]
class MyRichTextBox : System.Windows.Forms.RichTextBox
{
new public string Rtf
{
get { return base.Rtf; }
set { base.Rtf = value; }
}
}
}
-
Dave,
just flying past, so off the top of my head ..
What happens if you pass that a normal text string , as opposed to an rtf string ??
ie
should you test and differentiate for the set
.. either
base.Rtf = value;
or
base.Text = value;
ps .. may be WAY off base here .. not tested :)
-
Kerry,
I haven't tested passing in a non-RTF string (but I will shortly).
But (and these are famous last words), that should never happen in this program since the 'input' is a particular property and that property calls a function to return a report string that is Rtf format. The new control isn't intended for use other than this particular program.
But for my own edification, I will investigate testing the string.
EDIT:
I tested it and it does throw a "File format is not valid" exception but the standard RichTextBox throws the same thing is one tries to pass in an invalid string via .RTF property.
-
perhaps something like this MAY work
set
{
if ((value != null)
&&
(value.StartsWith("{\\rtf", true,System.Globalization.CultureInfo.CurrentCulture))
)
{
base.Rtf = value;
}
else
{
base.Text = value;
}
}
-
Actually, the easiest way seems to be
namespace NoneYa
{
[System.ComponentModel.DefaultBindingProperty("Rtf")]
class MyRichTextBox : System.Windows.Forms.RichTextBox
{
}
}
and just let base do ALL the work, ( for my purposes )
-
It works like a champ but seems WAY to easy. Is that all that I need to do? Or is there something that I am missing?
The whole new class is about 17 lines longs. So there must be something else that I need to do.
I know you are binding to an object, but I read somewhere that if you sub-class a control and add a new binding property, you also must add a matching <PropertyName>Changed event in order for the binding to work properly when data binding to a DataSet. I couldn’t find the discussion that I am referring to, but I did find an example of the concept. In addition the sample also gave the idea of using Value.StartsWith(“{\\rtf” to validate the String as Kerry did. I believe that is an error though. It should be “{\rtf” (single backslash).
I haven’t tested this to know how accurate this is, but I’m sure someone will let us all know if it is wrong.
Imports System.ComponentModel
<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), Bindable(True)> _
Public Shadows Property Rtf() As String
Get
Return MyBase.Rtf
End Get
Set(ByVal value As String)
If Not String.IsNullOrEmpty(value) AndAlso value.StartsWith("{\rtf", True, System.Globalization.CultureInfo.CurrentCulture) Then
MyBase.Rtf = value
Else
MyBase.Text = value
End If
End Set
End Property
End Class
-
I know you are binding to an object, but I read somewhere that if you sub-class a control and add a new binding property, you also must add a matching <PropertyName>Changed event in order for the binding to work properly when data binding to a DataSet. ....
Ken,
I have seen that idea expressed also and was trying to create the event when something shiny entered my field of view and my attention wandered. The binding seems to work without it, but there is one minor glitch that I will try to fix by adding the event.
Thanks for the sample code. I will post back with any resu Hey look!! A squirrel.
-
Either you are binding to an object, or you are binding
to a DataSet.
If you're binding to a specialization of another class,
then as long as the base class supports the required
property change notification, you're set - no events
needed.
I know you are binding to an object, but I read somewhere that if you sub-class a control and add a new binding property, you also must add a matching <PropertyName>Changed event in order for the binding to work properly when data binding to a DataSet. ....
Ken,
I have seen that idea expressed also and was trying to create the event when something shiny entered my field of view and my attention wandered. The binding seems to work without it, but there is one minor glitch that I will try to fix by adding the event.
Thanks for the sample code. I will post back with any resu Hey look!! A squirrel.
-
Ken,
I have seen that idea expressed also and was trying to create the event when something shiny entered my field of view and my attention wandered. The binding seems to work without it, but there is one minor glitch that I will try to fix by adding the event.
Thanks for the sample code. I will post back with any resu Hey look!! A squirrel.
David,
I did verify that a matching <PropertyName>Changed event is required for proper binding to a DataSet. Without this event the RowState may return Modified when in reality nothing changed. Even though you are binding to a custom object, you still should make your custom RichTextBox with the proper events…..never know; you might someday bind it to a DataSet.