ident Posted June 10, 2008 Posted June 10, 2008 Is there another way to list files? As it wont let me remove from the listbox using datasource Private Sub cmdRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdRemove.Click If lstDisplay.SelectedIndex <> -1 Then lstDisplay.Items.RemoveAt(lstDisplay.SelectedIndex) End If End Sub Private Sub cmdSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSearch.Click 'Search for torrent files Dim torrents() As String = System.IO.Directory.GetFiles("C:\Documents and Settings\Sam\Application Data\uTorrent", "*.torrent") 'Display found files lstDisplay.DataSource = torrents End Sub Quote
Administrators PlausiblyDamp Posted June 10, 2008 Administrators Posted June 10, 2008 Rather than an array could you not bind to a list of strings and simply remove the item from the underlying list? Something like Dim torrentList As List(Of String) Private Sub cmdRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click If lstDisplay.SelectedIndex <> -1 Then torrentList.Remove((lstDisplay.SelectedValue)) lstDisplay.DataSource = Nothing lstDisplay.DataSource = torrentList End If End Sub Private Sub cmdSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click 'Search for torrent files Dim torrents() As String = System.IO.Directory.GetFiles("C:\Documents and Settings\Sam\Application Data\uTorrent", "*.torrent") torrentList = torrents.ToList() 'Display found files lstDisplay.DataSource = torrentList End Sub Rather than the iffy bit of code setting the datasource to nothing and back you could also use a DataSource control... As an aside you are better using code like Dim torrents() As String = Directory.GetFiles(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "uTorrent"), "*.torrent") instead of hard coding paths. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
ident Posted June 10, 2008 Author Posted June 10, 2008 Thank you for the reply, for learning purposes can you explain how this code works, seems a lot tidier Dim torrents() As String = Directory.GetFiles(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "uTorrent"), "*.torrent") But vb.net does highlight Directory and path saying it's not declared. I am very new to coding so sorry if i am being stupid. I will test your other code now. But to be honest in the future i would like to be able to search all the c:\ drive or the location stated in txtDir rather then the specific location Thanks again Quote
Administrators PlausiblyDamp Posted June 10, 2008 Administrators Posted June 10, 2008 You will need to add an Imports System.Io to the top of the source file to get it to compile. the Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData bit will return the path for the current users application data - this can vary by OS or be configured differently to the normal OS' default so you are better avoiding hard coded paths. Path.Combine simply builds a valid path from two strings - I tend to use it because it handles leading / trailing or omitted path separators (/ or \) plus the fact the code expresses it's intention more clearly than just concatenating strings. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.