-
Sep 26th, 2020, 07:22 PM
#1
Thread Starter
Member
[RESOLVED] View files in folder based on button
Hey all pretty new at this. Situation I am in. I can already bring up list of files in a director. Issues I am trying to figure out is.
Form 1
Button 1 - folder1
Button 2 - folder2
Button 3 - folder3
When a button is pushed then
Form2.Show() is done
form2 I have
Code:
Dim directory = "c:\dir\" (i need folder# to insert after the dir\ but not sure how to send it.
Dim files() As System.IO.FileInfo
Dim dirinfo As New System.IO.DirectoryInfo(directory)
files = dirinfo.GetFiles("*", IO.SearchOption.AllDirectories)
For Each File In files
ListBox1.Items.Add(File)
Next
So when somebody hit button 1 - 3 it will display whatever files are in that directory in the list box.
-
Sep 26th, 2020, 07:54 PM
#2
Re: View files in folder based on button
You’d need to modify the constructor in Form2, so you could pass in the folder path when the form is created. Show me your Form2 code and I can explain how to modify it...
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Sep 26th, 2020, 08:01 PM
#3
Re: View files in folder based on button
It’ll be something like this...
Code:
Public Class Form2
Public folderPath As String
Public Sub New(ByVal folderPath As String)
Me.folderPath = folderPath
End Sub
‘ your code here. Me.folderPath is your directory path
End Class
Then to call it...
Code:
Dim frm As New Form2(“c:\aDirectory”)
frm.Show
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Sep 26th, 2020, 08:04 PM
#4
Re: View files in folder based on button
Actually, I missed a line. The first line in sub new should be...
Code:
InitializeComponent
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Sep 26th, 2020, 08:06 PM
#5
Thread Starter
Member
Re: View files in folder based on button
Thank paul. This is what I currently have in my form 2
Code:
Public Class EntryLogs
Private Sub EntryLogs_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim directory = "c:\elagent\tpia\" (The tpia is one of the folders I need to be able to change from what is chosen in form 1
Dim files() As System.IO.FileInfo
Dim dirinfo As New System.IO.DirectoryInfo(directory)
files = dirinfo.GetFiles("*", IO.SearchOption.AllDirectories)
For Each File In files
ListBox1.Items.Add(File)
Next
End Sub
Form 1 code is:
Code:
Private Sub ViewDailyLogToolStripMenuItem2_Click(sender As Object, e As EventArgs) Handles ViewDailyLogToolStripMenuItem2.Click
EntryLogs.Show()
'opendir = "tpia"
End Sub
Private Sub ViewDailyLogToolStripMenuItem1_Click(sender As Object, e As EventArgs) Handles ViewDailyLogToolStripMenuItem1.Click
EntryLogs.Show()
'opendir = "chat"
End Sub
Private Sub ViewDailyLogToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles ViewDailyLogToolStripMenuItem.Click
EntryLogs.Show()
'opendir = "phone"
End Sub
As you can tell I currently have opendir marked out due to errors. So I need to have the tpia change to chat, phone, etc.
-
Sep 26th, 2020, 08:10 PM
#6
Re: View files in folder based on button
Check post #3 and post #4
You just need to add in my code, then open EntryLogs as an instance as I showed in post #3
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Sep 26th, 2020, 08:13 PM
#7
Thread Starter
Member
Re: View files in folder based on button
Thanks Paul it works perfect.
-
Sep 27th, 2020, 09:26 PM
#8
Thread Starter
Member
Re: [RESOLVED] View files in folder based on button
Hey paul sorry for the bother.
Just finishing up the program I am working and was wondering with the code you provided is there a way to reverse the order of the list in the listbox, I have tried the default listbox1.items.reverse with no luck except for breaking it. If not no worries. I am sure the people are using it know how to scroll to the bottom of the list...lol
-
Sep 27th, 2020, 09:34 PM
#9
Re: [RESOLVED] View files in folder based on button
Firstly, you should not be doing this:
vb.net Code:
For Each File In files
ListBox1.Items.Add(File)
Next
Either call AddRange to add the lot in one go or else set the DataSource to bind.
As for reversing, files is an array and there is an Array.Reverse method.
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
|