Extending TreeView and TreeNode

G

Guest

Hello,

I've posted the same question on msdn forums, but I'll try it here too since
I'm lost here.

I'm creating a custom TreeView with custom TreeNode classes nested.

This is a sample what I want to do:

MyTreeView class:

public partial class MyTreeView : TreeView
{
public MyTreeView()
{
InitializeComponent();

Nodes.Add(new MyTreeNode());
}

private class MyTreeNode : TreeNode
{
public MyTreeNode() :
base("MyNode")
{
}
}
}

When I drag this control to a windows form, It will show the correct node
inside (created in the constructor), but it will also create this line on
form's InitializeComponent() method:

WindowsApplication2.MyTreeView.MyTreeNode myTreeNode1 = new
System.Windows.Forms.TreeNode("MyNode");

The problem here is that 1) the nested type is private, 2) there is no cast
from TreeNode to MyTreeNode.

Since I don't need the nodes to show up at design time, there is any way to
not create them only when at design?

Or is there any other way to do this?

Thanks

Pedro
 
S

Stoitcho Goutsev \(100\)

ferreira,

If you worry only about the designer generated code the easiest would be to
hide the Nodes porperty of the base class and apply attributes not to
serialize it:

[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public new TreeNodeCollection Nodes
{
get
{
return base.Nodes;
}

}

The user can still see and set the Nodes property from code thought, but
this you cannot faight because the property exist in the base class and it
is not even virtual.
 
G

Guest

Thanks for your reply Stoitcho Goutsev!

That was exactly what I was looking for.

Just tested and it worked fine!

Thanks!

Pedro Ferreira

Stoitcho Goutsev (100) said:
ferreira,

If you worry only about the designer generated code the easiest would be to
hide the Nodes porperty of the base class and apply attributes not to
serialize it:

[Browsable(false)]
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public new TreeNodeCollection Nodes
{
get
{
return base.Nodes;
}

}

The user can still see and set the Nodes property from code thought, but
this you cannot faight because the property exist in the base class and it
is not even virtual.

ferreira said:
Hello,

I've posted the same question on msdn forums, but I'll try it here too
since
I'm lost here.

I'm creating a custom TreeView with custom TreeNode classes nested.

This is a sample what I want to do:

MyTreeView class:

public partial class MyTreeView : TreeView
{
public MyTreeView()
{
InitializeComponent();

Nodes.Add(new MyTreeNode());
}

private class MyTreeNode : TreeNode
{
public MyTreeNode() :
base("MyNode")
{
}
}
}

When I drag this control to a windows form, It will show the correct node
inside (created in the constructor), but it will also create this line on
form's InitializeComponent() method:

WindowsApplication2.MyTreeView.MyTreeNode myTreeNode1 = new
System.Windows.Forms.TreeNode("MyNode");

The problem here is that 1) the nested type is private, 2) there is no
cast
from TreeNode to MyTreeNode.

Since I don't need the nodes to show up at design time, there is any way
to
not create them only when at design?

Or is there any other way to do this?

Thanks

Pedro
 

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