all groups > vb.net controls > november 2005 >
You're in the

vb.net controls

group:

usercontrol datasource property implementation


usercontrol datasource property implementation MamaKike
11/26/2005 12:00:00 AM
vb.net controls:

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
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
end set

But when i assign the property to the datasource that i want, th
_datasource in the object is nothing.

what is that i'm miss here. Can anyone help me. Does anyone have a bi
of code that implement this feature???

Thanks :

--
MamaKik
-----------------------------------------------------------------------
MamaKike's Profile: http://www.hightechtalks.com/m36
View this thread: http://www.hightechtalks.com/t229497
Re: usercontrol datasource property implementation Bart Mermuys
11/27/2005 12:02:28 AM
Hi,

[quoted text, click to view]

Should be :
_datasource = Value

[quoted text, click to view]

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]

Re: usercontrol datasource property implementation Fernando Cardoso
12/6/2005 1:14:52 AM
I have the same question, and I'm some stuck on this.

Using an object to store the datasource will not give you the possibility to
change the datasource using the property window designer.

I tried the IListSource and it used the right designer to change the
datasource, the problem is that I need to know the current item on the
datasource, I want to use this item properthies to set properties on my
custom control.

Using an IEnumerable to store the datasource will give me the current item
option, but it will not show the right designer. I think the right way to
implement a datasource on a custom control would give us the same designer
as the datagridview or other databindable controls.

Any tips on this problem will be appreciated.

Best regards,

Fernando Cardoso


"Bart Mermuys" <bmermuys.nospam@hotmail.com> escreveu na mensagem
news:eJsbD3t8FHA.1276@TK2MSFTNGP09.phx.gbl...
[quoted text, click to view]
Re: usercontrol datasource property implementation Fernando Cardoso
12/7/2005 9:07:08 PM
Very, very thanks!!!!!

Fernando Cardoso

"Bart Mermuys" <bmermuys.nospam@hotmail.com> escreveu na mensagem
news:OdEgnS3%23FHA.2812@TK2MSFTNGP09.phx.gbl...
[quoted text, click to view]
Re: usercontrol datasource property implementation Bart Mermuys
12/7/2005 10:24:52 PM
Hi,

[quoted text, click to view]

You should get a CurrencyManager for the DataSource+DataMember and then use
CurrencyManager.GetItemProperties.

[quoted text, click to view]

Then you'll need to use the same (relevant) attributes that the
DataGrid(View) has for DataSource/DataMember:

In NET.1.1:

[TypeConverter("System.Windows.Forms.Design.DataSourceConverter,
System.Design, Version=1.0.5000.0, Culture=neutral,
PublicKeyToken=b03f5f7f11d50a3a")]
public object DataSource {....}

[Editor("System.Windows.Forms.Design.DataMemberListEditor, System.Design,
Version=1.0.5000.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
typeof(UITypeEditor))]
public string DataMember { ... }

In NET2.0:
[AttributeProvider(typeof(IListSource))]
public object DataSource { ... }

[Editor("System.Windows.Forms.Design.DataMemberListEditor, System.Design,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
typeof(UITypeEditor))]
public string DataMember { ... }


HTH,
Greetings

[quoted text, click to view]
AddThis Social Bookmark Button