Why is it that treeView1.SelectedNode points to a node and is not nulled, when before you already have "treeView1.SelectedNode = null" executed?
Its seems that
treeView1.SelectedNode = null
does not work, is it? But before, 开发者_StackOverflow中文版in sometime through my development, it worked, SelectedNode is nulled and suddenly it doesn't.
You can only set the SelectedNode to null if the tree view doesn't have the focus. As soon as it gets the focus back, the control is going to select the node again. For example:
private void button1_Click(object sender, EventArgs e) {
treeView1.SelectedNode = null;
if (treeView1.SelectedNode == null) Console.WriteLine("okay");
treeView1.Focus();
if (treeView1.SelectedNode != null) Console.WriteLine("okay");
}
Output:
okay
okay
This is by design, the native TreeView control really likes having a selection.
It's easy to debug this.
treeView1.SelectedNode = null;
Debug.Assert(treeView1.SelectedNode == null);
Put it in Shown
event of yourForm
private void Form1_Shown(object sender, EventArgs e)
{
treeView1.SelectedNode = null;
}
精彩评论