Results 1 to 13 of 13

Thread: list box

  1. #1

    Thread Starter
    Member
    Join Date
    Nov 2009
    Posts
    56

    list box

    hey guys i got my list box working.but it lists the path and the name..

    ist it possible list only the image it self?

    Code:
    Imports System.IO
    Public Class Form3
    
        Private Sub FindFiles_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FindFiles.Click
            Dim thefiles() As String
            'change the path to a path that exists in your machine
            thefiles = Directory.GetFiles("C:\Users\TECHKER\Desktop\PROGRAMMING\PROJECTS\trainersTool\images\plyo")
            Me.ListBox1.DataSource = thefiles
        End Sub
    
        Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
    
        End Sub
    End Class

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

    Re: list box

    try this:

    vb Code:
    1. Dim di As New IO.DirectoryInfo("C:\Users\TECHKER\Desktop\PROGRAMMING\PROJECTS\trainersTool\images\plyo")
    2. Dim theFiles = (From fi In di.GetFiles _
    3.                         Select fi.Name).ToList
    4.  
    5. Me.ListBox1.DataSource = theFiles

  3. #3

    Thread Starter
    Member
    Join Date
    Nov 2009
    Posts
    56

    Re: list box

    cool this shows the file name.now can i get the pic to show?

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: list box

    Quote Originally Posted by .paul. View Post
    try this:

    vb Code:
    1. Dim di As New IO.DirectoryInfo("C:\Users\TECHKER\Desktop\PROGRAMMING\PROJECTS\trainersTool\images\plyo")
    2. Dim theFiles = (From fi In di.GetFiles _
    3.                         Select fi.Name).ToList
    4.  
    5. Me.ListBox1.DataSource = theFiles
    There's not actually any need to use LINQ here:
    vb.net Code:
    1. Me.ListBox1.DisplayMember = "Name"
    2. Me.ListBox1.DataSource = di.GetFiles()
    The advantage of binding is that you still have the full FileInfo object available. You might also set the ValueMember to "FullName" and then you can get the full path of the selected file from the SelectedValue.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Member
    Join Date
    Nov 2009
    Posts
    56

    Re: list box

    true..but i still need to figure out how to show the image..

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: list box

    ListBoxes don't show images. You'd have to either customise a ListBox or use a different control(s). How you display the images aside, how you get the Images is to first get the file paths, then call Image.FromFile for each one, e.g.
    vb Code:
    1. Dim paths As String() = IO.Directory.GetFiles("folder path here")
    2. Dim upperBound As Integer = paths.GetUpperBound(0)
    3. Dim images(upperBound) As Image
    4.  
    5. For index As Integer = 0 To upperBound
    6.     images(index) = Image.FromFile(paths(index))
    7. Next
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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

    Re: list box

    you could use a datagridview rather than a listbox. i'd still recommend using LINQ in vb2008:

    vb Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    4.         'this gets all the icons from a folder containing icons
    5.         'displaying the filename in the first column in the dgv
    6.         'the full path to the icon in the hidden second column
    7.         '+ the actual icon in the third column
    8.         Dim files = (From fi As IO.FileInfo In New IO.DirectoryInfo("C:\Users\Paul\Pictures\ICO").GetFiles _
    9.                      Select New With { _
    10.                      .filename = fi.Name,
    11.                      .fullName = fi.FullName,
    12.                      .image = Image.FromFile(fi.FullName) _
    13.                      }).ToList
    14.  
    15.         DataGridView1.DataSource = files
    16.         DataGridView1.Columns(1).Visible = False
    17.  
    18.     End Sub
    19.  
    20. End Class

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: list box

    Quote Originally Posted by .paul. View Post
    i'd still recommend using LINQ in vb2008
    In that case, yes, because it adds value. In the other case it didn't.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  9. #9

    Thread Starter
    Member
    Join Date
    Nov 2009
    Posts
    56

    Re: list box

    Quote Originally Posted by .paul. View Post
    you could use a datagridview rather than a listbox. i'd still recommend using LINQ in vb2008:

    vb Code:
    1. Public Class Form1
    2.  
    3.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    4.         'this gets all the icons from a folder containing icons
    5.         'displaying the filename in the first column in the dgv
    6.         'the full path to the icon in the hidden second column
    7.         '+ the actual icon in the third column
    8.         Dim files = (From fi As IO.FileInfo In New IO.DirectoryInfo("C:\Users\Paul\Pictures\ICO").GetFiles _
    9.                      Select New With { _
    10.                      .filename = fi.Name,
    11.                      .fullName = fi.FullName,
    12.                      .image = Image.FromFile(fi.FullName) _
    13.                      }).ToList
    14.  
    15.         DataGridView1.DataSource = files
    16.         DataGridView1.Columns(1).Visible = False
    17.  
    18.     End Sub
    19.  
    20. End Class
    this form is giving me errors?

    Error 2 Expression expected. C:\Users\TECHKER\Desktop\PROGRAMMING\PROJECTS\WindowsApplication1\WindowsApplication1\Form4.vb 26 41 WindowsApplication1


    Error 3 Leading '.' or '!' can only appear inside a 'With' statement. C:\Users\TECHKER\Desktop\PROGRAMMING\PROJECTS\WindowsApplication1\WindowsApplication1\Form4.vb 27 22 WindowsApplication1

    Error 4 Name 'fi' is not declared. C:\Users\TECHKER\Desktop\PROGRAMMING\PROJECTS\WindowsApplication1\WindowsApplication1\Form4.vb 27 34 WindowsApplication1

  10. #10
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: list box

    Try my ColorListBox control (link in signature). It allows you to display images. You won't be able to use data binding though; you'd have to loop through the filenames and add each item separately, like so
    Code:
    Dim di As New IO.DirectoryInfo("C:\Users\TECHKER\Desktop\PROGRAMMING\PROJECTS\trainersTool\images\plyo")
    
    Dim name As String
    Dim img As Image
    For Each fi As FileInfo In di.GetFiles()
       name = fi.Name
       img = Image.FromFile(fi.FullName)
       ColorListBox1.Items.Add(name, Color.Black, img)
    Next
    or something like that anyway.

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

    Re: list box

    copy the code exactly as it is with no extra spaces between lines + make sure the line continuation characters (_) are still in the code as in the posting

  12. #12

    Thread Starter
    Member
    Join Date
    Nov 2009
    Posts
    56

    Re: list box

    Quote Originally Posted by NickThissen View Post
    Try my ColorListBox control (link in signature). It allows you to display images. You won't be able to use data binding though; you'd have to loop through the filenames and add each item separately, like so
    Code:
    Dim di As New IO.DirectoryInfo("C:\Users\TECHKER\Desktop\PROGRAMMING\PROJECTS\trainersTool\images\plyo")
    
    Dim name As String
    Dim img As Image
    For Each fi As FileInfo In di.GetFiles()
       name = fi.Name
       img = Image.FromFile(fi.FullName)
       ColorListBox1.Items.Add(name, Color.Black, img)
    Next
    or something like that anyway.
    erro on cololistbox1 not declaired?

  13. #13

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