the old filelistbox ?

hawk1ns

Freshman
Joined
Jun 10, 2003
Messages
37
hello again :)

in an old program i used a filelistbox to display the content of a folder containing over 200 .gif and .jpg images , i could then choose one of these and have it placed into a picturebox..

can someone please suggest to me what they think would be the best way to do this on vb.net ?

Regards
Carl
 
Use a regular listbox, and write code using System.IO.Directory.GetFiles() to grab all the files in that directory in to a string array. You can then loop through that string array adding them all to the listbox. You can get just the filename portion using System.IO.Path.GetFilename().
 
Thankyou , i will give that a go.

i am impressed with vb.net , also very confused at the same time :)

before this i used vb5 and i have found the jump a little to much at times as i am a very new user :)

2 questions please...

1: could you give a small example of the code required please..

2: i see in other threads that the vb code is displayed in a white box .. hows this done on this forum :) lol

Kind Regards
Carl
 
Visual Basic:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim dr As Directory '////Imports System.IO must be at the top of your form.
        Dim i As Integer

        For i = 0 To dr.GetFiles("C:\").Length() - 1
            ListView1.Items.Add(dr.GetFiles("C:\").GetValue(i))
            '//// add the items from the directory to a listview ^^^
        Next

    End Sub

hope that helps a little :)
 
thanks , thats great ...
can you clarify what this states tho please ?

'////Imports System.IO must be at the top of your form.

what do i put at the top of the form ?

when i put the above code in i get a blue line under directory ..

Cheers for the help

Regards
Carl
 
it means that above your class, on the top of code you need to import classess, which makes a shortcut. Instead of typing System.IO.Directory you can type Directory.
So on top of your code type:
Visual Basic:
Imports System.IO
 
sorry maybe i should have been a little clearer , at the top of your form's code window , so you'd have something like this ( depending on your form's name )
Visual Basic:
Imports System.IO '///// very first line of code.
Imports System.Text'/// ignore that 1

Public Class Form1
    Inherits System.Windows.Forms.Form
'///////////////////////////////////////////////////

alternatively you can do this :
Visual Basic:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim dr As System.IO.Directory '/// directly use System.IO if you dont want to reference it at the top of form.
        Dim i As Integer

        For i = 0 To dr.GetFiles("C:\").Length() - 1
            ListView1.Items.Add(dr.GetFiles("C:\").GetValue(i))
            '//// add the items from the directory to a listview ^^^
        Next

    End Sub
 
thanks , i believe i am getting there , i can now click a button and have the files appear in the box , great :)

now i just need to get rid of the displayed path and just have the image name :)

i.e. at the moment i have c:\folder1\folder2\imagename.gif
and i just want imagename.gif displayed :)

BTW ... simple question ... how do you get the code above in the white box on this forum ?? lol

Regards
Carl
 
ok this will give you just the file name , eg "testing.txt"
Visual Basic:
    Private Sub Button10_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button10.Click
        Dim dr As System.IO.Directory '/// directly use System.IO if you dont want to reference it at the top of form.
        Dim fPath As System.IO.Path'//// use that.
        Dim i As Integer

        For i = 0 To dr.GetFiles("C:\").Length() - 1
            ListView1.Items.Add(fPath.GetFileName(dr.GetFiles("C:\").GetValue(i)))
            '//// add the items from the directory to a listview ^^^
        Next

    End Sub
 
This will get the names only:
Visual Basic:
Dim files() As String = System.IO.Directory.GetFiles("D:\")
For x As Integer = 0 To files.GetUpperBound(0) 'or you dim x normally
'if you use framework 1.0
     files(x) = System.IO.Path.GetFileName(files(x))
     listbox1.Items.Add(files(x))
Next

[edit] Oops :) to late [/edit]
 
ITS ME AGAIN ...

right i have this bit of code that puts the number of the selected item in to a text box...

If ListView1.SelectedIndices.Count > 0 Then
TextBox4.Text = ListView1.SelectedIndices(0).ToString

but how do i get the actual file name to appear in the text box instead ?

Cheers
 
Visual Basic:
    Private Sub ListView1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles ListView1.MouseUp
        If ListView1.Items.Count = 0 Then Exit Sub
        TextBox4.Text = ListView1.SelectedItems(0).Text
    End Sub
:)
 
Interesting hiccup

I noticed that when using the above sample and a Colums collection that when you call Listview1.Clear ( then reload the listbox) nothing appears in the list
I soon realized that i was clearing the column name with Listbox1.Clear and changed it to Listbox1.Items.Clear

Just a note in case you were thinking of using columns
 
Back
Top