开发者

"Ghost Nodes" in System.Windows.Forms.TreeView?

I have a simple form with a TreeView 开发者_开发问答called treeView1 and I like to add some nodes to it in the HandleCreated handler like this:

using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            treeView1.CheckBoxes = true;

            treeView1.HandleCreated += delegate
            {
                // Add one and only one node
                treeView1.Nodes.Add(new TreeNode("A Node"));
            };
        }
    }
}

When running this form there are actually two nodes visible! When I click the checkbox on the second node, the first node is checked/unchecked.

Does anybody know a way to prevent this "ghost node"?

The reason why I want to add nodes in the HandleCreated is, that this handler is used in a thread function which actually constructs a data structure for the nodes to be added. Adding the nodes later would mean I have to re-write a lot of my code.


That's weird! Looks like a bug to me... You should report it to the connect site.

One solution if you really want to hook on the handle creation is to derive from TreeView, like this, and use this class instead of the standard TreeView:

public class MyTreeView : TreeView
{
    protected override void OnHandleCreated(EventArgs e)
    {
        base.OnHandleCreated(e);
        Nodes.Add(new TreeNode("A Node"));
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