0

I added a treeview to my main form, right-clicking opens a contextmenu where you can add new nodes to the tree (in this case categories).

It creates, then adds the node and calls BeginEdit()...

    private void addCategoryToolStripMenuItem_Click(object sender, System.EventArgs e)
    {
        var category = new TreeNode();

        tvCategories.Nodes.Add(category);
        category.BeginEdit();
    }

...and then this:

From the info I gathered this should work just fine, howeeeeever:

enter image description here Any ideas? :)

Just a kind of extension: the problem doesn't lie within BeginEdit(), I can't edit the label at all. I still don't know why, but now I know I need to look somewhere else.

LarsTech
  • 80,625
  • 14
  • 153
  • 225
momo
  • 119
  • 3
  • 10
  • EndEdit should not be required, as it will end editing automatically. You use EndEdit to force it to stop editing. – NibblyPig Aug 17 '16 at 13:05
  • The AfterLabelEdit event only fired after label editing. So trying to end it *again* does not make any sense whatsoever. TreeView is a cranky mother, it *will* byte back when you don't use it correctly. – Hans Passant Aug 17 '16 at 13:08
  • Doesn't really answer my question... I didn't use EndEdit or AfterLabelEdit in the first place, took it out again now, same result. – momo Aug 17 '16 at 13:08
  • See [this answer](http://stackoverflow.com/a/881629/1997232). The downvote is because you didn't prepared [repro](http://stackoverflow.com/help/mcve), so the people think it's your mistake with calling `EndEdit()` forcibly. – Sinatr Aug 17 '16 at 13:27

2 Answers2

1

Your initial node can't be blank, so fill it with some kind of text:

var category = new TreeNode("abc");
LarsTech
  • 80,625
  • 14
  • 153
  • 225
  • Just wrote my own answer while you did yours :D Thanks! That's absolutely correct and the solution to my problem :) – momo Aug 17 '16 at 13:46
0

While I'm still not entirely sure why, the above code created a node that was uneditable even though the LabelEdit property was true.

This however seems to do the trick:

    private void addCategoryToolStripMenuItem_Click(object sender, System.EventArgs e)
    {
        tvCategories.Nodes.Add(new TreeNode("category"));
        tvCategories.Nodes[tvCategories.Nodes.Count - 1].BeginEdit();
    }

The 1st line creates and adds the new node, what's important here is that you provide an initial string even if the user has to change it. Why? Not sure. But leaving the string part empty results in the above problem.

The 2nd line just selects the last node.

momo
  • 119
  • 3
  • 10