Results 1 to 8 of 8

Thread: [RESOLVED] Listbox Question

  1. #1

    Thread Starter
    Member yacky's Avatar
    Join Date
    Nov 2009
    Posts
    38

    Resolved [RESOLVED] Listbox Question

    How can I make listbox that contains items from some folder...

    For example:
    I have 10 .txt files in 1 folder. In my program when I click button "view" there is a listbox that contains names of that .txt files.

  2. #2
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: Listbox Question

    vb Code:
    1. ListBox1.Items.Clear()
    2. ListBox1.Items.AddRange(IO.Directory.GetFiles(folderPath, "*.txt"))

  3. #3

    Thread Starter
    Member yacky's Avatar
    Join Date
    Nov 2009
    Posts
    38

    Re: Listbox Question

    It works but I want only name of the file to be shown in listbox

  4. #4
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Listbox Question

    IO.Path.GetFileName(ListBox1.Items(i).ToString)
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  5. #5
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: Listbox Question

    vb Code:
    1. Dim fbd As New FolderBrowserDialog
    2. If fbd.ShowDialog = DialogResult.OK Then
    3.     Dim di As New IO.DirectoryInfo(fbd.SelectedPath)
    4.     ListBox1.Items.Clear()
    5.     ListBox1.Items.AddRange(di.GetFiles("*.txt"))
    6. End If

  6. #6

    Thread Starter
    Member yacky's Avatar
    Join Date
    Nov 2009
    Posts
    38

    Re: Listbox Question

    Without FolderBrowserDialog. Just like first time but i need only name of the files...

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: Listbox Question

    vb Code:
    1. Dim di As New IO.DirectoryInfo("c:\folderPath")
    2. ListBox1.Items.Clear()
    3. ListBox1.Items.AddRange(di.GetFiles("*.txt"))

    will give you the filenames. i.e.

    filename.txt

    to display just the filenames without the extension:

    vb Code:
    1. ListBox1.Items.Clear()
    2. for each filename as string in IO.Directory.GetFiles(folderPath, "*.txt")
    3.     ListBox1.Items.Add(IO.Path.GetFileNamewithoutextension(filename))
    4. next

  8. #8

    Thread Starter
    Member yacky's Avatar
    Join Date
    Nov 2009
    Posts
    38

    Re: Listbox Question

    Thank you !

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width