add my own property to an existing control

H

Heath P. Dillon

Hi,
I put some data into a .tag property of a node for a treeview control, but
want to be able to store more data in other properties such as .tag2, .tag3
etc..

Is this possible to do? Can I add my own properties to an existing control ?

If possible, how is it done ?

thanks
 
T

Tom Shelton

Hi,
I put some data into a .tag property of a node for a treeview control, but
want to be able to store more data in other properties such as .tag2, .tag3
etc..

Is this possible to do? Can I add my own properties to an existing control ?

If possible, how is it done ?

thanks

For windows forms, you might look at extender providers:
http://msdn.microsoft.com/en-us/library/34kza2ww(VS.71).aspx

In WPF this is much easier with the concept of attached/dependancy properties.
 
J

James Hahn

You can do this by subclassing the control. See, for instance:

http://www.freevbcode.com/showcode.asp?ID=4446
"Here are two sample Visual Basic .NET projects which demonstrate the rather
simple but extremely powerful technique of subclassing the TreeView,
ComboBox, or ListBox, such that you can store additional custom information
behind (or inside) each individual Tree Node, Listbox Item or Combobox Item
as you desire. There is no logical limit to the amount of additional
information which can be stored alongside each individual node or item.
Details documentation is included."
 
J

Jack Jackson

Hi,
I put some data into a .tag property of a node for a treeview control, but
want to be able to store more data in other properties such as .tag2, .tag3
etc..

Is this possible to do? Can I add my own properties to an existing control ?

If possible, how is it done ?

thanks

I would create a class with whatever properties you want, then set the
Tag field to an instance of the class.
 
C

Cor Ligthert[MVP]

Heath,

In those cases I use the UserControl by inheriting the one where I want to
add a property.


This is quiet common.

By the way, the Type of Tag is object so you can even add an arraylist to
it.

Cor
 
P

Phill W.

Heath said:
I put some data into a .tag property of a node for a treeview control,
but want to be able to store more data in other properties such as
.tag2, .tag3 etc..

Is this possible to do? Can I add my own properties to an existing
control ?

No, but you can extend the TreeNode class so it contains whatever you
want it to have:

Class CustomLeaf
Inherits TreeNode

Public Sub New()
MyBase.New()
End Sub

' One new property
Public Property S1() as String ' say
Get/Set
End Property

' And another
Public Property Font() as System.Drawing.Font 'say
Get/Set
End Property

' ... as far as you like ...

End Class

.... then ...

Dim oCL as New CustomLeaf()

Me.TreeView1.Nodes.Add( oCL )

or

Me.TreeView1.Nodes(0).Nodes(2).Nodes(3).Add( oCL )

Whatever you can do with the TreeNode, you can do with your customised one.


Possibly OTT, but I'll throw it in anyway ...

If you're using a tree to display some "collection" of related objects,
have each object provide [a property that returns] a customised
"TreeNode" but give that TreeNode a reference back to the original
object, something like

Class LinkedThing

Public Function GetTreeNode() as ViewerNode
Return New ViewerNode( Me )
End Function

End Class

Class ViewerNode
Inherits TreeNode

Private Sub New()
End Sub

Friend Sub New( byVal source as LinkedThing )
m_source = source
MyBase.Text = source.Text ' say
End Sub

Public ReadOnly Property Source() as LinkedThing
Get
Return m_source
End Get
End Property

End Class

This way, you can access properties of objects in your "main" collection
(which may be of a totally different Type, inheriting from something
totally different), but still view and arrange them in a tree structure.

Just a thought.

HTH,
Phill W.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top