|
-
Apr 3rd, 2007, 08:47 PM
#1
Thread Starter
Hyperactive Member
[2005]Loading folders and making checkbox
I am developing an application that moves files to directories. But I'm going to have to update it a lot and do a lot of meaningless uploads when I can just have the user do plugins for the app, instead of uploading the new version every time.
So how would I go about loading folders from a directory, then making a checkbox with the folder name. To be honest, I don't know where to start. Thanks.
-
Apr 3rd, 2007, 09:31 PM
#2
Re: [2005]Loading folders and making checkbox
I would suggest using a CheckedListBox:
vb Code:
Dim files As String() = IO.Directory.GetFiles("folder path here")
For i As Integer = 0 To files.GetUpperBound(0) Step 1
'Remove the folder path.
files(i) = IO.Path.GetFileName(files(i))
Next i
myCheckedListBox.Items.AddRange(files)
-
Apr 3rd, 2007, 09:49 PM
#3
Thread Starter
Hyperactive Member
Re: [2005]Loading folders and making checkbox
Ok, thanks but can you add comments? Because I want to do the same thing except not check box but buttons. And I really don't understand your code. Thanks.
-
Apr 3rd, 2007, 09:57 PM
#4
Thread Starter
Hyperactive Member
Re: [2005]Loading folders and making checkbox
Ok, that has fixed one problem, but brought up another. Since the app make the check boxes on startup, how do it make it so when a button is click it copies the folder? I'm stumped here...
-
Apr 3rd, 2007, 10:44 PM
#5
Re: [2005]Loading folders and making checkbox
Are you saying that when the user clicks a button you want to copy the selected files from the current folder to another? If so:
vb Code:
Dim sourceFolderPath As String 'You should already have this.
Dim targetFolderPath As String 'Presumably you know this too.
For Each fileName As String In myCheckedListBox.CheckedItems
IO.File.Copy(IO.Path.Combine(sourceFolderPath, fileName), _
IO.Path.Combine(targetFolderPath, fileName))
Next fileName
If you don't understand the code you can look up each type and member in the MSDN library to get an understanding of what it is.
-
Apr 4th, 2007, 07:08 AM
#6
Thread Starter
Hyperactive Member
Re: [2005]Loading folders and making checkbox
I understand that code, all except one part. Which I'm not sure is even needed.
But in the other code, what I really don't understand is this:
Code:
For i As Integer = 0 To files.GetUpperBound(0) Step 1
-
Apr 4th, 2007, 06:12 PM
#7
Re: [2005]Loading folders and making checkbox
That part is very needed. You want to loop from the first index in the 'files' array to the last index. GetUpperBound(0) returns the last index in the array. It returns the same value as (Length - 1). You did look it up in MSDN didn't you?
As for the Step value, you should ALWAYS include a Step on your For loops if they use variable bounds. The only time it is really safe to omit it is if you are using literal or constant values for both the start and end values of the counter. If you don't include the Step then the default value will be used, which is based on the relative values of the start and end counter values. If the end is greater than the start then the default step is 1. If the end is less than the start then the default step is -1.
Now consider this. Let's say that there are 5 files in the array, thus GetUpperBound returns 4. If you don't specify a Step then 1 will be used, thus the loop will use counter values of 0, 1, 2, 3 and 4. No problem there. Now let's say that there are no files in the array because the folder is empty. GetUpperBound will now return -1, so if you don't specify a Step then the default -1 will be used and the loop counter will take the values 0 and -1. Your app will now crash with an IndexOutOfRangeException. If you had specified a Step of 1 then the loop would never be entered, which is exactly what you want: no elements in the array should mean no looping.
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
|