cant modify content when datasource set

ident

Newcomer
Joined
May 29, 2008
Messages
5
Is there another way to list files? As it wont let me remove from the listbox using datasource

Code:
    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
 
Rather than an array could you not bind to a list of strings and simply remove the item from the underlying list?
Something like
Visual Basic:
    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
Visual Basic:
   Dim torrents() As String = Directory.GetFiles(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "uTorrent"), "*.torrent")
instead of hard coding paths.
 
Thank you for the reply, for learning purposes can you explain how this code works, seems a lot tidier
Code:
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
 
You will need to add an
Visual Basic:
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.
 
Back
Top