Results 1 to 7 of 7

Thread: [2005]Loading folders and making checkbox

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2006
    Posts
    343

    Exclamation [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.

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

    Re: [2005]Loading folders and making checkbox

    I would suggest using a CheckedListBox:
    vb Code:
    1. Dim files As String() = IO.Directory.GetFiles("folder path here")
    2.  
    3. For i As Integer = 0 To files.GetUpperBound(0) Step 1
    4.     'Remove the folder path.
    5.     files(i) = IO.Path.GetFileName(files(i))
    6. Next i
    7.  
    8. myCheckedListBox.Items.AddRange(files)
    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

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2006
    Posts
    343

    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.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2006
    Posts
    343

    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...

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

    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:
    1. Dim sourceFolderPath As String 'You should already have this.
    2. Dim targetFolderPath As String 'Presumably you know this too.
    3.  
    4. For Each fileName As String In myCheckedListBox.CheckedItems
    5.     IO.File.Copy(IO.Path.Combine(sourceFolderPath, fileName), _
    6.                  IO.Path.Combine(targetFolderPath, fileName))
    7. 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.
    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

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Oct 2006
    Posts
    343

    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

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

    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.
    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

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