Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

  • *Gurus*
Posted
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().

MVP, Visual Developer - .NET

 

Now you see why evil will always triumph - because good is dumb.

 

My free .NET Windows Forms Controls and Articles

Posted

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

  • Leaders
Posted

   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 :)

Posted

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

  • *Experts*
Posted

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:

Imports System.IO

  • Leaders
Posted

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 )

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 :

   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

Posted

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

  • Leaders
Posted

ok this will give you just the file name , eg "testing.txt"

   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

  • *Experts*
Posted

This will get the names only:

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]

Posted

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

  • Leaders
Posted

   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

:)

Posted

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...