Hi,
[quoted text, click to view] "MamaKike" <MamaKike.1z3rpz@no-mx.forums.yourdomain.com.au> wrote in message
news:MamaKike.1z3rpz@no-mx.forums.yourdomain.com.au...
>
> I'm a newbie using vb .net, and i'm trying to develop a UserControl.
> In this control i'm doing all the drawing and i want to add a
> datasource property to it. :mad:
> i'm doing this
>
> Public Property Datasource() As Object
> Get
> return _datasource
> End Get
> Set (ByVal Value As Object)
> _datasource = object
Should be :
_datasource = Value
[quoted text, click to view] > end set
>
> But when i assign the property to the datasource that i want, the
> _datasource in the object is nothing.
>
> what is that i'm miss here. Can anyone help me. Does anyone have a bit
> of code that implement this feature???
At the end of the post, i'll add an example that shows some code for a basic
bindable control with very basic drawing.
The idea is that you try to avoid using the DataSource directly, always try
to get/set data through the CurrencyManager, this way you can bind to
anything (DataSet, DataTable, DataView, custom collection).
The methods GetFieldName, FieldCount, RowCount, GetValue, SetValue all use
the CurrencyManager and are meant to make it easier.
HTH,
Greetings
(Watch the line wraps)
Public Class BindableUserControl
Inherits System.Windows.Forms.UserControl
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'UserControl overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
CheckCurrencyMgr(True)
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
components = New System.ComponentModel.Container
End Sub
#End Region
Private _DataSource As Object
Private _DataMember As String = ""
Private _CurrencyMgr As CurrencyManager
Public Property DataSource() As Object
Get
Return _DataSource
End Get
Set(ByVal Value As Object)
If (Not Value Is _DataSource) Then
_DataSource = Value
CheckCurrencyMgr(False)
Refresh()
End If
End Set
End Property
Public Property DataMember() As String
Get
Return _DataMember
End Get
Set(ByVal Value As String)
If (Value <> _DataMember) Then
_DataMember = Value
CheckCurrencyMgr(False)
Refresh()
End If
End Set
End Property
Private Sub CheckCurrencyMgr(ByVal ReleaseOnly As Boolean)
If (Not _CurrencyMgr Is Nothing) Then
' release CurrencyManager
RemoveHandler _CurrencyMgr.ItemChanged, AddressOf
OnCurrencyMgr_ItemChanged
RemoveHandler _CurrencyMgr.PositionChanged, AddressOf
OnCurrencyMgr_PosChanged
RemoveHandler _CurrencyMgr.MetaDataChanged, AddressOf
OnCurrencyMgr_MetaChanged
End If
_CurrencyMgr = Nothing
If (Not _DataSource Is Nothing AndAlso ReleaseOnly = False) Then
' attach CurrencyManager
_CurrencyMgr = BindingContext(_DataSource, _DataMember)
AddHandler _CurrencyMgr.ItemChanged, AddressOf OnCurrencyMgr_ItemChanged
AddHandler _CurrencyMgr.PositionChanged, AddressOf
OnCurrencyMgr_PosChanged
AddHandler _CurrencyMgr.MetaDataChanged, AddressOf
OnCurrencyMgr_MetaChanged
End If
End Sub
Protected Overridable Sub OnCurrencyMgr_ItemChanged(ByVal sender As Object,
ByVal e As ItemChangedEventArgs)
' list or items inside list have changed
' check e for more information
Refresh()
End Sub
Protected Overridable Sub OnCurrencyMgr_MetaChanged(ByVal sender As Object,
ByVal e As EventArgs)
' fields changed
' check e for more information
Refresh()
End Sub
Protected Overridable Sub OnCurrencyMgr_PosChanged(ByVal sender As Object,
ByVal e As EventArgs)
' if your control supports selecting an item, then
' you must set SelectedIndex to _CurrencyMgr.Postion
End Sub
Protected ReadOnly Property FieldCount() As Integer
Get
If (_CurrencyMgr Is Nothing) Then
Throw New Exception("Do not use before control is bound.")
End If
Return _CurrencyMgr.GetItemProperties().Count
End Get
End Property
Protected Function GetFieldName(ByVal idx As Integer) As String
If (_CurrencyMgr Is Nothing) Then
Throw New Exception("Do not use before control is bound.")
End If
Return _CurrencyMgr.GetItemProperties()(idx).Name
End Function
Protected ReadOnly Property RowCount() As Integer
Get
If (_CurrencyMgr Is Nothing) Then
Throw New Exception("Do not use before control is bound.")
End If
Return _CurrencyMgr.Count
End Get
End Property
Protected Function GetValue(ByVal FieldIdx As Integer, ByVal RowIdx As
Integer) As Object
If (_CurrencyMgr Is Nothing) Then
Throw New Exception("Do not use before control is bound.")
End If
Return
_CurrencyMgr.GetItemProperties()(FieldIdx).GetValue(_CurrencyMgr.List(RowIdx))
End Function
Protected Sub SetValue(ByVal FieldIdx As Integer, ByVal RowIdx As Integer,
ByVal Value As Object)
If (_CurrencyMgr Is Nothing) Then
Throw New Exception("Do not use before control is bound.")
End If
_CurrencyMgr.GetItemProperties()(FieldIdx).SetValue(_CurrencyMgr.List(RowIdx),
Value)
End Sub
Protected Overrides Sub OnPaint(ByVal e As
System.Windows.Forms.PaintEventArgs)
Try
' wait until bound
If (Not _CurrencyMgr Is Nothing) Then
' print field names
For colIdx As Integer = 0 To FieldCount - 1
e.Graphics.DrawString(GetFieldName(ColIdx), _
Font, Brushes.Black, colIdx * 60, 0)
Next
' print all rows
For rowIdx As Integer = 0 To RowCount - 1
For colIdx As Integer = 0 To FieldCount - 1
Dim Value As Object = GetValue(colIdx, rowIdx)
e.Graphics.DrawString(IIf(Value Is DBNull.Value, "<null>", Value), _
Font, Brushes.Black, colIdx * 60, 20 + (rowIdx * 20))
Next
Next
End If
Catch ex As Exception
Console.WriteLine(ex.ToString())
End Try
End Sub
End Class
[quoted text, click to view] >
> Thanks :D
>
>
> --
> MamaKike
> ------------------------------------------------------------------------
> MamaKike's Profile:
http://www.hightechtalks.com/m365 > View this thread:
http://www.hightechtalks.com/t2294972 >