473,320 Members | 1,951 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

How to run any file from treeview

133 100+
I have searched the net and i cannot find any code that will run any file from treeview , I searched microsoft site and it only tells me how to create nodes in treeview. I have no code to give you because i cant find any so if anyone would have an idea please point me in the right direction.

Thanks
Gobble.
Jun 1 '09 #1
12 4188
tlhintoq
3,525 Expert 2GB
"Run a file from Treeview" ??

A treeview doesn't contain a file. It contains nodes. What those nodes represent is up to you.

Expand|Select|Wrap|Line Numbers
  1. + C:\TextFiles
  2. |-- + A
  3.       |-- Apple.txt
  4.       |-- Ardvark.jpg
  5. |-- One.txt
  6. |-- Two.txt
  7.  
So in some way you need to translate whatever the text of your TreeView.Nodes collection item is to a path that leads to the file.

Once you have that path... What do you mean by 'run'? Is your treeview items filled with paths to executables that can run? Batch files? Or when you say 'run' do you mean 'load' such as load the data of a text file, or the image of a jpg?
Jun 1 '09 #2
gobblegob
133 100+
thanks for the reply , I do mean load. Basically the treeview is a win explorer clone that is specific to a folder. so i am looking for a way of getting the links of the nodes, i will keep trying
Jun 2 '09 #3
tlhintoq
3,525 Expert 2GB
@gobblegob
Ok. Then each node is a file name inside a folder?

Expand|Select|Wrap|Line Numbers
  1. + C:\files
  2.      |-- Accounts.txt
  3.      |-- ToDo.txt
  4.      |-- Dogs.jpg
  5.  
and so on?

So you just need to build the full path of the file, then load that.

Expand|Select|Wrap|Line Numbers
  1. NewFilePath = ThisNode.Parent.text + @"\" + ThisNode.text;
Should get you C:\files\ToDo.txt when enacted on the right node. You'll have to adjust the variable names to your application of course.

Hollar if you get stuck.
Jun 2 '09 #4
gobblegob
133 100+
Thanks for the help but i've changed it around all the way i could think of and
i get an error vb error "cannot file file name" so i must be getting closer.

Expand|Select|Wrap|Line Numbers
  1.         Dim OpenApp As String
  2.  
  3.         OpenApp = TreeView1.Parent.Text + "\" + TreeView1.Text
  4.  
  5.         Process.Start(OpenApp)
Jun 4 '09 #5
tlhintoq
3,525 Expert 2GB
I'm not super literate in VB. Is "\" legal?
In C# the \ is used to indicate a special character such as \n for new line, \t for tab. So you either have to type "\\" or @"\"

"cannot file file name"
Do you mean "cannot find file {name}"

Put a breakpoint at line five and make sure the path OpenApp is valid and correctly goes to an application. You should be able to copy its value and paste it into a 'RUN" dialog and have it work.
Jun 4 '09 #6
gobblegob
133 100+
i am still getting error "the system cannot find the file specified"
looks fine to me hmmm

Expand|Select|Wrap|Line Numbers
  1.         For Each file As ListViewItem In ListView1.Items
  2.             Dim filePath As String = file.SubItems(0).Text & "\" & file.Text
  3.                     Process.Start(filePath)
  4.             End If
  5.         Next
Any ideas?
Jun 5 '09 #7
tlhintoq
3,525 Expert 2GB
One should always assume the worst in code... Nothing will work... Nothing will exist that you expect... The user will always enter things wrong, or at least differently than you dexpected...

Expand|Select|Wrap|Line Numbers
  1. If (Path.FileExists(filePath)) Process.Start(filePath);
  2. else Console.WriteLine(string.format("Couldn't find file '{0}' ", filePath));
This not only avoids the error if the file doesn't exist, but will output the exact string it's looking for when it fails.
Jun 5 '09 #8
gobblegob
133 100+
Thanks for the help mate but im going insane with this. i cannot work out how i can do this i will paste all i have and if anyone can see what the problem is please let me know.

i cant work out how i can load what ever item doubleclick on in listview1

