hawk1ns Posted June 17, 2003 Posted June 17, 2003 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 Quote
*Gurus* divil Posted June 17, 2003 *Gurus* Posted June 17, 2003 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(). Quote MVP, Visual Developer - .NET Now you see why evil will always triumph - because good is dumb. My free .NET Windows Forms Controls and Articles
hawk1ns Posted June 17, 2003 Author Posted June 17, 2003 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 Quote
Leaders dynamic_sysop Posted June 17, 2003 Leaders Posted June 17, 2003 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 :) Quote
hawk1ns Posted June 17, 2003 Author Posted June 17, 2003 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 Quote
*Experts* mutant Posted June 17, 2003 *Experts* Posted June 17, 2003 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 Quote
hawk1ns Posted June 17, 2003 Author Posted June 17, 2003 thanks for clearing that up :) :) Regards Carl Quote
Leaders dynamic_sysop Posted June 17, 2003 Leaders Posted June 17, 2003 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 Quote
hawk1ns Posted June 17, 2003 Author Posted June 17, 2003 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 Quote
Leaders dynamic_sysop Posted June 17, 2003 Leaders Posted June 17, 2003 [ vb ] your code [ / vb ] without the spaces ;) Quote
hawk1ns Posted June 17, 2003 Author Posted June 17, 2003 can you suggest how to display just imagename.gif and not . c:\folder1\folder2\imagename.gif thanks Quote
Leaders dynamic_sysop Posted June 17, 2003 Leaders Posted June 17, 2003 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 Quote
*Experts* mutant Posted June 17, 2003 *Experts* Posted June 17, 2003 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] Quote
hawk1ns Posted June 17, 2003 Author Posted June 17, 2003 FANTASTIC !! Just what i wanted :) Thankyou for your help here Best wishes Carl Quote
hawk1ns Posted June 17, 2003 Author Posted June 17, 2003 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 Quote
Leaders dynamic_sysop Posted June 17, 2003 Leaders Posted June 17, 2003 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 :) Quote
archer_coal Posted June 18, 2003 Posted June 18, 2003 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 Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.