|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionWindows Forms databinding has greatly improved in .NET 2.0. Microsoft has enhanced our object-oriented toolbox by allowing Windows Forms controls to databind to our custom objects' properties. This new functionality is centered around the new BackgroundAnother option that I considered exploring was creating a custom attribute to add to the properties that I wanted to have Using the codeWe start off by creating a class library that will contain our custom We will be supporting two options for how the databound control of the Public Enum ReadOnlyBehavior
Disable = 0
Hide = 1
End Enum
We will now add a class named Public Class SmartPropertyBindingSource
Inherits BindingSource
End Class
Next, we will create a Public Property ReadOnlyControlBehavior() As ReadOnlyBehavior
Get
Return mReadOnlyBehavior
End Get
Set(ByVal value As ReadOnlyBehavior)
mReadOnlyBehavior = value
End Set
End Property
We will also go ahead and provide overloads for the three constructors in the base class. Once we have done this, we are ready to write the functionality of the class. By overriding the Protected Overrides Sub OnBindingComplete(ByVal e As _
System.Windows.Forms.BindingCompleteEventArgs)
'get the current type of the object we are binding
Dim t As Type = e.Binding.BindingManagerBase.Current.GetType
'get the property information using the BindingMemberInfo object
Dim prop As System.Reflection.PropertyInfo = _
t.GetProperty(e.Binding.BindingMemberInfo.BindingMember)
'see if it ReadOnly
If Not prop.CanWrite Then
'decide what to do with the control
Select Case Me.ReadOnlyControlBehavior
Case ReadOnlyBehavior.Disable
e.Binding.Control.Enabled = False
Case ReadOnlyBehavior.Hide
e.Binding.Control.Visible = False
End Select
End If
'continue with the method in the base class
MyBase.OnBindingComplete(e)
End Sub
That's all there is to it. Using reflection, we can investigate whether the property is Points of InterestThe new .NET 2.0 framework provides a great deal of enhanced functionality from the previous framework allowing for easier integration of custom business objects. We can build upon this functionality by customizing classes to better suit our needs.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||