Treeview selectednode problem/question

  • Thread starter Thread starter David Elliott
  • Start date Start date
D

David Elliott

Hi,

I have a Windows Explorer-like treeview that displays directories. After a
cut/paste operation, I want the treeview to display the folder where the
paste happened. But, I'm running into a problem following the assignment of
the selectednode in the treeview. Here's the relevant code:

// separate the node names
string [] split = mcurrentPath.Split(new Char [] {'\\'});

TreeNode currNode = null;

int currIndex = 0;

// find the Node for this node name
foreach (string s in split)
{
if (currIndex == 0)
// we're on the top-most node
currNode = tvwDirectory.TopNode;

// search the current node's nodes for this text
foreach (TreeNode n in currNode.Nodes)
{
if (n.Text == s)
{
currNode = n;
break;
}
}
currIndex++;
}

tvwDirectory.BeginUpdate();
tvwDirectory.SelectedNode = null;
tvwDirectory.Nodes.Clear();
FilltreeView();
tvwDirectory.SelectedNode = currNode;
tvwDirectory.SelectedNode.Expand(); <-- this is where I blow up!
tvwDirectory.EndUpdate();

I've looked at currNode using the debugger and it looks OK. But, on the
Expand() I get this error:
System.NullReferenceException: Object reference not set to an instance of
an object.

Any help will be most appreciated.

Thanks,
 
Hi, David

curnode cannot be for selection after you clear list of nodes in treeview.
It's not present anymore in treeview.

HTH
Alex
 
David said:
I have a Windows Explorer-like treeview that displays directories. After a
cut/paste operation, I want the treeview to display the folder where the
paste happened. But, I'm running into a problem following the assignment of
the selectednode in the treeview. Here's the relevant code:

// separate the node names
string [] split = mcurrentPath.Split(new Char [] {'\\'});

TreeNode currNode = null;

int currIndex = 0;

// find the Node for this node name
foreach (string s in split)
{
if (currIndex == 0)
// we're on the top-most node
currNode = tvwDirectory.TopNode;

// search the current node's nodes for this text
foreach (TreeNode n in currNode.Nodes)
{
if (n.Text == s)
{
currNode = n;
break;
}
}
currIndex++;
}

tvwDirectory.BeginUpdate();
tvwDirectory.SelectedNode = null;
tvwDirectory.Nodes.Clear();

^^^^^^^
this will clear all nodes. So whatever value currNode is pointing to, it's
not a node in the tree anymore.
FilltreeView();

^^^^^^^^
Here you add NEW nodes.
tvwDirectory.SelectedNode = currNode;

^^^^^^^^
Here you're setting the SelectedNode property to a node object that's NOT
part of the tree, as you cleared all.
tvwDirectory.SelectedNode.Expand(); <-- this is where I blow up!
tvwDirectory.EndUpdate();

I've looked at currNode using the debugger and it looks OK. But, on the
Expand() I get this error:
System.NullReferenceException: Object reference not set to an instance of
an object.

of course, it can't find the node in the tree. :)

FB
 
Hi Alex,

Thanks very much. That helped.

Regards,

David


Hi, David

curnode cannot be for selection after you clear list of nodes in
treeview. It's not present anymore in treeview.

HTH
Alex

David Elliott said:
Hi,

I have a Windows Explorer-like treeview that displays directories.
After a cut/paste operation, I want the treeview to display the
folder where the paste happened. But, I'm running into a problem
following the assignment of
the selectednode in the treeview. Here's the relevant code:

// separate the node names
string [] split = mcurrentPath.Split(new Char [] {'\\'});

TreeNode currNode = null;

int currIndex = 0;

// find the Node for this node name
foreach (string s in split)
{
if (currIndex == 0)
// we're on the top-most node
currNode = tvwDirectory.TopNode;

// search the current node's nodes for this text
foreach (TreeNode n in currNode.Nodes)
{
if (n.Text == s)
{
currNode = n;
break;
}
}
currIndex++;
}

tvwDirectory.BeginUpdate();
tvwDirectory.SelectedNode = null;
tvwDirectory.Nodes.Clear();
FilltreeView();
tvwDirectory.SelectedNode = currNode;
tvwDirectory.SelectedNode.Expand(); <-- this is where I blow up!
tvwDirectory.EndUpdate();

I've looked at currNode using the debugger and it looks OK. But, on
the Expand() I get this error:
System.NullReferenceException: Object reference not set to an
instance of an object.

Any help will be most appreciated.

Thanks,
 
Thanks Frans. You are correct on all counts.

I've changed it around a bit and it's working much better now.

Regards,

David


David said:
I have a Windows Explorer-like treeview that displays directories.
After a cut/paste operation, I want the treeview to display the
folder where the paste happened. But, I'm running into a problem
following the assignment of the selectednode in the treeview. Here's
the relevant code:

// separate the node names
string [] split = mcurrentPath.Split(new Char [] {'\\'});

TreeNode currNode = null;

int currIndex = 0;

// find the Node for this node name
foreach (string s in split)
{
if (currIndex == 0)
// we're on the top-most node
currNode = tvwDirectory.TopNode;

// search the current node's nodes for this text
foreach (TreeNode n in currNode.Nodes)
{
if (n.Text == s)
{
currNode = n;
break;
}
}
currIndex++;
}

tvwDirectory.BeginUpdate();
tvwDirectory.SelectedNode = null;
tvwDirectory.Nodes.Clear();

^^^^^^^
this will clear all nodes. So whatever value currNode is pointing
to, it's
not a node in the tree anymore.
FilltreeView();

^^^^^^^^
Here you add NEW nodes.
tvwDirectory.SelectedNode = currNode;

^^^^^^^^
Here you're setting the SelectedNode property to a node object
that's NOT
part of the tree, as you cleared all.
tvwDirectory.SelectedNode.Expand(); <-- this is where I blow up!
tvwDirectory.EndUpdate();

I've looked at currNode using the debugger and it looks OK. But, on
the Expand() I get this error:
System.NullReferenceException: Object reference not set to an
instance of an object.

of course, it can't find the node in the tree. :)

FB
 
Back
Top