Populating/selecting/manipulating windows file names in a checked list box

After many hours of trying to solve this problem I can't proceed without some assistance. Thanks Shakalama for the previous helpful information.

I need to read in multiple windows filenames into a checked listbox component with the multiselect option. Wrox page s855-857 has some clues but I still can't solve my problem. After the following initialisation:

Me.lblApplicationStatus.Text = "Started to retrieve text files..."

OpenFileDialog1.Title = _

"Select the required text files..."

OpenFileDialog1.InitialDirectory = "c:\ACCESS\somefoldername"

OpenFileDialog1.FileName = " " ' am unsure if this is required

OpenFileDialog1.Filter = "*.txt|*.txt"

If OpenFileDialog1.ShowDialog() = Windows.Forms.DialogResult.OK Then.............

it seems I should then be able to loop through the selected filenames programatically after the user has selected his files and pressed CTRL-M.

Is some form of the construct:

For Each file_name as string in dlbwhatever.FileNames required

but this gives an error "can't convert a 1-dimensional string to string"

The whole thing is a box of worms, no doco posted anywhere that I can find

Is there a guru in the house



Answer this question

Populating/selecting/manipulating windows file names in a checked list box

  • DBG

    hi,

    you are welcome Allen, i'm not guru can i answer

    anyway you can try something like this


    Public Class Form1
    'this form contain a listbox and a button

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    Dim ofd As New OpenFileDialog
    ofd.Filter =
    "txt|*.txt"

    'allow multiselection in your open file dialog

    ofd.Multiselect = True

    Dim result As DialogResult = ofd.ShowDialog()
    'open file dialog doesnt' have ok button so make just check if its not cancel

    If Not result = Windows.Forms.DialogResult.Cancel Then

    'here its file name(s) with s to return array of file names

    Dim names() As String = ofd.FileNames
    For Each name As String In names
    'get just the file name as what i had taught in other thread
    ' if you gonna use those paths to open the files then you have to save the entire path or to remove the next line of code

    Dim fileName As String = System.IO.Path.GetFileName(name)
    ListBox1.Items.Add(fileName)
    Next

    End If

    End Sub
    End
    Class


     

    hope this helps



  • Ravenet

    I'd like to do something similar to this, but I want to be able to display all the files in a certain folder and allow the user to be able to open them. Most files will be PDFs and Excel files.

    any ideas on how to mod this code to do that


  • JKD

    "Using" is a new addition for VB 2005, but in earlier versions you can still call Ddispose explicitly. In the grand scheme of things it's not going to make a significant difference in this case, but it's good practice to always dispose all objects that support it once you're finished with them. If you get in good habits early when it doesn't really matter, it will already be second nature by the time it does matter. Disposal and garbage collection are related but they are not the same thing. The managed memory occupied by the object will be reclaimed by the garbage collector when it's needed, regardless of whether you dispose the object or not. The difference is that if you dispose the object, any system resources it holds, like file handles, etc. will be released immediately and will not be lost to the system while it waits for the garbage collector to call the object's Finalize method when it reclaims the memory.
  • szobi

    hi,

    you are welcome Allen actualy its not dialects or manythings that do the same thing i simply open the dialog just open it

    but Jm used using keyword using keyword open the dialog and when you finish it dispose it so it working in two things opening and disposing

    i didn't use using because i think GC will do that for me because i think its a temporary sub and doesn't do much but if it was a fundamental part of your work that get much data you suppose to use "using" as what JM did

    best regards



  • froggy77

    If you want the full paths in the CheckedListBox:

    Using ofd As New OpenFileDialog

    If ofd.ShowDialog() = Windows.Forms.DialogResult.OK Then

    Me.CheckedListBox1.Items.AddRange(ofd.FileNames)

    End If

    End Using

    If you want only the file names:

    Using ofd As New OpenFileDialog

    If ofd.ShowDialog() = Windows.Forms.DialogResult.OK Then

    For Each filePath As String In ofd.FileNames

    Me.CheckedListBox1.Items.Add(IO.Path.GetFileName(filePath))

    Next filePath

    End If

    End Using


  • SQLMcp

    Many thanks to Shak' and JM for 2 perfect answers. My prototype is running fine now - much yet to do tho'. As an ancient Prog/Analyst originally trained in 70's on Cobol/Fortran IV & 77/PL1G and Adabas Natural this transition to VB is proving difficult but I'm determined to see it through. It's not so much the OOP stuff that bothers me as I did a fair amount of work on MS ACCESS in early 90's but the depth of the VB language or should I say the group of dialects that seem to have been brought forward to 2005.

    Herewith supplied a case each of your favourite tipple.


  • Populating/selecting/manipulating windows file names in a checked list box