Prevent Inherited Property From Persisting

M

mikesnoise

I have a control called FolderTreeView that derives from
System.Windows.Forms.TreeView and automatically populates itself with
TreeNodes representing the Windows file system. Here's the behavior I'm
seeing:

1) Add the FolderTreeView control to my Form in the designer.
2) Modify the FolderTreeView.cs file.
3) Rebuild and go back to the Form designer that is hosting the control
from step 1.

The result is another root "Desktop" node has been added to the control
so I end up with duplicated entries. My theory is that the Nodes
property from the base TreeView class is being persisted with the form.
Then, the FolderTreeView's constructor runs which calls Nodes.Clear()
and then populates the control with top level FileSystem objects. Next,
the old nodes from before the rebuild are added back to the TreeView
control which results in duplicates.

My best guess at fixing this is to somehow prevent the designer from
persisting the Nodes property of the FolderTreeView control. But since
this property is inherited from TreeView I'm not sure how to change the
way it is persisted. Please help!

-Mike-
 
P

Peter Thornqvist

Can't you just redeclare the Nodes property like this:

private class FolderTreeView : System.Windows.Forms.TreeView
{
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public System.Windows.Forms.TreeNodeCollection Nodes;
}
 
M

mikesnoise

That seems to do it! Thanks! Now the only problem is that when the
TreeView control is selected in the designer the Properties window has
a link to "Edit Nodes". How do I remove this link?

-Mike-
 
M

mikesnoise

Actually this is the control I started with. I modified it to
automatically populate the nodes of the tree upon construction and
that's when I found the problem with the property persistence. I
suspect that's the reason the original author didn't have the
constructor populate the nodes.

-Mike-
 
P

Peter Thornqvist

That seems to do it! Thanks! Now the only problem is that when the
TreeView control is selected in the designer the Properties window has
a link to "Edit Nodes". How do I remove this link?

[Browsable(false)]

maybe?
 
M

mikesnoise

No, I already had [Browsable(false)] and that made the Nodes property
disappear from the property grid but did not remove the Edit Nodes verb
from the designer. Looked around online and found a better way to solve
all my problems. Turns out that by creating a new Designer class for
the tree view I can filter out properties that shouldn't be shown in
the properties grid. A side-effect of this is that any properties that
are filtered out will not persist in the designer. Also, this prevents
me from having to declare a new Nodes property in order to hide the
inherited property. The following code eliminates the Nodes property
from the properties grid and fixes the bug that prompted this thread:

internal class FolderTreeViewDesigner : ControlDesigner
{
protected override void
PostFilterProperties(System.Collections.IDictionary properties)
{
base.PostFilterProperties(properties);
properties.Remove("Nodes");
}
}

[DesignerAttribute(typeof(FolderTreeViewDesigner))]
public class FolderTreeView : TreeView
{
...
}

The added bonus here is that hijacking the Designer of the TreeView
control removes the Edit Nodes verb as well!

-Mike-

That seems to do it! Thanks! Now the only problem is that when the
TreeView control is selected in the designer the Properties window has
a link to "Edit Nodes". How do I remove this link?[Browsable(false)]

maybe?
 

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