BindingSourcePlus class

Posted Thu, Aug 3 2006 1:11 by bill
source for my BindingSourcePlus class. 
 
 
Option Strict On
Option Explicit On
 
Imports System
Imports System.Windows.Forms
 
<System.ComponentModel.DesignerCategory("")> _
Public Class BindingSourcePlus
   Inherits BindingSource
 
#Region ".ctors"
 
   Public Sub New()
      '
   End Sub
 
   Public Sub New(ByVal components As System.ComponentModel.IContainer)
      MyBase.New(components)
   End Sub
 
#End Region
 
 
#Region "UI thread sensitive ListChanged invoke"
 
   Private m_Host As Control
 
   ''' <summary>
   ''' Get/Set the Host control to allow the Bindingsource to marshal LsitChanged events to the UI thread
   ''' </summary>
   ''' <value>the containing control or Form</value>
   ''' <returns></returns>
   ''' <remarks></remarks>
   Public Property Host() As Control
      Get
         Return m_Host
      End Get
      Set(ByVal value As Control)
         m_Host = value
      End Set
   End Property
 
 
   Protected Overrides Sub OnListChanged(ByVal e As System.ComponentModel.ListChangedEventArgs)
      Dim cntl As Control = Me.Host
      If cntl IsNot Nothing AndAlso cntl.InvokeRequired Then
         cntl.Invoke(New OnListChangedDelegate(AddressOf OnListChanged), e)
      Else
         MyBase.OnListChanged(e)
      End If
   End Sub
 
 
   Private Delegate Sub OnListChangedDelegate(ByVal e As System.ComponentModel.ListChangedEventArgs)
 
#End Region
 
 
#Region "cornstarch implementation see: http://msmvps.com/blogs/bill/archive/2005/10/05/69012.aspx"
 
 
   Private m_ReadValuesOnChange As Boolean
 
   Public Property ReadValuesOnChange() As Boolean
      Get
         Return m_ReadValuesOnChange
      End Get
      Set(ByVal value As Boolean)
         m_ReadValuesOnChange = value
      End Set
   End Property
 
   Protected Overrides Sub OnBindingComplete(ByVal e As System.Windows.Forms.BindingCompleteEventArgs)
      If m_ReadValuesOnChange Then
         e.Binding.ReadValue()
      End If
      MyBase.OnBindingComplete(e)
   End Sub
 
#End Region
 
 
End Class
 
 
 
Filed under:

Comments

# re: BindingSourcePlus class

Monday, September 18, 2006 3:36 PM by Luc Morin

Bill,

I'm quite surprised to see no comments on this proposed solution to the BindingSource issue.

I've seen also the BindingSourceRefresh class in Rocky's book (which you provided also if I'm not mistaken), and I'm wondering what the "prefered" solution would be.

Could you explain a bit the 2 different methods ? Is one more effective ?

Thank you.

# re: BindingSourcePlus class

Monday, September 18, 2006 7:58 PM by bill

hi Luc,

The last region in the above class is the same "effect" as my cornstarch extender provider example Rocky uses. The difference being this one is a derived class instead of using an extender. (so it's actually a lot less code <g>)

The other part is the bit that ensures that list changed events occur on the main Ui thread.  For that part to work you need to set the Host property (at design time)  If you don't then it doesn't marshal to the UI thread.

Basically I owuld recommend using this new class as it allows you to have your data source updating asynchronously regardless of how that is invoked, so you can move BO's into different tiers, share them amongst applications, etc, and the UI will update correctly :)