Associating a set function with a TreeView Node

B

bg_ie

Hi,

Lets say I have a TreeView with nodes that represent objects of a
number of different types. Is there a way of associating the Set
method of each object with its node in the TreeView? That way when you
select a node in order to change its associated data, you have easy
access to its Set function.

The way I normally handel this situation is is to create an enum of
all the different types that the treeview contains. I then have a
string table which corresponds to each member of the enum. I can then
use these to set the Name member of each Node and hence establish from
that which type I'm working with.

I'm guessing there is an easier way of doing this...

Thanks,

Barry.
 
L

Larry Lard

Hi,

Lets say I have a TreeView with nodes that represent objects of a
number of different types. Is there a way of associating the Set
method of each object with its node in the TreeView? That way when you
select a node in order to change its associated data, you have easy
access to its Set function.

The way I normally handel this situation is is to create an enum of
all the different types that the treeview contains. I then have a
string table which corresponds to each member of the enum. I can then
use these to set the Name member of each Node and hence establish from
that which type I'm working with.

I'm guessing there is an easier way of doing this...

Two approaches spring to mind:

- If all these objects have a Set method*, then pull that method out
into an ISettable interface that they all then implement. Then have each
TreeNode hold** a reference to an object of type ISettable which it can
use to get at its ISettable.

- If the objects 'setting' behaviour isn't uniform, define a delegate
type (from void to void, or whatever), then in the TreeNode hold** a
delegate to the object's DoWhatever method.

* If 'Set' is just an example, OK, but if not then it's stylsitically
not a very nice name for a method.

** Two ways to make a TreeNode hold an arbitrary extra piece of
information: lazy + dirty, use the Tag property; clean and not much more
work, derive from TreeNode a TreeNodeEx class that holds whatever
(strongly typed) extra information you need.
 
B

bg_ie

Two approaches spring to mind:

- If all these objects have a Set method*, then pull that method out
into an ISettable interface that they all then implement. Then have each
TreeNode hold** a reference to an object of type ISettable which it can
use to get at its ISettable.

- If the objects 'setting' behaviour isn't uniform, define a delegate
type (from void to void, or whatever), then in the TreeNode hold** a
delegate to the object's DoWhatever method.

* If 'Set' is just an example, OK, but if not then it's stylsitically
not a very nice name for a method.

** Two ways to make a TreeNode hold an arbitrary extra piece of
information: lazy + dirty, use the Tag property; clean and not much more
work, derive from TreeNode a TreeNodeEx class that holds whatever
(strongly typed) extra information you need.

--
Larry Lard
(e-mail address removed)
The address is real, but unread - please reply to the group
For VB and C# questions - tell us which version- Dölj citerad text -

- Visa citerad text -

Cool, thanks very much for your help Larry. I still have one remaining
problem though.

Each object, represented as a node in the TreeView, has a member
called "comment". So I've decided to begin here. My TreeNodeEx class
is as follows -

namespace MyApplication
{
class TreeNodeEx : TreeNode
{
public CommentInterface commentInterface;
}
}

With my interface then looking like -

interface InterfaceComment
{
string Comment
{
get;
set;
}
}

Which my objects then implement.

Now I create a TreeNodeEx object as follows -

TreeNodeEx treeNode = new TreeNodeEx();
treeNode.Text = person[0].Name;

But how do I set the interface?

treeNode.commentInterface = person[0].???;

person[0].Comment cant be assigned to treeNode.commentInterface since
it is returning a bool and is not a reference to its interface.

Thanks again,

Barry.
 
J

Jay Mitchell

person[0].Comment cant be assigned to treeNode.commentInterface since
it is returning a bool and is not a reference to its interface.

I'm assuming that you meant that person[0].Comment is returning a
string rather than a bool....

You need a concrete implementation of InterfaceComment. In this case,
person's class should implement InterfaceComment.

class Person : InterfaceComment

Now you can assign person[0] to treeNode.commentInterface.
 

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