|
-
Apr 27th, 2013, 03:29 AM
#1
Thread Starter
Member
[RESOLVED] Populate label with full file path from listview item
I am attempting my first project in VB to create a program that simplifies what I do at work. So far I have everything I need working correctly except my treeview/listview function I am building. What I need is to be able to open a file using specific programs. I have already done this using the "openFileDialog" to populate a full filename into a label which I then run "process.Start" off of.
I would like to make life even easier by adding a treeview explorer to populate a listview containing ONLY certain files with extensions that I actually need. Which I have already done. Right now I have it set to only show .cue, .ddp, and .iso. (Yes I know running the loop multiple times to get multiple extensions is "dirty" but it works)
When I double click a file in listview I want the full file path +name to populate the same label (FilePathLabel) so I can then run the file using my "Process.Start" buttons.
I hope this makes sense. I will paste my code below as well as attach an image of my form. I am very much new to this but I am trying. PS I am getting no credit for this at work besides eliminating about 7 desktop icons and 2-3 open folders on my screen at any given time.
I know the process.start buttons aren't yet linked to the programs yet. I want to get over this last hurdle first before I go any further.
Code:
Imports System.IO
Imports System.Runtime.InteropServices
Public Class Form1
Private Structure SHFILEINFO
Public hIcon As IntPtr ' : icon
Public iIcon As Integer ' : icondex
Public dwAttributes As Integer ' : SFGAO_ flags
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=260)> _
Public szDisplayName As String
<MarshalAs(UnmanagedType.ByValTStr, SizeConst:=80)> _
Public szTypeName As String
End Structure
Private Declare Auto Function SHGetFileInfo Lib "shell32.dll" _
(ByVal pszPath As String, _
ByVal dwFileAttributes As Integer, _
ByRef psfi As SHFILEINFO, _
ByVal cbFileInfo As Integer, _
ByVal uFlags As Integer) As IntPtr
Private Const SHGFI_ICON = &H100
Private Const SHGFI_SMALLICON = &H1
Private Const SHGFI_LARGEICON = &H0 ' Large icon
Private Const MAX_PATH = 260
Private Sub AddImages(ByVal strFileName As String)
Dim shInfo As SHFILEINFO
shInfo = New SHFILEINFO()
shInfo.szDisplayName = New String(vbNullChar, MAX_PATH)
shInfo.szTypeName = New String(vbNullChar, 80)
Dim hIcon As IntPtr
hIcon = SHGetFileInfo(strFileName, 0, shInfo, Marshal.SizeOf(shInfo), SHGFI_ICON Or SHGFI_SMALLICON)
Dim MyIcon As Drawing.Bitmap
MyIcon = Drawing.Icon.FromHandle(shInfo.hIcon).ToBitmap
ImageList1.Images.Add(strFileName.ToString(), MyIcon)
End Sub
Private Sub AddAllFolders(ByVal TNode As TreeNode, ByVal FolderPath As String)
Try
For Each FolderNode As String In Directory.GetDirectories(FolderPath)
Dim SubFolderNode As TreeNode = TNode.Nodes.Add(FolderNode.Substring(FolderNode.LastIndexOf("\"c) + 1))
SubFolderNode.Tag = FolderNode
SubFolderNode.Nodes.Add("Loading...")
Next
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
Private Sub Treeview1_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect
Dim FileExtension As String
Dim SubItemIndex As Integer
Dim DateMod As String
ListView1.Items.Clear()
If TreeView1.SelectedNode.Nodes.Count = 1 AndAlso TreeView1.SelectedNode.Nodes(0).Text = "Loading..." Then
TreeView1.SelectedNode.Nodes.Clear()
AddAllFolders(TreeView1.SelectedNode, CStr(TreeView1.SelectedNode.Tag))
End If
Dim folder As String = CStr(TreeView1.SelectedNode.Tag)
If Not folder Is Nothing AndAlso IO.Directory.Exists(folder) Then
Try
For Each file As String In IO.Directory.GetFiles(folder, "*.cue*")
FileExtension = IO.Path.GetExtension(file)
DateMod = IO.File.GetLastWriteTime(file).ToString()
AddImages(file)
ListView1.Items.Add(file.Substring(file.LastIndexOf("\"c) + 1), file.ToString())
ListView1.Items(SubItemIndex).SubItems.Add(FileExtension.ToString() & " File")
ListView1.Items(SubItemIndex).SubItems.Add(DateMod.ToString())
SubItemIndex += 1
Next
Catch ex As Exception
MsgBox(ex.Message)
End Try
End If
If Not folder Is Nothing AndAlso IO.Directory.Exists(folder) Then
Try
For Each file As String In IO.Directory.GetFiles(folder, "*.ddp*")
FileExtension = IO.Path.GetExtension(file)
DateMod = IO.File.GetLastWriteTime(file).ToString()
AddImages(file)
ListView1.Items.Add(file.Substring(file.LastIndexOf("\"c) + 1), file.ToString())
ListView1.Items(SubItemIndex).SubItems.Add(FileExtension.ToString() & " File")
ListView1.Items(SubItemIndex).SubItems.Add(DateMod.ToString())
SubItemIndex += 1
Next
Catch ex As Exception
MsgBox(ex.Message)
End Try
End If
If Not folder Is Nothing AndAlso IO.Directory.Exists(folder) Then
Try
For Each file As String In IO.Directory.GetFiles(folder, "*.iso*")
FileExtension = IO.Path.GetExtension(file)
DateMod = IO.File.GetLastWriteTime(file).ToString()
AddImages(file)
ListView1.Items.Add(file.Substring(file.LastIndexOf("\"c) + 1), file.ToString())
ListView1.Items(SubItemIndex).SubItems.Add(FileExtension.ToString() & " File")
ListView1.Items(SubItemIndex).SubItems.Add(DateMod.ToString())
SubItemIndex += 1
Next
Catch ex As Exception
MsgBox(ex.Message)
End Try
End If
End Sub
Private Sub Treeview1_BeforeExpand(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewCancelEventArgs) Handles TreeView1.BeforeExpand
If e.Node.Nodes.Count = 1 AndAlso e.Node.Nodes(0).Text = "Loading..." Then
e.Node.Nodes.Clear()
AddAllFolders(e.Node, CStr(e.Node.Tag))
End If
End Sub
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
TreeView1.Sort()
Dim Tnode As TreeNode = TreeView1.Nodes.Add("(Drive W:)")
AddAllFolders(Tnode, "C:\")
ListView1.View = View.Details
' Add a column with width 80 and left alignment
ListView1.Columns.Add("File Name", 150, HorizontalAlignment.Left)
ListView1.Columns.Add("File Type", 80, HorizontalAlignment.Left)
ListView1.Columns.Add("Date Modified", 150, HorizontalAlignment.Left)
TreeView2.Sort()
Dim Tnode1 As TreeNode = TreeView2.Nodes.Add("(Drive V:)")
AddAllFolders(Tnode1, "C:\")
ListView1.View = View.Details
' Add a column with width 80 and left alignment
End Sub
Private Sub TabPage1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TabPage1.Click
End Sub
Private Sub TreeView2_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView2.AfterSelect
Dim FileExtension As String
Dim SubItemIndex As Integer
Dim DateMod As String
ListView1.Items.Clear()
If TreeView2.SelectedNode.Nodes.Count = 1 AndAlso TreeView2.SelectedNode.Nodes(0).Text = "Loading..." Then
TreeView2.SelectedNode.Nodes.Clear()
AddAllFolders(TreeView2.SelectedNode, CStr(TreeView2.SelectedNode.Tag))
End If
Dim folder As String = CStr(TreeView2.SelectedNode.Tag)
If Not folder Is Nothing AndAlso IO.Directory.Exists(folder) Then
Try
For Each file As String In IO.Directory.GetFiles(folder, "*.cue*")
FileExtension = IO.Path.GetExtension(file)
DateMod = IO.File.GetLastWriteTime(file).ToString()
AddImages(file)
ListView1.Items.Add(file.Substring(file.LastIndexOf("\"c) + 1), file.ToString())
ListView1.Items(SubItemIndex).SubItems.Add(FileExtension.ToString() & " File")
ListView1.Items(SubItemIndex).SubItems.Add(DateMod.ToString())
SubItemIndex += 1
Next
Catch ex As Exception
MsgBox(ex.Message)
End Try
End If
If Not folder Is Nothing AndAlso IO.Directory.Exists(folder) Then
Try
For Each file As String In IO.Directory.GetFiles(folder, "*.ddp*")
FileExtension = IO.Path.GetExtension(file)
DateMod = IO.File.GetLastWriteTime(file).ToString()
AddImages(file)
ListView1.Items.Add(file.Substring(file.LastIndexOf("\"c) + 1), file.ToString())
ListView1.Items(SubItemIndex).SubItems.Add(FileExtension.ToString() & " File")
ListView1.Items(SubItemIndex).SubItems.Add(DateMod.ToString())
SubItemIndex += 1
Next
Catch ex As Exception
MsgBox(ex.Message)
End Try
End If
If Not folder Is Nothing AndAlso IO.Directory.Exists(folder) Then
Try
For Each file As String In IO.Directory.GetFiles(folder, "*.iso*")
FileExtension = IO.Path.GetExtension(file)
DateMod = IO.File.GetLastWriteTime(file).ToString()
AddImages(file)
ListView1.Items.Add(file.Substring(file.LastIndexOf("\"c) + 1), file.ToString())
ListView1.Items(SubItemIndex).SubItems.Add(FileExtension.ToString() & " File")
ListView1.Items(SubItemIndex).SubItems.Add(DateMod.ToString())
SubItemIndex += 1
Next
Catch ex As Exception
MsgBox(ex.Message)
End Try
End If
End Sub
Private Sub Treeview2_BeforeExpand(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewCancelEventArgs) Handles TreeView2.BeforeExpand
If e.Node.Nodes.Count = 1 AndAlso e.Node.Nodes(0).Text = "Loading..." Then
e.Node.Nodes.Clear()
AddAllFolders(e.Node, CStr(e.Node.Tag))
End If
End Sub
Private Sub TreeView2_AfterSelect_1(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs)
End Sub
Private Sub ListView1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListView1.SelectedIndexChanged
End Sub
Private Sub ListView1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles ListView1.DoubleClick
End Sub
Private Sub TabPage2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TabPage2.Click
End Sub
Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
End Sub
Private Sub GroupBox1_Enter(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GroupBox1.Enter
End Sub
Private Sub Label1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
End Sub
Private Sub SearchBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SearchBtn.Click
OpenFileDialog.ShowDialog()
FilePathLabel.Text = OpenFileDialog.FileName
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Process.Start("", FilePathLabel.Text)
End Sub
End Class
-
Apr 27th, 2013, 05:05 AM
#2
Re: Populate label with full file path from listview item
Hi,
You can do this by adding the Path to your file to a hidden column in the ListView. To do this you do NOT need to declare another Column Header for the ListView when you load the Form but just add an additional SubItem value for each ListViewItem that is added. i.e:-
Code:
Dim LVI As New ListViewItem
With LVI
.Text = "YourFileName"
.SubItems.Add("YourFileExtension")
.SubItems.Add("YourFileDate")
.SubItems.Add("YourFilePath")
End With
ListView1.Items.Add(LVI)
You can then join your path and filename together when you Double Click the file to be shown. i.e:-
Code:
Private Sub ListView1_DoubleClick(sender As Object, e As System.EventArgs) Handles ListView1.DoubleClick
If ListView1.SelectedItems.Count > 0 Then
Dim LVI As ListViewItem = ListView1.SelectedItems(0)
MsgBox("File Name and Path is " & LVI.SubItems(3).Text & "\" & LVI.SubItems(0).Text)
End If
End Sub
I would also suggest the you familiarize yourself with how Functions and Subroutines work to aid you in your programming.
Hope that helps.
Cheers,
Ian
-
Apr 27th, 2013, 06:22 AM
#3
Thread Starter
Member
Re: Populate label with full file path from listview item
Hey Ian,
Thank you for your prompt response. Using the information you have provided I am starting to understand how to piece together the final string for the output. AND where it needs to be in my form.
I changed the DoubleClick action to output into the FilePathLabel just fine. But I can't seem to get the first part of it (The PATH to the file). So now the output is only ie: \lol.cue
The file path depends on what folder the treeview is in am I right?
I feel like I am almost there.
-
Apr 27th, 2013, 07:02 AM
#4
Thread Starter
Member
Re: Populate label with full file path from listview item
@Ian
Thank you so much for not spelling it out for me and letting me figure it out on my own. I had to name a DIM called FilePath:
Dim FilePath As String
then use trial and error to figure this out:
FilePath = IO.Path.GetFullPath(folder)
and finally:
ListView1.Items(SubItemIndex).SubItems.Add(FilePath.ToString())
Then when I double click on the item in listview the filepath prefixes the filename.
Sir you are a gentleman and a scholar. Thank you so much!
-
Apr 27th, 2013, 07:47 AM
#5
Re: [RESOLVED] Populate label with full file path from listview item
Good to hear and you are welcome.
Cheers,
Ian
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|