PC Review Forums Newsgroups Microsoft DotNet Microsoft Dot NET Framework Forms Extending TreeView and TreeNode

Reply

Extending TreeView and TreeNode

 
Thread Tools Rate Thread
Old 09-02-2006, 10:46 PM   #1
=?Utf-8?B?ZmVycmVpcmE=?=
Guest
 
Posts: n/a
Default Extending TreeView and TreeNode


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
  Reply With Quote
Old 10-02-2006, 04:10 PM   #2
Stoitcho Goutsev \(100\)
Guest
 
Posts: n/a
Default Re: Extending TreeView and TreeNode

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" <ferreira@discussions.microsoft.com> wrote in message
news:3A07B4BD-18BE-4B1C-991F-76C259AA4C10@microsoft.com...
> 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



  Reply With Quote
Old 10-02-2006, 04:30 PM   #3
=?Utf-8?B?ZmVycmVpcmE=?=
Guest
 
Posts: n/a
Default Re: Extending TreeView and TreeNode

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)" wrote:

> 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" <ferreira@discussions.microsoft.com> wrote in message
> news:3A07B4BD-18BE-4B1C-991F-76C259AA4C10@microsoft.com...
> > 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

>
>
>

  Reply With Quote
Reply



Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off