I'm trying to actually deselect all nodes in my TreeView. By "actually", I mean that TreeView1.SelectedNode = null;
will actually deselect the node in the tree.
Right now, visually speaking, the node is deselected. However, when I try to add a new node, the treeview will automatically select the first node in the tree (at the top) and create a subnode when ideally I want to create a parent node. I can't just deselect the selected node before adding either, because the user might want to add a child node. The behaviour I'd like is for the parent/child node adding to be based on what is selected in the treeview. If nothing is selected, add a parent, if something is selected, add a child in that selected node.
I construct a TreeNode
object called node
in a function with images and text and all that then I have the following:
if (tvContent.SelectedNode == null)
tvContent.Nodes.Add(node);
else
{
tvContent.SelectedNode.Nodes.Add(node);
tvContent.SelectedNode.Expand();
}
I have a "Deselect All" button that is supposed to make the above code work. The code for that button is simply:
tvContent.SelectedNode = null;
Pardon my tagging both C# and VB.NET. I'm good with both so if someone can help me out in either language that'd be terrific!
Thanks
EDIT:
Interesting. It seems that while testing if the selected node is null, .NET will automatically set the selected node to the first node in the tree. The following code shows the "trigger" message box, but immediately selects the first node in the tree after the if
statement is complete.
private void btnDeselectAll_Click(object sender, EventArgs e)
{
tvContent.SelectedNode = null;
if (tvContent.SelectedNode == null) MessageBox.Show("trigger");
}
EDIT2: The issue lies in using an InputBox for the title input of the node. For whatever reason, that changes the selected node of the treeview. I tried this in a stock project and managed to replicate the issue. I guess there's no fixing this :S