|
-
Mar 26th, 2010, 04:08 PM
#1
Thread Starter
Member
[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
-
Mar 26th, 2010, 04:13 PM
#2
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:
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
 Originally Posted by SJWhiteley
"game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....
-
Mar 26th, 2010, 04:25 PM
#3
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
-
Mar 26th, 2010, 04:32 PM
#4
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
 Originally Posted by SJWhiteley
"game trainer" is the same as calling the act of robbing a bank "wealth redistribution"....
-
Mar 26th, 2010, 04:35 PM
#5
Re: List Box
day? Pffft! try week.... fortunately it's now Miller time for me. Woo-Hoo!
-tg
-
Mar 26th, 2010, 04:36 PM
#6
Thread Starter
Member
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.
-
Mar 26th, 2010, 04:53 PM
#7
Thread Starter
Member
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.
-
Mar 26th, 2010, 05:04 PM
#8
Thread Starter
Member
Re: List Box
I got it to work... It helps if I am looking for the right file extention.
Thanks guys
-
Mar 27th, 2010, 08:37 AM
#9
Thread Starter
Member
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
-
Mar 27th, 2010, 12:29 PM
#10
Hyperactive Member
Re: [RESOLVED] List Box
 Originally Posted by VB_begginer
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:
Dim dirInfo As New DirectoryInfo("C:\")
' Getting the files with .txt extension
Dim files() As FileInfo = dirInfo.GetFiles("*.txt")
' Adding the filenames to the listbox
For Each file As FileInfo In files
ListBox1.Items.Add(file.Name & Environment.NewLine())
Next
-
Mar 29th, 2010, 01:34 PM
#11
Thread Starter
Member
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
-
Mar 29th, 2010, 01:55 PM
#12
Hyperactive Member
Re: [RESOLVED] List Box
 Originally Posted by VB_begginer
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:
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.
-
Mar 29th, 2010, 03:21 PM
#13
Thread Starter
Member
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
-
Mar 29th, 2010, 03:42 PM
#14
Thread Starter
Member
Re: [RESOLVED] List Box
What does IO mean in
System.IO?
-
Mar 29th, 2010, 03:52 PM
#15
Hyperactive Member
Re: [RESOLVED] List Box
 Originally Posted by VB_begginer
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 
-
Mar 29th, 2010, 03:54 PM
#16
Hyperactive Member
Re: [RESOLVED] List Box
 Originally Posted by VB_begginer
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 
-
Mar 29th, 2010, 03:54 PM
#17
Re: [RESOLVED] List Box
 Originally Posted by VB_begginer
What does IO mean in
System.IO?
IO = Input Output
-tg
-
Mar 29th, 2010, 04:29 PM
#18
Thread Starter
Member
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?
-
Mar 29th, 2010, 05:02 PM
#19
Hyperactive Member
Re: [RESOLVED] List Box
 Originally Posted by VB_begginer
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:
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|