Reputation: 5129
I want to create a simple printer manager to use in our Terminal server environment. Because of GPO restrictions, there are limits of what built-in functionality I can use. So I decided to try to write my own simple GUI to do that.
Now, the printers are distributed in a folder, with subfolders to categorize them. In each folder there are .lnk files to the actual printer on the printserver.
What I want to do is to populate a treeview with the folders, and the printers in a listview, based on which item is clicked on the treeview.
I've already managed to search for directories and to search the files for each item I've clicked. But I realized, why not use a collection or similar to do this during the startup of the form? That way, it'll be faster. Because right now, there's a small delay each time I click an item in the treeview. Because it scans for files each time.
How can I add the same to a collection and use that instead?
Here's my current code:
Public Sub populateTreeView(ByVal strPath As String)
Dim di As New IO.DirectoryInfo(strPath)
Dim diar1 As IO.DirectoryInfo() = di.GetDirectories()
Dim dra As IO.DirectoryInfo
For Each dra In diar1
ImageList1.Images.Add(GetSmallIcon(dra.FullName))
TreeView1.Nodes.Add("", dra.Name, nIndex)
nIndex = nIndex + 1
Next
End Sub
Private Sub TreeView1_AfterSelect(sender As Object, e As TreeViewEventArgs) Handles TreeView1.AfterSelect
ListView1.Clear()
nIndex = 0
Dim di As New IO.DirectoryInfo(strIniSettings & "\" & TreeView1.SelectedNode.Text)
Dim diar1 As IO.FileInfo() = di.GetFiles()
Dim dra As IO.FileInfo
For Each dra In diar1
Dim strName As String
strName = Replace(dra.Name, ".lnk", "")
ImageList2.Images.Add(GetLargeIcon(dra.FullName))
ListView1.Items.Add("", strName, nIndex)
nIndex = nIndex + 1
Next
End Sub
Notice the Imagelists? I also get the Icon for each item as well.
Upvotes: 0
Views: 5776
Reputation: 101072
Since your data is not complex, a simple LookUp
may be the right collection for you (or just a plain Dictionary).
Just query the printers once, and store it in a member variable, or just use the Tag
property of the TreeNode
s so store the file names.
In the example below, I'm using a simple Linq query to create a LookUp
where the Key
is the directory name (you could also just use full path to the directory), and the items are the file names.
You could then either query the collection by a given Key
(the directory name), or use the Tag
property.
LINQPad example:
Sub Main
' query printers once (just replace C:\test with your path)
' store the result in a member variable of your form
Dim printer = new DirectoryInfo("C:\test").GetDirectories() _
.SelectMany(Function(d) d.GetFiles()) _
.ToLookup(Function(f) f.Directory.Name, Function(f) f.Name)
' Or, using a Dictionary
' Dim printer = new DirectoryInfo("C:\test").GetDirectories() _
' .ToDictionary(Function(d) d.Name, Function(d) d.GetFiles().Select(Function(f) f.Name).ToList())
Dim l = new ListView() With {.Dock = DockStyle.Right}
Dim t = new TreeView() With {.Dock = DockStyle.Left}
AddHandler t.AfterSelect, Sub(s, e)
' This is your AfterSelect event handler
' The filenames are stored in the Tag of the TreeNode
' You could also use 'For Each p As String in printer(e.Node.Text)'
l.Items.Clear()
For Each p As String in e.Node.Tag
Dim item = l.Items.Add(p.Replace(".lnk", ""))
'TODO: Set Icon on item
Next
End Sub
' Populate TreeView once
For Each folder in printer
Dim fNode = t.Nodes.Add(folder.Key)
'TODO: Set Icon on fNode
' store the files in the Tag of the node.
' You don't have to, but it will make it easier
fNode.Tag = folder
Next
' Show test form
Dim w = new Form()
w.Controls.Add(t)
w.Controls.Add(l)
w.Show()
End Sub
Upvotes: 1