javasource

Hello all,

Part of an application that I'm working on uses a treeview/listview control. My goal is pretty simple, I think...

I've created an application that parses HTML files. It allows the user to edit these parsed fields and save their changes over the existing file. I realize that I could use the filebrowser dialog, or simply enter the path of file, but I need the user to be able to select a file in the listview control and have that file load in a textbox.

Again, user selects (clicks) a file in the listview. By clicking the file it automatically opens in the textbox. I have no clue as to how to accomplish this seemingly simple task. Any help would be very much appreciated!

Thanks,

Mike



Re: Visual Basic Express Edition open a file in listview control

ReneeC

How much do you have already






Re: Visual Basic Express Edition open a file in listview control

javasource

ReneeC,

This is small part of a much larger app. As far as this part is concerned, I have the entire UI put together. I also have written all of the parsing functions - using instr and mid functions. For the initial dev, I simply entered the path of a ext file into a streamreader, and parsed the file accordingly. So, the final piece is to automate this process with the aforementioned functionality. I have the split container, listview, and treeview controls in place. When I debug, I can view the files (simply the name and type are displayed...) Here's the code for the tree/listview...

Private Sub PopulateTreeView()

Dim rootNode As TreeNode

TreeView1.ImageList.Images.Add("folder", New Icon("folder.ico"))

TreeView1.ImageList.Images.Add("document", New Icon("doc.ico"))

Dim info As New DirectoryInfo("C:\Control\Product")

If info.Exists Then

rootNode = New TreeNode(info.Name)

rootNode.Tag = info

GetDirectories(info.GetDirectories(), rootNode)

TreeView1.Nodes.Add(rootNode)

End If

End Sub

Private Sub GetDirectories(ByVal subDirs() As DirectoryInfo, _

ByVal nodeToAddTo As TreeNode)

Dim aNode As TreeNode

Dim subSubDirs() As DirectoryInfo

Dim subDir As DirectoryInfo

For Each subDir In subDirs

aNode = New TreeNode(subDir.Name, 0, 0)

aNode.Tag = subDir

aNode.ImageKey = "folder"

subSubDirs = subDir.GetDirectories()

If subSubDirs.Length <> 0 Then

GetDirectories(subSubDirs, aNode)

End If

nodeToAddTo.Nodes.Add(aNode)

Next subDir

End Sub

Public Sub New()

InitializeComponent()

PopulateTreeView()

End Sub

Private Sub TreeView1_NodeMouseClick(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeNodeMouseClickEventArgs) Handles TreeView1.NodeMouseClick

Dim newSelected As TreeNode = e.Node

ListView1.Items.Clear()

Dim nodeDirInfo As DirectoryInfo = _

CType(newSelected.Tag, DirectoryInfo)

Dim subItems() As ListViewItem.ListViewSubItem

Dim item As ListViewItem = Nothing

Dim file As FileInfo

For Each file In nodeDirInfo.GetFiles()

item = New ListViewItem(file.Name, 1)

subItems = New ListViewItem.ListViewSubItem() _

{New ListViewItem.ListViewSubItem(item, "File"), _

New ListViewItem.ListViewSubItem(item, _

file.LastAccessTime.ToShortDateString())}

item.SubItems.AddRange(subItems)

ListView1.Items.Add(item)

Next file

ListView1.AutoResizeColumns(ColumnHeaderAutoResizeStyle.HeaderSize)

End Sub

~mike





Re: Visual Basic Express Edition open a file in listview control

ReneeC

Ok first of all

the listview.view = details

multiselect = false

fullrow select = true

Private Filecontents as string

Private Sub listview1_ItemSelectionChanged(ByVal sender As Object, _
ByVal e As System.Windows.Forms.ListViewItemSelectionChangedEventArgs) _
Handles listview1.ItemSelectionChanged

if listview1.selecteditem isNothing then return

Try

Filecontents = My.computer.filesystem.ReadAllText(listview1.selecteditem.text)

Cach ex as exception

message

end try

End sub






Re: Visual Basic Express Edition open a file in listview control

javasource

ok, I verified multiselect and fullrow select as well as the details view... no prob there... ;)

I pasted the code, but have a few errors...

In the if statement, the listview1.selecteditem does not exist, so I changed it to - listview1.selecteditems... I'm assuming that's fine.

In the Filecontents statement, Filecontents = My.computer.filesystem.ReadAllText(listview1.selecteditem.text),

I get the following error... (i also changed the selecteditem to selecteditems):

Error 1 'text' is not a member of 'System.Windows.Forms.ListView.SelectedListViewItemCollection'.

also, what do you mean by message(), can't use that that type as an expression

What am I doing wrong Sorry for the confusion - I do appreciate your time!

Mike





Re: Visual Basic Express Edition open a file in listview control

ReneeC

It's my fault. The If statement should say this:

If listview1.SelectedItems.Count = 0 Then Exit Sub

Filecontents = My.computer.filesystem.ReadAllText(listview1.selecteditems(0).text)






Re: Visual Basic Express Edition open a file in listview control

javasource

looks like I'm making progress with this...

When I debug this, I get a file not found exception. It is looking for the file in the debug bin folder, not the directory listed in the listview control. So, instead of My.computer.filesystem.ReadAllText, what should I put

Also, I simply pasted over the Try.. End Try statement. Is that what I should've done

Mike





Re: Visual Basic Express Edition open a file in listview control

ReneeC

pasted over

The file read statement will work IF you have a valid file spec in the listview item. If there is not a valid spec you will get and exception which the Try-catch is designed to handled. An error message goes where I had message.






Re: Visual Basic Express Edition open a file in listview control

javasource

ok, I figured that was meant to be there - but I guess I thought the count = 0 would do the same thing. My bad.

As I mentioned before, the (ListView1.SelectedItems(0).Text), does point to the listview item. The My.Computer.FileSystem.ReadAllText makes the app go and look in the debug bin folder of the project - not the directory the file actually resides in. So, how do I point it to the right directory





Re: Visual Basic Express Edition open a file in listview control

ReneeC

you need to have the full filespec either in the listviewitem OR

Part or all of it in the listviewitem.tag....

The point is.... you need a filespec






Re: Visual Basic Express Edition open a file in listview control

javasource

ReneeC,

Thanks for your help. I've tagged the listview with the directory path and it works.

Filecontents = My.Computer.FileSystem.ReadAllText(ListView1.Tag & ListView1.SelectedItems(0).Text)

You're the best,

Mike





Re: Visual Basic Express Edition open a file in listview control

javasource

I've ended up using this instead:

added a textbox (textbox7) to my form and...

Dim newSelected As TreeNode = e.Node

TextBox7.Text = CType(newSelected.Tag, DirectoryInfo).FullName

to the TreeView1.NodeMouseClick event

I then added...

Filecontents = My.Computer.FileSystem.ReadAllText(TextBox7.Text & "\" & ListView1.SelectedItems(0).Text)

to the ListView1.ItemSelectionChanged event handler.

This allows the user to navigate through sub-directories, and still have the ability to read the selected file.