C# - TreeView SelectedNode question

EFileTahi-A

Contributor
Joined
Aug 8, 2004
Messages
623
Location
Portugal / Barreiro
Am trying to make my treeView to select (check) automaticaly all the parent nodes from which the selected node belongs to, unfortunately, I'm having trouble to do this. For starters, I can't even retrieve the current selected node by using "this.myTreeView.SelectedNode" property.

It keeps saying me that the "object was not set in an property index" or something like that.

So, How can I say to the treeview to select all the parent nodes from the current selected one?

Than you...
 
EFileTahi-A said:
For starters, I can't even retrieve the current selected node by using "this.myTreeView.SelectedNode" property.

It keeps saying me that the "object was not set in an property index" or something like that.

The SelectedNode property is a reference to the currently selected node, so if no node is selected, it's a null reference. So...

The most likely cause of this is that either your treeview has no nodes or there is no node selected from the treeview's nodes. Either way, it's a good idea to check the SelectedNode property to see if it's null (not currently holding a reference to a node) before using it, for ex:

Code:
private void button1_Click(object sender, System.EventArgs e)
{
	TreeNode oNode = this.treeView1.SelectedNode;
	
	if (oNode != null)
	{
		SelectNodeAndParents(oNode);
	}
	else
	{
		MessageBox.Show("You must select a node first");
	}
}

EFileTahi-A said:
So, How can I say to the treeview to select all the parent nodes from the current selected one?

Each node has a Parent property that contains a reference to its parent node - or null if the node has no parent (ie, it's a top level node).

To select a node and all of its parents you'd need to select the node, see if it had a parent and select it, see if the parent had a parent and select, and so on. This is something you can so in a loop (somewhat recursively). For example:

Code:
private void SelectNodeAndParents(TreeNode oNode)
{
	oNode.Checked = true;
	//oNode.Checked = !oNode.Checked;  //  Use this if you want to toggle.

	TreeNode oParent = oNode.Parent;

	while (oParent != null)
	{
		oParent.Checked = true;
		//oParent.Checked = !oParent.Checked;  //  Use this if you want to toggle.

		oParent = oParent.Parent;
	}
}

In the routine above a node is passed in, the node itself is checked, and then a loop executes that keeps selecting parent nodes as long as they exist (oParent is initially set to the parent of the node that's passed in, then it's set to the next parent in line as long as the loop continues - the loop keeps going until it encounters a parent that's null, which indicates a top level node and end of the line).

I've attached the source (only) of a scrappy sample project that uses the code above on some sample nodes if you'd care to see this working...

Good luck,
Paul
 

Attachments

Hi PWnettle, tks for ur post.

Of course I had my treeView full of nodes and of course I know I had to select one node first in order to the "supposely" this.myTreeView.SelectedNode property to work.

The problem was when I selected a node with the mouse. A mouseUp or mouseClick event would trigger the following line of code once a node was selected:

MessageBox.Show(this.treeView.SelectedNode.ToString());

Unfortunatly it would reads always NULL (after I select a node) and would give me the error I discribed in my first post, which I have no ideia why until I read your post!

Now I know I have first to declare a TreeNode object to get the info from the TreeView.SelectedNode object :)

Tks once more buddy!

- THREAD CLOSED -
 
Last edited:
EFileTahi-A said:
Now I know I have first to declare a TreeNode object to get the info from the TreeView.SelectedNode object :)

I don't think that's really true...it's more of a stylistic thing...

This:

Code:
private void button1_Click(object sender, System.EventArgs e)
{
	TreeNode oNode = this.treeView1.SelectedNode;
	
	if (oNode != null)
	{
		SelectNodeAndParents(oNode);
	}
	else
	{
		MessageBox.Show("You must select a node first");
	}
}

And this:

Code:
private void button1_Click(object sender, System.EventArgs e)
{
	if (this.treeView1.SelectedNode != null)
	{
		SelectNodeAndParents(this.treeView1.SelectedNode);
	}
	else
	{
		MessageBox.Show("You must select a node first");
	}
}

...should be the same. I've just adjusted to the style most commonly used where I work - we tend to create a simple reference (like 'TreeNode oNode' in the first snippet) if we're going to refer to a reference often rather than using the more lengthy references (like 'this.treeView1.SelectedNode), because our custom objects can often have some lengthy names, especially when dealing with multiple levels of subobjects.

But anyways, if it works it's all good - and apologies if I insulted your intelligence with the bit about populating and making sure a node is selected - I was only attempting to be thorough!

Pardon the rambling. ;)

Paul
 
Back
Top