Reputation: 6778
I have a custom TreeView
class like so:
using System.Windows.Forms;
namespace My.Namespace
{
public partial class SimpleTree : System.Windows.Forms.TreeView
{
private TreeNode topNode; //////////
private TreeNode lowerNode1;
private TreeNode lowerNode2;
private TreeNode lowerNode3;
public SimpleTree()
{
InitializeComponent();
BuildTree();
TreeViewEventArgs eventArgs = new TreeViewEventArgs(topNode, TreeViewAction.Unknown);
this.ExpandAll(); // must be after adding the nodes
}
private void BuildTree()
{
this.Name = "Simple Tree";
topNode = new TreeNode("Top Node");
topNode.Name = "Top Node"
this.Nodes.Add(topNode);
lowerNode1 = new TreeNode("Lower Node 1");
lowerNode1.Name = "Lower Node 1";
topNode.Nodes.Add(lowerNode1);
lowerNode2 = new TreeNode("Lower Node 2");
lowerNode2.Name = "Lower Node 2";
topNode.Nodes.Add(lowerNode2);
lowerNode3 = new TreeNode("Lower Node 3");
lowerNode3.Name = "Lower Node 3";
topNode.Nodes.Add(lowerNode3);
}
// This is a simplified example, much more functionality here in real code
}
}
I include this SimpleTree in ProjectView.cs (that has many components).
In ProjectView.Designer.cs, there is this:
private SimpleTreeView trvSimple;
this.trvSimple = new My.Namespace.SimpleTreeView();
this.trvSimple.BackColor = System.Drawing.Color.White;
this.trvSimple.Location = new System.Drawing.Point(3, 65);
this.trvSimple.Name = "trvSimple";
this.trvSimple.Scrollable = false;
this.trvSimple.Size = new System.Drawing.Size(128, 109);
this.trvSimple.TabIndex = 1;
this.trvSimple.Text = "trvSimple";
this.trvSimple.BeforeCollapse += new System.Windows.Forms.TreeViewCancelEventHandler(this.trvSimple_BeforeCollapse);
this.trvSimple.BeforeSelect += new System.Windows.Forms.TreeViewCancelEventHandler(this.trvSimple_BeforeSelect);
This all works. The tree looks like:
Top Node
-- Lower Node 1
-- Lower Node 2
-- Lower Node 3
However, when I change anything else in ProjectView, the Visual Editor then adds this code to ProjectView.Designer.cs
System.Windows.Forms.TreeNode treeNode1 = new System.Windows.Forms.TreeNode("Lower Node 1");
System.Windows.Forms.TreeNode treeNode2 = new System.Windows.Forms.TreeNode("Lower Node 2");
System.Windows.Forms.TreeNode treeNode3 = new System.Windows.Forms.TreeNode("Lower Node 3");
System.Windows.Forms.TreeNode treeNode6 = new System.Windows.Forms.TreeNode("Top Node", new System.Windows.Forms.TreeNode[] {
treeNode1,
treeNode2,
treeNode3});
So now it looks like this:
Top Node
-- Lower Node 1
-- Lower Node 2
-- Lower Node 3
Top Node
-- Lower Node 1
-- Lower Node 2
-- Lower Node 3
How do I prevent this code from being added?
Upvotes: 1
Views: 1112
Reputation: 81610
You probably shouldn't do this from a custom control. It would be simpler to just populate a TreeView control at run time in the form's constructor.
If you still want to do this from your custom control, you can try only adding the nodes when you are not design mode:
public SimpleTree() {
if (LicenseManager.UsageMode != LicenseUsageMode.Designtime) {
BuildTree();
}
this.ExpandAll();
}
The other possibility is to just override the Nodes collection and force it to not serialize the data at design time:
[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
public new TreeNodeCollection Nodes {
get {
return base.Nodes;
}
}
This would make adding or editing any nodes at design time pointless since it wouldn't save anything.
Upvotes: 1