Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

VB .net query

Options
  • 20-06-2005 5:15pm
    #1
    Registered Users Posts: 245 ✭✭


    I'm writing a program that allows me to add filenames to a list box using an OpenFileDialog. This works fine if i add one at a time, however i want to be able to select a bunch of files and add them at once to the list box. When i do this however, it only adds the last one. Anybody know how i can fix this? :confused:

    Thanks,

    P


Comments

  • Moderators, Science, Health & Environment Moderators Posts: 8,950 Mod ✭✭✭✭mewso


    set the multiselect property to true.


  • Registered Users Posts: 245 ✭✭Polonious


    Yep I knew that bit, which allows me to select the files, but when i click open only one appears in the list box...


  • Registered Users Posts: 15,443 ✭✭✭✭bonkey


    What code are you using to populate teh listbox with?

    Maybe if you posted the current code, it would be easier to point out the flaw.

    jc


  • Moderators, Science, Health & Environment Moderators Posts: 8,950 Mod ✭✭✭✭mewso


    If OpenFileDialog1.ShowDialog = DialogResult.OK Then
        Dim s As String
        For Each s In OpenFileDialog1.FileNames
            's is now the path to each file
        Next
    End If
    


  • Registered Users Posts: 7,468 ✭✭✭Evil Phil


    I'm assuming you left some code out of your for-each loop there. Could you post the full code?


  • Advertisement
  • Moderators, Science, Health & Environment Moderators Posts: 8,950 Mod ✭✭✭✭mewso


    Well all I'm sayng is the variable s is a string of the full path to each file. I don't know what anybody wants to do with it. If you were adding it to a listbox I suppose it would be listbox1.items.add(s) or something. Up to you.

    *Edit. Phil I'm posting a possible solution. Taint me with the problem.


  • Registered Users Posts: 245 ✭✭Polonious


    Here is the code and it works. But it needs to be altered in some way so that when i enable multiselect it will work...

    Private Sub cmdAddFiles_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAddFiles.Click

    Dim additem As String
    Dim MyDialog As New OpenFileDialog

    MyDialog.Multiselect = False
    MyDialog.InitialDirectory = "C:\"
    MyDialog.Filter = "csv files (*.csv)|*.csv"

    If (MyDialog.ShowDialog() = DialogResult.OK) Then
    additem = MyDialog.FileName
    lstFileList.Items.Add(additem)
    End If

    End Sub


  • Closed Accounts Posts: 17,208 ✭✭✭✭aidan_walsh


    Polonious wrote:
    Here is the code and it works. But it needs to be altered in some way so that when i enable multiselect it will work...

    Private Sub cmdAddFiles_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAddFiles.Click

    Dim additem As String
    Dim MyDialog As New OpenFileDialog

    MyDialog.Multiselect = False
    MyDialog.InitialDirectory = "C:\"
    MyDialog.Filter = "csv files (*.csv)|*.csv"

    If (MyDialog.ShowDialog() = DialogResult.OK) Then
    additem = MyDialog.FileName
    lstFileList.Items.Add(additem)
    End If

    End Sub
    Get your returned files using FileNames instead of FileName, and then loop through the array of strings, adding each to the list item.


  • Moderators, Science, Health & Environment Moderators Posts: 8,950 Mod ✭✭✭✭mewso


    Yeah sorry I must have been using invisible text in that code sample I posted.


  • Registered Users Posts: 2,758 ✭✭✭Peace


    This....
    musician wrote:
    If OpenFileDialog1.ShowDialog = DialogResult.OK Then
        Dim s As String
        For Each s In OpenFileDialog1.FileNames
            's is now the path to each file
        Next
    End If
    

    Plus this...
    Polonious wrote:
     additem = MyDialog.FileName
    lstFileList.Items.Add(additem)
    

    should get you sorted.


  • Advertisement
  • Registered Users Posts: 245 ✭✭Polonious


    Nice one Musician, that worked a treat. Thanks all for your help.


  • Registered Users Posts: 7,468 ✭✭✭Evil Phil


    If OpenFileDialog1.ShowDialog = DialogResult.OK Then
    
        stFileList.Items.AddRange(OpenFileDialog1.FileNames)
    
    End If
    

    Would be a neater way to do it as there's no need for all that looping, please note that this code may not be exact as I'm translating from C# and don't know vb.net. You get the idea though :D
    musician wrote:
    *Edit. Phil I'm posting a possible solution. Taint me with the problem.
    Soz, mixed you up with the OP.


  • Moderators, Science, Health & Environment Moderators Posts: 8,950 Mod ✭✭✭✭mewso


    Evil Phil wrote:
    Would be a neater way to do it as there's no need for all that looping,

    Assuming you dont want to create objects with the strings before adding them to the listbox or can you with addrange (put them in a collection perhaps)?


Advertisement