By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,905 Members | 1,616 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,905 IT Pros & Developers. It's quick & easy.

Associating a set function with a TreeView Node

P: n/a
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.

Apr 2 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
bg***@yahoo.com wrote:
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.
--
Larry Lard
la*******@googlemail.com
The address is real, but unread - please reply to the group
For VB and C# questions - tell us which version
Apr 2 '07 #2

P: n/a
On 2 Apr, 14:45, Larry Lard <larryl...@googlemail.comwrote:
b...@yahoo.com wrote:
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.

--
Larry Lard
larryl...@googlemail.com
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.

Apr 2 '07 #3

P: n/a
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.

Apr 3 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.