michivo

I'm experiencing a weird problem with a System.Windows.Forms.TreeView. After the following few lines of code:
TreeNode myNode = new TreeNode("foo");
treeView.Nodes.Clear();
treeView.Nodes.Add(myNode);
treeView.SelectedNode = treeView.Nodes[0];

treeView.SelectedNode is null. I'm doing a whole lot of things with my treeView before that, so I don't know what messed it up. Anyway, after clearing it, adding one Node and setting SelectedNode to this one node, it's not supposed to be null, right Apart from the fact that SelectedNode is always null, everything's just fine with the treeView...
I'm pretty sure it's not a thread-issue as I used Invoke to call the method containing the snippet above. I'm also on the same thread the tree was created on and there's only this one thread running at this time.
Does anyone have an idea what could be causing this problem


Re: Windows Forms General TreeView: SelectedNode always null?!

Alan Robbins

Michivo

Where is the code snippet you posted located I've had a similar issue with the tree when I tried to select a node in a class derived from tree node's constructor -- because the tree's node collection was being modified it wouldn't let me select until that modification was over.

HTH






Re: Windows Forms General TreeView: SelectedNode always null?!

nobugz

Well, that's certainly supposed to work. What doesn't add up: "I used Invoke" and "there's only this one thread running". You should at least have two threads running if you used Control.Invoke(). Show your code that calls Invoke.





Re: Windows Forms General TreeView: SelectedNode always null?!

michivo

I was pretty desperate, that's why I tried using Invoke. After that I saw that there's only one thread running anyway so it wouldn't have been necessary.
Anyway, it doesn't make a difference if I use Invoke or not. I can access treeView.Nodes[0] as usual, I just can't set the treeView.SelectedNode.
If I create a new tree instead of just clearing it, SelectedNode is not null, so something must be messed up with the treeview.

So, after



Re: Windows Forms General TreeView: SelectedNode always null?!

michivo

OK, I guess I found the solution: The snippet above is triggered by clicking a Node in the tree. Apparently I can't change the selected node when clicking the tree. An exception telling me that fact would've been nice instead of just having a Selected Node that is null ;)




Re: Windows Forms General TreeView: SelectedNode always null?!

nobugz

Your original treeview had hottracking enabled and was sorted, your new one doesn't. TreeView often misbehaves when you alter selections or nodes in one of its event handlers. What causes your code to run





Re: Windows Forms General TreeView: SelectedNode always null?!

plinko

I've just run into this same problem with the treeview. Is there any way to tell when the selected node can be set I dont mean testing for null, but what conditions prevent the selectednode from being set





Re: Windows Forms General TreeView: SelectedNode always null?!

nobugz

Selecting a node in a click event doesn't work. Check this thread for a trick that uses Control.BeginInvoke().





Re: Windows Forms General TreeView: SelectedNode always null?!

plinko

Thanks for the reply. I'm not trying to set the selected node in any event procedure.

However, I am trying to set the selected node right after my code has finished populating the treeview. (the node i am trying to select is valid and has been loaded at this point)

Would this be the problem Is there an event that fires after the treeview has finished populating itself (and ready to accept a selectednode )





Re: Windows Forms General TreeView: SelectedNode always null?!

nobugz

I can't reproduce your problem. This worked fine:

public partial class Form1 : Form {
public Form1() {
InitializeComponent();
treeView1.Nodes.Add("one");
treeView1.Nodes.Add("two");
treeView1.SelectedNode = treeView1.Nodes[1];
}
}





Re: Windows Forms General TreeView: SelectedNode always null?!

plinko

Here is a pared down version of what I'm trying to do. Form has a single treeeview and a button.

Code Snippet

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Me.Load

Call PopulateTreev()

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim FineMe As String = "F3"

Dim TVNodeArr As TreeNode()

Dim TN As TreeNode

Dim MySelectedtreeNod As TreeNode = Nothing

TVNodeArr = TreeView1.Nodes.Find(FineMe, True)

If TVNodeArr.GetLength(0) > 0 Then

For Each TN In TVNodeArr

'code to decide which one is the one i want

MySelectedtreeNod = TN

Exit For

Next

End If

If IsNothing(MySelectedtreeNod) = True Then

MsgBox("nothing found, cant select")

Else

Call PopulateTreev(MySelectedtreeNod)

End If

End Sub

Sub PopulateTreev(Optional ByVal TheNodeSelected As TreeNode = Nothing)

Dim MainNode As New TreeNode

Dim Nodd As TreeNode

Dim NoddChild As TreeNode

TreeView1.Nodes.Clear()

MainNode.Text = "Main Happy Node"

TreeView1.HideSelection = False

Nodd = New TreeNode("Fonzie")

Nodd.Name = "aaaaaaaa"

NoddChild = New TreeNode("Fonzies Children")

NoddChild.Nodes.Add("F1", "Fonz 1")

NoddChild.Nodes.Add("F2", "Fonz 2")

NoddChild.Nodes.Add("F3", "Fonzette 3")

NoddChild.Nodes.Add("F4", "Fonz 4")

Nodd.Nodes.Add(NoddChild)

MainNode.Nodes.Add(Nodd)

Nodd = New TreeNode("Chachi")

Nodd.Name = "whaa"

NoddChild = New TreeNode("Chachis Children")

NoddChild.Nodes.Add("C1", "Ralph 1")

NoddChild.Nodes.Add("C2", "Joanie 2")

Nodd.Nodes.Add(NoddChild)

MainNode.Nodes.Add(Nodd)

TreeView1.Nodes.Add(MainNode)

If Not IsNothing(TheNodeSelected) Then

TreeView1.SelectedNode = TheNodeSelected

MsgBox("Selected Node Specified: " & TheNodeSelected.Text.ToString)

MsgBox("Treeview.Selectednode: " & TreeView1.SelectedNode.Text.ToString)

End If

End Sub

End Class

If I remove the TreeView1.Nodes.Clear() line, the code will select the correct node in the 'old' node.

But in my real program I have to dynamically (re)populate the treeview as the user interacts with the other parts of the program. So looking at this, seems that the Node that results from my 'Find' is connected to the 'old' node. And even if i re-load the treeview with the exact same information, the re-loaded node is somehow different than the old one. Is this correct

Is there a property or such that I can adjust so that that my 'Find' node will associate with the new node

Thanks for reading!





Re: Windows Forms General TreeView: SelectedNode always null?!

nobugz

The problem is that you are trying to select a node that is no longer in the tree. You removed it when you call Clear(). Just re-execute the Find().





Re: Windows Forms General TreeView: SelectedNode always null?!

plinko

Not in the tree based on what, though If it has the same name, fullpath, tag, etc, what gives it it's unique identity

(your suggestion works, thanks. I just want to usderstand how this darn control works)





Re: Windows Forms General TreeView: SelectedNode always null?!

nobugz

Its object reference, MySelectedTreeNod in your case.