Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi
I have a browse button and text box and when i click on that browse button file dialog box is opening and now my concern is the selected path for that file should be opened in tree view box.. can you please help me in that..
Posted
Updated 10-Nov-14 23:14pm
v2
Comments
Richard MacCutchan 11-Nov-14 5:22am    
Please explain what you mean by "the selected path for that file should be opened in tree view box".
Karthik Bilakanti 11-Nov-14 5:26am    
I want to open an excel sheet in tree view control using open file dilog box thats my issue.
Richard MacCutchan 11-Nov-14 5:35am    
Fine, but as a technical explanation of your problem that makes no sense.

The OpenFile dialog is used to obtain a file path from the user. You can then read that file in various different ways depending on its content. Similarly you can add data to a TreeView in different ways. You need to read the documentation for all these classes to see how they fit with your requirements.
Karthik Bilakanti 11-Nov-14 5:45am    
Thank you

1 solution

Get the file name from the OpenFileDialog.FileName property, and use string.Split to break it into it's component folders with the backslash character.

Then loop through each string in the array Split returns, creating a new node for each and add it into the Nodes property of the previous one.
C#
OpenFileDialog ofd = new OpenFileDialog();
if (ofd.ShowDialog() == DialogResult.OK)
    {
    string[] parts = ofd.FileName.Split('\\');
    TreeNodeCollection previousNodes = myTreeView.Nodes;
    foreach (string part in parts)
        {
        TreeNode node = new TreeNode(part);
        previousNodes.Add(node);
        previousNodes = node.Nodes;
        }
    myTreeView.ExpandAll();
    }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900