Re: Reloading the treeView and expanding the last selected node in C#

J

Jeff Gaines

How Reloading the treeView and expanding the last selected in C# ?

Currently i am doing

TreeNode selected = treeview.SelectedNode;
this.LoadTreeView();
selected.Expand();

I think I would do it the other way round - trap BeforeExpand and fill the
node there.
 
J

Jeff Gaines

Hi Jeff,

Thanks, could u please explain me in detail. I am doing as u told, but not
succeed.

I'll try :)

If you have a function:

void MyTreeView_BeforeExpand(object sender, TreeViewCancelEventArgs e)
{
// Clear old node
e.Node.Nodes.Clear();
// Call your function to fill the node being expanded
FillNode(e.Node);
}

void FillNode(TreeNode tnParent)
{
TreeNode tnNew;
// Get the key
int index = (int)tnParent.Tag;
// Get the records to fill the Node

foreach(record recTemp in records)
{
// Create Node
tnNew = new TreeNode(recTemp.name);
// Save key in Tag
tnNew.Tag = recTemp.index;
tnParent.Nodes.Add(tnNew);
}
}

You get the Node being expanded in e.Node - you will need to keep a key of
some sort in the Tag of the Node so you know which node you are filling.
You'll have to edit the FillNode function so it gets the appropriate
records. You also only have to write it once - add your root node when you
form loads then it will call this function when it is expanded.
The only caveat is that you will need to add a dummy node to get the '+'
sign in the TreeView, if so you should clear the node before you call the
FillNode function.
 
G

Guest

Hi Jeff,

Actually i am doing like this

void LoadTreeView()
{
treeview.Nodes.Clear();
//This Method adds the nodes in treeview
// In this method passing the xmlDocument
addItem(xmlDocument.DocumentElement, null);
}



void BeginRefreshTreeView()
{
TreeNode selected = treeview.SelectedNode;
this.LoadTreeView(); //Calling Above LoadTreeView()
method here
if (selected != null)
{
xmltree.SelectedNode = selected;
selected.Expand();
}
}

Is there any modification needs in code for as what i m desiring ?
 
C

Cyril Gupta

Hello JaiPrakash,
Hmm... I don't really know if that code would work for you, because it seems
you are clearing the Treeview and the node that you have reference to might
just not exist when you access it again.

If the contents of the treeview is going to remain the same after the refresh,
I recommend that you use the Index property instead of taking the reference
to the actual node.

What exactly are you trying to do here? Can it be done without a refresh?

Regards,
Cyril Gupta

JS> Hi Jeff,
JS>
JS> Actually i am doing like this
JS>
JS> void LoadTreeView()
JS> {
JS> treeview.Nodes.Clear();
JS> //This Method adds the nodes in treeview
JS> // In this method passing the xmlDocument
JS> addItem(xmlDocument.DocumentElement, null);
JS> }
JS> void BeginRefreshTreeView()
JS> {
JS> TreeNode selected = treeview.SelectedNode;
JS> this.LoadTreeView(); //Calling Above
JS> LoadTreeView()
JS> method here
JS> if (selected != null)
JS> {
JS> xmltree.SelectedNode = selected;
JS> selected.Expand();
JS> }
JS> }
JS> Is there any modification needs in code for as what i m desiring ?
JS>
JS> "Jeff Gaines" wrote:
JS>
 
G

Guest

Hi Cyril and Jeff,

I got success to do this thing by using only single line.

treeview.SelectedNode = treeview.Nodes.Find(selected.Name, true)[0];
means code will be as below

TreeNode selected = treeview.SelectedNode;
this.LoadTreeView();
if (selected != null)
{
//Find the child node in treeview and expand at that node.
treeview.SelectedNode = xmltree.Nodes.Find(selected.Name,
true)[0];
treeview.SelectedNode.Expand();
}

In this line i am set the value of selected.Name in my addItem() method.

Thank you.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top