Vakio

I have a program that i want to use to browse and play my movies with... i have a form where you add a movie to my program... in the form i have a browser that shows my local disk, then i have a textbox that shows the current url.

for example, if i go to local disc C and then to my music the textbox text = file:///C:/my Music

Now... the problem is this. Let's say that in my music i have a mp3 file called Hellow...

i vould like my textbox to also show the file i have selected with my mouse. So if i go to my music and click (not double-click) the mp3 file, my textbox text vould be file:///C:/my Music/hellow

i vould be very grateful if someone could tell me how to do this....



Re: Visual Basic Express Edition Get the textbox to also show the selected file

paoloTheCool

Can you post your code, because I do not know how you are displaying the files. Is it in a listView, a listBox...etc

Thanks,

paoloTheCool





Re: Visual Basic Express Edition Get the textbox to also show the selected file

Vakio

I use a webbrowser to do it... her is the code

Private Sub WebBrowser1_DocumentCompleted(ByVal sender As Object, ByVal e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted

TextBox1.Text = WebBrowser1.Url.ToString

End Sub





Re: Visual Basic Express Edition Get the textbox to also show the selected file

paoloTheCool

Vakio wrote:

i vould like my textbox to also show the file i have selected with my mouse.

But where are you selecting the files

Thanks,

paoloTheCool





Re: Visual Basic Express Edition Get the textbox to also show the selected file

Martin Xie - MSFT

Hi Vakio,

I understand your meaning.

You use WebBrowser control to show local file sysytem, similar to File Explorer.

Code Block

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

WebBrowser1.Navigate("C:\my Music")

End Sub

Then you want to show the file path on TextBox control when you click/select a file (e.g. Hellow.mp3) in WebBrowser control.

But I feel that there is no relevant event or property or method used to capture/retrieve the selected item in WebBrowser control. Any suggestion or solution is appreciated.

Instead, you can use the TreeView control (standard or custom) to make a file explorer. Then you can retrieve selected file and show it on TextBox like this:

Code Block

Private Sub TreeView1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TreeView1.Click

TextBox1.Text = TreeView1.SelectedNode.Text

End Sub

1. An All VB.NET Explorer Tree Control with ImageList Management

All Windows folders, including Virtual Folders like Desktop, My Computer, and History can be properly displayed and made available to the containing form.

2. Explorer ComboBox and ListView in VB.NET

A ComboBox (and ListView) displaying the file system, using the system image list.

3. File Explorer using Treeview controller in C# 2.0

This simple application can be used to display files and folders in your computer using Treeview controller.

Regards,

Martin






Re: Visual Basic Express Edition Get the textbox to also show the selected file

Vakio

ok... but how do i get the treeview to show anything when my form loads





Re: Visual Basic Express Edition Get the textbox to also show the selected file

Martin Xie - MSFT

HI Vakio,

This code sample implements a simple File Explorer using TreeView control.

The TreeView will load all drives/directories/files of your computer when loading Form.

The file path will shown on TextBox control when you click/select a file in the TreeView control.

Prerequisites: TreeView1 and TextBox1 on Form1.

Handle three events: Form1_Load, TreeView1_BeforeExpand and TreeView1_AfterSelect.

Code Block

Imports System.IO

Imports Microsoft.VisualBasic.FileIO

Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Try

'Create Desktop

Dim desktop As TreeNode = New TreeNode()

desktop.Text = "Desktop"

desktop.Tag = "Desktop"

desktop.Nodes.Add("")

TreeView1.Nodes.Add(desktop)

'Get driveInfo

For Each drv As DriveInfo In DriveInfo.GetDrives()

Dim fChild As TreeNode = New TreeNode()

If drv.DriveType = DriveType.CDRom Then

fChild.ImageIndex = 1

fChild.SelectedImageIndex = 1

ElseIf drv.DriveType = DriveType.Fixed Then

fChild.ImageIndex = 0

fChild.SelectedImageIndex = 0

End If

fChild.Text = drv.Name

fChild.Nodes.Add("")

TreeView1.Nodes.Add(fChild)

Next drv

Catch ex As Exception

MessageBox.Show(ex.Message)

End Try

End Sub

'Enumerate directories and files

Private Sub TreeView1_BeforeExpand(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewCancelEventArgs) Handles TreeView1.BeforeExpand

Dim parentNode As TreeNode = e.Node

If parentNode.Nodes(0).Text = "" Then

Dim rootDir As DirectoryInfo

' To fill Desktop

Dim arr As Char() = {"\"c}

Dim nameList As String() = parentNode.FullPath.Split(arr)

Dim path As String = ""

If nameList.GetValue(0).ToString() = "Desktop" Then

path = SpecialDirectories.Desktop & "\"

For i As Integer = 1 To nameList.Length - 1

path = path & nameList(i) & "\"

Next i

rootDir = New DirectoryInfo(path)

'for other Directories

Else

rootDir = New DirectoryInfo(parentNode.FullPath & "\")

End If

parentNode.Nodes(0).Remove()

For Each dir As DirectoryInfo In rootDir.GetDirectories()

Dim node As TreeNode = New TreeNode()

node.Text = dir.Name

node.Nodes.Add("")

parentNode.Nodes.Add(node)

Next dir

'Fill files

For Each file As FileInfo In rootDir.GetFiles()

Dim node As TreeNode = New TreeNode()

node.Text = file.Name

node.ImageIndex = 2

node.SelectedImageIndex = 2

parentNode.Nodes.Add(node)

Next file

End If

End Sub

Private Sub TreeView1_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect

'Show the file path on TextBox control when you click/select a file in the TreeView control

TextBox1.Text = TreeView1.SelectedNode.FullPath

End Sub

End Class

Regards,

Martin






Re: Visual Basic Express Edition Get the textbox to also show the selected file

bdbodger

I did some thing simular to what dave did only I used the tag property of the node to save the file info . I found that easier than the way dave did it . I used a timer so that the form could load first before populating the treeview . This code gets the drives and the first level of files . When you expand a node it gets the next level below that so that it does not try to load too much at once and only what is needed . I used the file info in the nodes tag to display other information like creation time and last access time etc when a node was selected .

Code Block

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick

Timer1.Enabled = False

TreeView1.BeginUpdate()

TreeView1.Nodes.Clear()

TreeView1.Nodes.Add("Desktop")

TreeView1.Nodes.Add("My Documents")

Dim newNode As TreeNode = Nothing

Dim deskinfo As DirectoryInfo = New DirectoryInfo(My.Computer.FileSystem.SpecialDirectories.Desktop)

TreeView1.Nodes(0).Tag = deskinfo

nodetree(TreeView1.Nodes(0)) ' get next level in path

Dim docinfo As DirectoryInfo = New DirectoryInfo(My.Computer.FileSystem.SpecialDirectories.MyDocuments)

TreeView1.Nodes(1).Tag = docinfo

nodetree(TreeView1.Nodes(1)) ' get next level in path

Dim getInfo As System.IO.DriveInfo()

getInfo = System.IO.DriveInfo.GetDrives

For Each info As System.IO.DriveInfo In getInfo

If info.IsReady Then

newNode = New TreeNode(info.VolumeLabel)

Else

newNode = New TreeNode(info.Name)

End If

newNode.ImageIndex = 3

Dim di As DirectoryInfo = New DirectoryInfo(info.Name)

newNode.Tag = di

TreeView1.Nodes.Add(newNode)

nodetree(newNode) ' get next level in path

Next

TreeView1.EndUpdate()

TreeView1.CollapseAll()

Panel1.Visible = False

End Sub

Public Function nodetree(ByVal sender As TreeNode)

Try

Dim longpath As String = Nothing

If TypeOf sender.Tag Is DirectoryInfo Then

Dim Sinfo As DirectoryInfo = sender.Tag

longpath = Sinfo.FullName

Else

Dim Finfo As FileInfo = sender.Tag

longpath = Finfo.DirectoryName

End If

Dim dirs() As String = Nothing

Dim dirname As String = Nothing

dirs = Directory.GetDirectories(longpath, "*.*", SearchOption.TopDirectoryOnly)

For Each d As String In dirs

Dim di As New DirectoryInfo(d)

dirname = d.Replace(longpath, "")

dirname = dirname.Replace("\", "")

Dim newnode As New TreeNode(dirname)

newnode.Tag = di

sender.Nodes.Add(newnode)

Next

Dim files() As String = Directory.GetFiles(longpath, "*.*", SearchOption.TopDirectoryOnly)

For Each f As String In files

Dim fi As FileInfo = New FileInfo(f)

Dim newnode As New TreeNode(Path.GetFileName(f))

newnode.ImageIndex = 4

newnode.Tag = fi

sender.Nodes.Add(newnode)

Next

Catch

End Try

Return Nothing

End Function

Private Sub TreeView1_BeforeExpand(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewCancelEventArgs) Handles TreeView1.BeforeExpand

Try

Dim Nnodes As TreeNodeCollection = e.Node.Nodes

For Each N As TreeNode In Nnodes

If TypeOf N.Tag Is DirectoryInfo Then

If N.Nodes.Count = 0 Then

nodetree(N)

End If

End If

Next

Catch ex As Exception

End Try

End Sub

Private Sub TreeView1_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect

UpDatenode(TreeView1.SelectedNode) ' displays information about selected node from tag

End Sub





Re: Visual Basic Express Edition Get the textbox to also show the selected file

Vakio

i tryed with your code Martin, but VB did not understand the words "driveinfo" and "DirectoryInfo" and "SpecialDirectories"

why

i'm just a beginner with all this programming... so i don't get all these things........yet.....





Re: Visual Basic Express Edition Get the textbox to also show the selected file

Vakio

also tryed with your code bdbodger.... but here too the same problems appeared....





Re: Visual Basic Express Edition Get the textbox to also show the selected file

bdbodger

put this at the top of your code

Code Block

Imports System.IO

or use System.IO.DriveInfo also this forum wraps long lines

Code Block

Dim docinfo As DirectoryInfo = New DirectoryInfo(My.Computer.FileSystem.SpecialDirectories.MyDocuments)

is one line





Re: Visual Basic Express Edition Get the textbox to also show the selected file

Vakio

ok.. now everything else works except the last part containing "updatenode"




Re: Visual Basic Express Edition Get the textbox to also show the selected file

bdbodger

This is what I have but I also have code for a structure I called ZipFileInfo so you will have to edit that part out . This code puts information about the selected node into a richtextbox called InfoBox . What you would do is if the tag contains fileinfo update the richtextbox with the first method if it contains directory info do it the second way .

Code Block

Private Sub UpDatenode(ByVal node As TreeNode)

Try

If TypeOf node.Tag Is FileInfo Or TypeOf node.Tag Is ZipFileInfo Then

Dim F_Info As FileInfo

If TypeOf node.Tag Is ZipFileInfo Then

Dim zfi As ZipFileInfo = node.Tag 'sender.tag

F_Info = zfi.fileinfo

Else

F_Info = node.Tag

End If

Dim checkinfo As New FileInfo(F_Info.FullName)

If Not checkinfo.Equals(F_Info) Then

If TypeOf node.Tag Is ZipFileInfo Then

Dim newzipinfo As New ZipFileInfo

newzipinfo = node.Tag

newzipinfo.fileinfo = checkinfo

node.Tag = newzipinfo

Else

node.Tag = checkinfo

End If

F_Info = checkinfo

End If

InfoBox.Clear()

InfoBox.SelectionColor = Color.Red

InfoBox.AppendText("File Name: ")

InfoBox.SelectionColor = Color.Black

InfoBox.AppendText(F_Info.FullName & vbCrLf)

InfoBox.SelectionColor = Color.Red

InfoBox.AppendText("Size: ")

InfoBox.SelectionColor = Color.Black

InfoBox.AppendText(F_Info.Length & " Bytes" & vbCrLf)

InfoBox.SelectionColor = Color.Red

InfoBox.AppendText("Last Access Time: ")

InfoBox.SelectionColor = Color.Black

InfoBox.AppendText(F_Info.LastAccessTime & vbCrLf)

InfoBox.SelectionColor = Color.Red

InfoBox.AppendText("Last Write Time: ")

InfoBox.SelectionColor = Color.Black

InfoBox.AppendText(F_Info.LastWriteTime)

ElseIf TypeOf node.Tag Is DirectoryInfo Then

Dim D_info As DirectoryInfo = Nothing

D_info = node.Tag

Dim checkinfo As New DirectoryInfo(D_info.FullName)

If Not checkinfo.Equals(D_info) Then

node.Tag = checkinfo

D_info = checkinfo

End If

InfoBox.Clear()

InfoBox.SelectionColor = Color.Red

InfoBox.AppendText("Directory Name: ")

InfoBox.SelectionColor = Color.Black

InfoBox.AppendText(D_info.FullName & vbCrLf)

InfoBox.SelectionColor = Color.Red

InfoBox.AppendText("Creation Time: ")

InfoBox.SelectionColor = Color.Black

InfoBox.AppendText(D_info.CreationTime & vbCrLf)

InfoBox.SelectionColor = Color.Red

InfoBox.AppendText("Last Access Time: ")

InfoBox.SelectionColor = Color.Black

InfoBox.AppendText(D_info.LastAccessTime & vbCrLf)

InfoBox.SelectionColor = Color.Red

InfoBox.AppendText("Last Write Time: ")

InfoBox.SelectionColor = Color.Black

InfoBox.AppendText(D_info.LastWriteTime)

End If

Catch ex As Exception

End Try

End Sub





Re: Visual Basic Express Edition Get the textbox to also show the selected file

Vakio

ok... but that "zipfileinfo"... what is it it is now the only thing VB does not recognize...





Re: Visual Basic Express Edition Get the textbox to also show the selected file

bdbodger

It is a structure look it up in the help files but you won't need it for this. It just contains some extra info that my project used so that code has to get the fileInfo from the ZipFileInfo instead of the tag being fileInfo or directoryInfo only . Either way I am using the file or directory information stored in the treenodes tag

Code Block

Public Structure ZipFileInfo

Dim zipfilename As String

Dim zipentry As ZipEntry

Dim fileinfo As FileInfo

End Structure