Author Topic: DataBinding to a Rich Text Box  (Read 5920 times)

0 Members and 1 Guest are viewing this topic.

SomeCallMeDave

  • Guest
DataBinding to a Rich Text Box
« 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.

Code: [Select]
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; }
        }
    }
}



kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2144
  • class keyThumper<T>:ILazy<T>
Re: DataBinding to a Rich Text Box
« Reply #1 on: February 05, 2009, 04:56:19 PM »
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 :)
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

SomeCallMeDave

  • Guest
Re: DataBinding to a Rich Text Box
« Reply #2 on: February 05, 2009, 05:14:18 PM »
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.
« Last Edit: February 05, 2009, 05:19:19 PM by David Blackmon »

kdub_nz

  • Mesozoic keyThumper
  • SuperMod
  • Water Moccasin
  • Posts: 2144
  • class keyThumper<T>:ILazy<T>
Re: DataBinding to a Rich Text Box
« Reply #3 on: February 05, 2009, 05:35:00 PM »
 perhaps something like this  MAY work
Code: [Select]
   
   set
   {
      if ((value != null)
      &&
      (value.StartsWith("{\\rtf", true,System.Globalization.CultureInfo.CurrentCulture))
      )
      {
         base.Rtf = value;
      }
      else
      {
         base.Text = value;
      }
   }
Called Kerry in my other life
Retired; but they dragged me back in !

I live at UTC + 13.00

---
some people complain about loading the dishwasher.
Sometimes the question is more important than the answer.

SomeCallMeDave

  • Guest
Re: DataBinding to a Rich Text Box
« Reply #4 on: February 05, 2009, 06:10:24 PM »
Actually, the easiest way seems to be
Code: [Select]
namespace NoneYa
{
    [System.ComponentModel.DefaultBindingProperty("Rtf")]
    class MyRichTextBox : System.Windows.Forms.RichTextBox
    {

    }
}

and just let base do ALL the work, ( for my purposes )

Ken Alexander

  • Newt
  • Posts: 61
Re: DataBinding to a Rich Text Box
« Reply #5 on: February 06, 2009, 11:34:12 AM »

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.

Code: [Select]

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

Ken Alexander

SomeCallMeDave

  • Guest
Re: DataBinding to a Rich Text Box
« Reply #6 on: February 06, 2009, 12:39:37 PM »


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.

TonyT

  • Guest
Re: DataBinding to a Rich Text Box
« Reply #7 on: February 08, 2009, 02:09:20 AM »
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 Alexander

  • Newt
  • Posts: 61
Re: DataBinding to a Rich Text Box
« Reply #8 on: February 09, 2009, 01:14:27 PM »

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.
Ken Alexander