opening a file by clicking a treeview node

G

Guest

I added a treeview to my winform, based on an xml file.
I want that the treeview will act as a menu, so each time a user
click\select a node, a file will be opened. the pairs of node item - file is
defined in the xml file in the following example structure:

<?xml version="1.0" encoding="utf-8" ?>
<course id="2555" title="Developing Microsoft .NET Applications for Windows
(Visual C# .NET)" length="5 days"
source="http://www.microsoft.com/learning/syllabi/en-us/2555Afinal.mspx">
<module id="1" title="Introducing Windows Forms"
location="D:\Disk-C\Documents and Settings\user\My Documents\XML\c#">
<lesson id="1.1">
<subject>Creating a Form</subject>
<file>Introducing_Windows_Forms_Course-2555-Module-1.pdf</file>
</lesson>
<lesson id="1.2">
<subject>Adding Controls to a Form</subject>
<file>type&exceptions.pdf</file>
</lesson>
<lesson id="1.3">
<subject>Creating an Inherited Form</subject>
<file>winforms.pdf</file>
</lesson>
</module>
</course>

Can someone please assist?
(I'm using c#)

Thanks a lot
 
A

AlexS

You can assign file path to tree view node Tag property and use it when
clicked. Or selected.

HTH
Alex
 
G

Guest

Can you please further explain?

Thanks a lot

AlexS said:
You can assign file path to tree view node Tag property and use it when
clicked. Or selected.

HTH
Alex
 
A

AlexS

For example,

....
// set node tag to proper file
treeView.SelectedNode.Tag = fileNameFromXMLFile;
treeView.SelectedNode.Text = subjectFromXMLFile;
....

Then in proper event handler
// get file from node tag
string fileName = (string)treeView.SelectedNode.Tag;
....

You can find out which node was clicked e.g. with this code - see TreeView
and TreeNode classes in MS Help for details

// get node from mouse
private void treeView1_MouseDown(object sender,
System.Windows.Forms.MouseEventArgs e)
{
mySelectedNode = treeView1.GetNodeAt(e.X, e.Y);
if (mySelectedNode != null) {
// use file name from node
string fileName = (string)mySelectedNode.Tag
...
}
}

so, you can parse xml file and store detected file paths in proper nodes in
your tree.

HTH
 

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