Expand|Select|Wrap|Line Numbers
  1.     Private Sub AddAllFolders(ByVal TNode As TreeNode, ByVal FolderPath As String)
  2.         Try
  3.             For Each FolderNode As String In Directory.GetDirectories(FolderPath)
  4.                 Dim SubFolderNode As TreeNode = TNode.Nodes.Add(FolderNode.Substring(FolderNode.LastIndexOf("\"c) + 1))
  5.                 SubFolderNode.Tag = FolderNode
  6.                 SubFolderNode.Nodes.Add("Loading...")
  7.             Next
  8.         Catch ex As Exception
  9.             MsgBox(ex.Message)
  10.         End Try
  11.     End Sub
  12.  
  13.  
  14.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  15.         Dim Tnode As TreeNode = TreeView1.Nodes.Add("(Documents Quick Link Menu:)")
  16.         AddAllFolders(Tnode, Application.StartupPath + "\Documents Quick Link Menu\")
  17.  
  18.     End Sub
  19.  
  20.  
  21.     Private Sub Treeview1_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect
  22.         ListView1.Items.Clear()
  23.         If TreeView1.SelectedNode.Nodes.Count = 1 AndAlso TreeView1.SelectedNode.Nodes(0).Text = "Loading..." Then
  24.             TreeView1.SelectedNode.Nodes.Clear()
  25.             AddAllFolders(TreeView1.SelectedNode, CStr(TreeView1.SelectedNode.Tag))
  26.         End If
  27.         ListView1.Clear()
  28.         ListView1.View = View.Details
  29.         ListView1.Columns.Add("File Name", 150, HorizontalAlignment.Left)
  30.         Dim folder As String = CStr(TreeView1.SelectedNode.Tag)
  31.         If Not folder Is Nothing AndAlso IO.Directory.Exists(folder) Then
  32.             Try
  33.                 For Each file As String In IO.Directory.GetFiles(folder)
  34.                     ListView1.Items.Add(file.Substring(file.LastIndexOf("\"c) + 1), file.ToString())
  35.                 Next
  36.             Catch ex As Exception
  37.                 MsgBox(ex.Message)
  38.             End Try
  39.         End If
  40.     End Sub
  41.  
  42.  
  43.     Private Sub Treeview1_BeforeExpand(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewCancelEventArgs) Handles TreeView1.BeforeExpand
  44.         If e.Node.Nodes.Count = 1 AndAlso e.Node.Nodes(0).Text = "Loading..." Then
  45.             e.Node.Nodes.Clear()
  46.             AddAllFolders(e.Node, CStr(e.Node.Tag))
  47.         End If
  48.     End Sub
  49.  
  50.  
  51.     Private Sub ListView1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListView1.DoubleClick
  52.         If ListView1.SelectedItems.Count = 0 Then Exit Sub
  53.         Process.Start(ListView1.SelectedItems(0).Tag)
  54.      End Sub
Jun 7 '09 #9
FishVal
2,653 Expert 2GB
  • Full path is built from target file parent folder path which is obtained from TreeView and file name which is obtained from ListView.
  • Depending on functionality provided by TreeView class there could be a method/property returning path delimited with separators to a specific node (as for example in MSComCtl32.TreeView) or in worst case you'll need to design a custom recursive procedure iterating nodes from a selected one to root node.

Regards,
Fish.
Jun 7 '09 #10
tlhintoq
3,525 Expert 2GB
I think if you don't try to condense the loading code, you will have a little better idea of what it is trying to load. My if...else construct below is C#. You may have adjust for VB

Expand|Select|Wrap|Line Numbers
  1.     Private Sub ListView1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListView1.DoubleClick
  2.         If ListView1.SelectedItems.Count = 0 Then Exit Sub
  3.         Process.Start(ListView1.SelectedItems(0).Tag)
  4.      End Sub
Don't presume the file must exist. That is why you get a crash with file not found. Check to see if hte file exists, and only proceed if it does. If it does not exist, then open a messagebox telling you it doesn't exist and the exact name of what it tries to open. Right here you may find a descrepancy in the file name: It may not be what you thought it would be.
Expand|Select|Wrap|Line Numbers
  1.     Private Sub ListView1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListView1.DoubleClick
  2.         If ListView1.SelectedItems.Count = 0 Then Exit Sub
  3.  
  4. Dim FileName as string = ListView1.SelectedItems(0).Tag
  5. if (System.IO.File.Exists(FileName)
  6. {
  7.         Process.Start(ListView1.SelectedItems(0).Tag)
  8. }
  9. else
  10. {
  11. MessageBox.Show(FileName,"File Not Found");
  12. }
  13.      End Sub

Please try this much and let me know exactly the full path of the file not found as reported by the messagebox.
Jun 7 '09 #11
gobblegob
133 100+
ok i tried this and the message box it empty.

Expand|Select|Wrap|Line Numbers
  1.         If ListView1.SelectedItems.Count = 0 Then Exit Sub
  2.  
  3.         Dim FileName As String = ListView1.SelectedItems(0).Tag
  4.         If (System.IO.File.Exists(FileName)) Then
  5.  
  6.             Process.Start(ListView1.SelectedItems(0).Tag)
  7.  
  8.         Else
  9.  
  10.             MessageBox.Show(FileName, "File Not Found")
  11.         End If
Jun 8 '09 #12
tlhintoq
3,525 Expert 2GB
@gobblegob
Working backwards we learned:
Line 10: Therefore 'FileName' was empty...
Line 3 : Therefore "ListView1.SelectedItem(0).Tag is empty

Wait... "ListView1"? ListView? I thought this question was about running from a TreeView.


May I make a suggestion? Start simply and complicate your program after the basics work. I don't completely understand what you are trying to do with the ListView but I would think that you should first worry about making things work with one control: The TreeView you originally wrote about. Worry about keeping the TreeView and your ListView controls synchronized after you've made the first control work.

With the two controls intermingled as they are you have many places that could cause a problem.
Are you setting the TreeNode.Tag with wrong values
Are you reading the TreeNode.Tag wrongly
Are you not keeping the TreeNode and the ListView synchronized
Are you not passing from TreeNode to ListView correctly
and so on.

Start with just getting the TreeNode.Tags to contain the right paths to the files, and getting them to launch from the TreeNode. Once that works, then add in other features like a multicolumn ListView that displays additional details of your file.

Or, start with a ListView that you hardcode values in. Get those paths to launch. Then add in the nice TreeView.

Pick one or the other as a place to start.
Jun 8 '09 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Macca | last post by:
Im am writing an application that will looks similar to Outlook or a help file, i.e tree structure on left with right hand side pane showing data. It is split into two panes.The Left pane has a...
1
by: NagaKiran | last post by:
Hi I am able to display XML contents in a treeview. But I am unable to create an XML file using treeview data. I have a TreeView control wich contains many levels of data.I want create write...
6
by: Alan Silver | last post by:
Hello, I want to show a treeview display of the contents of a sitemap file on another web site, chosen according to the currently logged on user. I know at run time the path to the sitemap file,...
0
by: Patrick | last post by:
Hi, Im trying to populate my treeview control onmy webpage with the information that is stored in a xml file, but i cant seem to find any good references about it. The xml file looks like this. ...
0
by: Christian Rühl | last post by:
Good Morning, I have generated a TreeView from a DOM. Now I want to set the checkboxes according to an ini file I've written. The ini file looks like this: .... Dsr=1 version=1...
1
by: Christian Rühl | last post by:
hey! what i wanna do sounds very simple at first, but it turned out to be a real bone crusher... i want to check if a treeView node is checked and if a correspondent node in my xml config file...
1
by: Christian Rühl | last post by:
hey! what i wanna do sounds very simple at first, but it turned out to be a real bone crusher... i want to check if a treeView node is checked and if a correspondent node in my xml config file...
6
by: | last post by:
Hi, I'm steel trying to read and update my XML file with Visual Basic Express but i am unable to find the right way to read my xml file and update it if neccessary... Here is my problem :...
6
by: star-italia | last post by:
Hi, Is it possible to include a XAML file into another XAML file? For example If i have - Window1.xaml - Window2.xaml and both have a treeview with a custom Style, can i describe the style...
12
by: minimalniemand | last post by:
I try to recursivley fill a treeview from a xml-file. It works well, when the contents of the xml and the treeview match 1:1. code here: http://mad-scientists.co.uk/micha/shownode.txt a sample...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.