Results 1 to 19 of 19

Thread: [RESOLVED] List Box

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2010
    Posts
    33

    Resolved [RESOLVED] List Box

    How do I get the selected files in this code to show in a list box?

    Code:
    Public Class Form1
    
    
        Private Sub BtBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            Dim BtBrowse As New FolderBrowserDialog()
    
            BtBrowse.SelectedPath = "C:\"
    
            If BtBrowse.ShowDialog() = Windows.Forms.DialogResult.OK Then
    
                For Each File As String In My.Computer.FileSystem.GetFiles(BtBrowse.SelectedPath, FileIO.SearchOption.SearchTopLevelOnly, "*.txt")
                Next
    
            End If
    
        End Sub
    
    End Class

    My lack of knowledge prohibits me from giving more info, so if you need more info, please ask and I will do my best to explain my desired results. I am just trying to learn.

    Thanks

  2. #2
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,826

    Re: List Box

    Once the items have been loaded, are you just wanting the selected item to be sent to the textbox when selected? Or when another button is pressed?

    You can simply utilize the SelectedIndexChanged ListBox event and use the following code:

    vb Code:
    1. TextBox1.Text = ListBox1.Text.ToString

    If you use this exact code in that specified event, the text will appear automatically when clicked.
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  3. #3
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: List Box

    "Once the items have been loaded" -- I think that's what the OP is asking for... badly worded but sometimes that happens when you're not sure what you're after.

    I think this is what you are looking for:
    Code:
                For Each File As String In My.Computer.FileSystem.GetFiles(BtBrowse.SelectedPath, FileIO.SearchOption.SearchTopLevelOnly, "*.txt")
                    Listbox1.Items.Add(File)
                Next
    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4
    Wait... what? weirddemon's Avatar
    Join Date
    Jan 2009
    Location
    USA
    Posts
    3,826

    Re: List Box

    Oh. I think I saw "ListBox" as TextBox or something. Eh... it's been a long day :P
    CodeBank contributions: Process Manager, Temp File Cleaner

    Quote Originally Posted by SJWhiteley
    "game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....

  5. #5
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: List Box

    day? Pffft! try week.... fortunately it's now Miller time for me. Woo-Hoo!

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6

    Thread Starter
    Member
    Join Date
    Mar 2010
    Posts
    33

    Re: List Box

    Reply to #2
    Weirddemon

    hmmm... not sure what you are asking/telling me? I am completly new to programing sorry, but I know all about Enums now... :-)

    All I want to do for now is to press Button1 and have a list of the files in the selected folder show in ListBox1.

    I am stair-stepping, try to learn one thing at a time. I have a grand scheme at the end but for now, I can only handle one step at a time.


    Thanks
    Last edited by VB_begginer; Mar 26th, 2010 at 04:58 PM.

  7. #7

    Thread Starter
    Member
    Join Date
    Mar 2010
    Posts
    33

    Re: List Box

    Reply to #3
    tech & wierd

    This is my code now, I think I am missing a step? You mentioned "Once the items have been loaded"

    I am not sure that I have loaded anything because my list box is still empty. So I can assume now that the original code posted above is just a browse function, the code you gave me is a list function, now I must figure out how to load the desired files. Makes sense. (but I don't know how to load items?) I will search the forum for how to load items and hopefully check back for maybe a reply on how to load items at a later time. hint, hint.

    Thanks


    Code:
    Public Class Form1
    
    
        Private Sub BtBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            Dim BtBrowse As New FolderBrowserDialog()
    
            BtBrowse.SelectedPath = "C:\"
    
            If BtBrowse.ShowDialog() = Windows.Forms.DialogResult.OK Then
    
                For Each File As String In My.Computer.FileSystem.GetFiles(BtBrowse.SelectedPath, FileIO.SearchOption.SearchTopLevelOnly, "*.txt")
                    ListBox1.Items.Add(File)
                Next
    
            End If
    
        End Sub
    
    End Class
    Last edited by VB_begginer; Mar 26th, 2010 at 04:58 PM.

  8. #8

    Thread Starter
    Member
    Join Date
    Mar 2010
    Posts
    33

    Re: List Box

    I got it to work... It helps if I am looking for the right file extention.

    Thanks guys

  9. #9

    Thread Starter
    Member
    Join Date
    Mar 2010
    Posts
    33

    Re: [RESOLVED] List Box

    How do I get the listbox to only show the files? Example being only "file.txt" not c:\path......file.txt?

    I am finding VB to be understandable once someone shows me the code, but I am finding it hard to be intuative.

    Thanks

  10. #10
    Hyperactive Member The Fire Snake's Avatar
    Join Date
    Sep 2009
    Location
    USA
    Posts
    401

    Re: [RESOLVED] List Box

    Quote Originally Posted by VB_begginer View Post
    How do I get the listbox to only show the files? Example being only "file.txt" not c:\path......file.txt?

    I am finding VB to be understandable once someone shows me the code, but I am finding it hard to be intuative.

    Thanks
    As with so many things in VB.Net and programming in general there are many ways to do the same thing. If I were to do this then I would have done the following. Please note the Directory classes GetFiles() method returns file name with paths and the DirectoryInfo classes GetFiles() method returns file names without paths.

    vb Code:
    1. Dim dirInfo As New DirectoryInfo("C:\")
    2.  
    3. ' Getting the files with .txt extension
    4. Dim files() As FileInfo = dirInfo.GetFiles("*.txt")
    5.  
    6. ' Adding the filenames to the listbox
    7. For Each file As FileInfo In files
    8.    ListBox1.Items.Add(file.Name & Environment.NewLine())
    9. Next

  11. #11

    Thread Starter
    Member
    Join Date
    Mar 2010
    Posts
    33

    Re: [RESOLVED] List Box

    Fire Snake, thanks for the repy

    The following is the code you gave me, I have a question about it

    Code:
    Public Class Form1
    
    
        Private Sub BtBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtBrowse.Click
    
            Dim DirInfo As New DirectoryInfo("C;\")
            Dim files() As FileInfo = DirInfo.GetFiles("*.*")
    
            For Each file As FileInfo In files
                ListBox1.Items.Add(file.Name & Enviroment.NewLine())
            Next
    
        End Sub
    
    End Class
    My erro box tells me that:
    Type 'DirectoryInfo' is not defined
    Type 'FileInfo' is not defined
    Type 'FileIno' is not defined

    Please understand that I am very new to this. I am using Visual Basic 2008 if this makes a difference between VB.net as you identivied the version in your post reply.

    I am not sure what I am missing or what I am doing wrong.

    Thanks

  12. #12
    Hyperactive Member The Fire Snake's Avatar
    Join Date
    Sep 2009
    Location
    USA
    Posts
    401

    Re: [RESOLVED] List Box

    Quote Originally Posted by VB_begginer View Post
    Fire Snake, thanks for the repy

    The following is the code you gave me, I have a question about it

    Code:
    Public Class Form1
    
    
        Private Sub BtBrowse_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtBrowse.Click
    
            Dim DirInfo As New DirectoryInfo("C;\")
            Dim files() As FileInfo = DirInfo.GetFiles("*.*")
    
            For Each file As FileInfo In files
                ListBox1.Items.Add(file.Name & Enviroment.NewLine())
            Next
    
        End Sub
    
    End Class
    My erro box tells me that:
    Type 'DirectoryInfo' is not defined
    Type 'FileInfo' is not defined
    Type 'FileIno' is not defined

    Please understand that I am very new to this. I am using Visual Basic 2008 if this makes a difference between VB.net as you identivied the version in your post reply.

    I am not sure what I am missing or what I am doing wrong.

    Thanks
    No Problem. Before the line that says Public Class Form1, type the following:
    vb Code:
    1. Imports System.IO

    What this does is it allows you to use the DirectoryInfo class and its associated members(you could also use the full namespace name without the need for imports, but I don't want to get into the details and confuse you). Without this import your code won't "see" the DirectoryInfo class or the FileInfo class. Just remember that different classes are in different places and many times you will need to import the "library" where they are housed, in order to use them. The VB environment imports some "libraries" by default but others you will need to import yourself, like in this case. Hope it makes sense. There is so much stuff out there and it will take time to get it all straight. I myself have only been using VB.Net for like a year or so, but I learn so much each day. Ask if you don't get it.
    Last edited by The Fire Snake; Mar 29th, 2010 at 02:02 PM.

  13. #13

    Thread Starter
    Member
    Join Date
    Mar 2010
    Posts
    33

    Re: [RESOLVED] List Box

    Hey it worked... cool! :-)

    What does this part of the code do? I took it out of my code because it was sayin the envirment was not declared. but worked without it.

    Code:
    & Enviroment.NewLine


    This is all starting to make sense as I learn new things. But it is so vast and confusing that my head spins, it amazes me that people can read, write and understand this stuff.

    But now I understand that I have load libraries,

    one step at a time

    Thanks

  14. #14

    Thread Starter
    Member
    Join Date
    Mar 2010
    Posts
    33

    Re: [RESOLVED] List Box

    What does IO mean in
    System.IO?

  15. #15
    Hyperactive Member The Fire Snake's Avatar
    Join Date
    Sep 2009
    Location
    USA
    Posts
    401

    Re: [RESOLVED] List Box

    Quote Originally Posted by VB_begginer View Post
    Hey it worked... cool! :-)
    What does this part of the code do? I took it out of my code because it was sayin the envirment was not declared. but worked without it.

    Code:
    & Enviroment.NewLine
    Environment.NewLine gives you the newline character so 2 things will be on separate lines. Like for instance you want one word on one line and one on another.

    I thought you needed it here, but in this case you don't. The Listbox automatically lists each of its items on a separate line.
    Remember to click on the scales to the left and rep me if I helped

  16. #16
    Hyperactive Member The Fire Snake's Avatar
    Join Date
    Sep 2009
    Location
    USA
    Posts
    401

    Re: [RESOLVED] List Box

    Quote Originally Posted by VB_begginer View Post
    What does IO mean in
    System.IO?
    IO means InputOutput as this "library" has many classes that deal with common input output functions like reading from files, writing to files etc.
    Remember to click on the scales to the left and rep me if I helped

  17. #17
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: [RESOLVED] List Box

    Quote Originally Posted by VB_begginer View Post
    What does IO mean in
    System.IO?
    IO = Input Output

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  18. #18

    Thread Starter
    Member
    Join Date
    Mar 2010
    Posts
    33

    Re: [RESOLVED] List Box

    k, cool

    just one last question and I will move on to a new post. Since it was brought up in this post.

    How would you declare an Enviroment and where would you typicly use this function? (is it a function?)
    would you use it in a message box, maybe? to have multiple line?

  19. #19
    Hyperactive Member The Fire Snake's Avatar
    Join Date
    Sep 2009
    Location
    USA
    Posts
    401

    Re: [RESOLVED] List Box

    Quote Originally Posted by VB_begginer View Post
    k, cool

    just one last question and I will move on to a new post. Since it was brought up in this post.

    How would you declare an Enviroment and where would you typicly use this function? (is it a function?)
    would you use it in a message box, maybe? to have multiple line?
    Environment is a class not a function. I just took a look and it contains all shared members, so you wouldn't need to instantiate it. You can use it by just using the classname.member notation.
    NewLine() is a property that exists in the Environment class. Since NewLine() is a shared property you would just need to use the classname.member notation as mentioned.

    Ex:
    vb Code:
    1. Environment.NewLine()

    Whenever you want text to appear on multiple line you would use this. You can use it in a message box, like you mentioned, or in a text file etc
    Remember to click on the scales to the left and rep me if I helped

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