CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14
  1. #1
    Join Date
    Dec 2008
    Posts
    4

    Selecting multiple folders in a visual basic 6 application

    Hi all,

    Could someone please tell me what the best way is to present the user with a dialog box that can be used to select multiple folders?

    I am creating a backup program which requires that the user can select the file(s) and folder(s) they wish to backup, these will then be added to a "files to backup" list box for user review.
    I have managed to implement a multiple file selection system using the visual basic common dialog box controls (implemented using the API), but this does not allow for selection of multiple folders.
    Even if this was possible, it would be difficult for the program to determine whether the user wishes to enter a folder or select it.
    i have looked on this forum and others, but so far no luck.
    Any help would be much appreciated.

  2. #2
    Join Date
    Jan 2006
    Location
    Pearl of the orient
    Posts
    304

    Re: Selecting multiple folders in a visual basic 6 application

    Perhaps in listbox where its Multiselect is set to true?

  3. #3
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: Selecting multiple folders in a visual basic 6 application

    The answer to that greatly depends on what control you have used to implement your file/folder list.
    If you use the TreeView or the ListView as contained in 'Microsoft Common Controls 6.0' the answer is: use the .CheckBoxes property to allow checkboxes for each individual item. This is best to switch individual items on and off.
    The CommonDialog is not suitable for the task. Although you can select multiple files, you can not select multiple folders. I'd suggest to populate a TreeView with the files and folders of a selected drive and use checkboxes for the selection of individual branches.
    If you like to use the TreeView, I can give you a sample how to use it tomorrow.

  4. #4
    Join Date
    Dec 2008
    Posts
    4

    Re: Selecting multiple folders in a visual basic 6 application

    Hi WoF,

    Sorry for the late reply, i was away for a few weeks, got back a few days ago.

    Yes please could you send me the code for the tree view?

    What i wanted was a windows-explorer type interface with a list view and tree view.
    I wanted them to be able to select multiple folders with the control+up and down arrows, but tree views do not allow selection of folders using this method.

    I have actually ended up using drive and directory list boxes, with an "add folder" button, the user selects a folder and clicks add.
    this seems to work ok - i use the common open dialog box for selecting multiple files.
    but i would still be interested in the tree view code.

    Thanks for the tip
    Ryan Hutchings

  5. #5
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: Selecting multiple folders in a visual basic 6 application

    Well, this sample shows how to fill a treeview with file and folder information of a given drive.
    I have activated the checkbox feature for you to see how this works.
    Certainly, you would have to expand the program. After having selected lots of folders with the checkboxes, you have to walk through all items and collect all checked foldernames.
    Attached Files Attached Files

  6. #6
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Selecting multiple folders in a visual basic 6 application

    Both the standard file browser dialog, as well as the folder browser allow multi-select. Just use those, and split the items. Here's an old sample.. (scroll down)

    Code:
    Option Explicit
    
    ' cdlCFPrinterFonts &H2
    
    ' Save File Options
    '&H2 - Forces a warning before overwriting a file
    '&H8 - Stops default directory from changing
    '&H200 - more than one file can be selected.
    '&H1000 - This makes it so the file must exist.
    '&H2000 - This warns the user before creating a new file.
    
    Private Sub cmdOpen_Click()
      ' CancelError is True.
       On Error GoTo ErrHandler
       CommonDialog1.InitDir = App.Path
       ' Set Flags
        CommonDialog1.Flags = cdlOFNAllowMultiselect Or cdlOFNLongNames
       ' Set filters.
       CommonDialog1.Filter = "All Files (*.*)|*.*|Text" & _
          "Files (*.txt)|*.txt|Batch Files (*.bat)|*.bat"
       '  CommonDialog1.Filter = "AutoForm Files (*.doc) (*.rtf)|*.doc;*.rtf|All " & _
              "Files (*.*)|*.*"
       
       ' Specify default filter.
       CommonDialog1.FilterIndex = 2 ' Default to TEXT
       ' Display the Open dialog box.
       CommonDialog1.ShowOpen
       ' Call the open file procedure.
     '  OpenFile (CommonDialog1.FileName)
       Debug.Print CommonDialog1.FileName
       Exit Sub
    
    ErrHandler:
    ' User pressed Cancel button.
       Exit Sub
    
    End Sub
    
    Private Sub cmdShowColor_Click()
      ' Set Cancel to True
      CommonDialog1.CancelError = True
      On Error GoTo ErrHandler
      ' Set the Flags property
      CommonDialog1.Flags = cdlCFEffects Or cdlCFBoth
      ' Display the Font dialog box
      CommonDialog1.ShowColor
      rtb.BackColor = CommonDialog1.Color
      Exit Sub
    ErrHandler:
      ' User pressed the Cancel button
      Exit Sub
    End Sub
    
    Private Sub cmdShowFont_Click()
      ' Set Cancel to True
      CommonDialog1.CancelError = True
      On Error GoTo ErrHandler
      ' Set the Flags property
      CommonDialog1.Flags = cdlCFEffects Or cdlCFBoth ' cdlCFPrinterFonts
      ' Display the Font dialog box
      CommonDialog1.ShowFont
      If rtb.SelLength = 0 Then
        rtb.SelStart = 0
        rtb.SelLength = Len(rtb.SelRTF)
      End If
      rtb.SelFontName = CommonDialog1.FontName
      rtb.SelFontSize = CommonDialog1.FontSize
      rtb.SelBold = CommonDialog1.FontBold
      rtb.SelItalic = CommonDialog1.FontItalic
      rtb.SelUnderline = CommonDialog1.FontUnderline
      rtb.SelStrikeThru = CommonDialog1.FontStrikethru
      rtb.SelColor = CommonDialog1.Color
      Exit Sub
    ErrHandler:
      ' User pressed the Cancel button
      Exit Sub
    End Sub
    
    Private Sub cmdSaveFile_Click()
        Dim strFileName As String
        Dim ans As Integer
        CommonDialog1.Flags = &H2 ' Overwrite Flag
        CommonDialog1.Filter = "RTF|*.rtf|Text|*.txt"
        CommonDialog1.ShowSave
        On Error GoTo SaveProblems
        strFileName = CommonDialog1.FileName
        If CommonDialog1.FilterIndex = 1 Then
            CommonDialog1.DefaultExt = "rtf"
            rtb.SaveFile strFileName
        Else
            CommonDialog1.DefaultExt = "txt"
            rtb.SaveFile strFileName, rtfText
        End If
        Exit Sub
    SaveProblems:
            MsgBox "Can’t save the file, try again.", vbCritical
        Exit Sub
    End Sub
    
    Private Sub Form_Load()
      Label1.Caption = "Open File into RTB " & vbCrLf & _
                       "Save File from RTB" & vbCrLf & _
                       "Show Font" & vbCrLf & _
                       "Change selected text" & vbCrLf & _
                       "       or all text." & vbCrLf & _
                       "Show Color" & vbCrLf & _
                       "Change BackColor of RTB"
    End Sub
    
    Sub SaveMultipleFiles()
        Dim strOrigDir As String
        Dim strNewDir As String
        Dim varTemp As Variant
        Dim lngIdx As Long
            strNewDir = "D:\Temp\"
            With CommonDialog1
            .Flags = cdlOFNAllowMultiselect Or cdlOFNLongNames Or cdlOFNExplorer
            .ShowSave
            varTemp = Split(.FileName, vbNullChar)
        End With
            If IsArray(varTemp) Then
            If UBound(varTemp) = 0 Then
                FileCopy varTemp(lngIdx), strNewDir & Right$(varTemp(lngIdx), Len(varTemp(lngIdx)) - InStrRev(varTemp(lngIdx), "\"))
            Else
                strOrigDir = varTemp(0)
                For lngIdx = LBound(varTemp) + 1 To UBound(varTemp)
                    FileCopy strOrigDir & "\" & varTemp(lngIdx), strNewDir & varTemp(lngIdx)
                Next
            End If
        End If
    End Sub
    Last edited by dglienna; January 13th, 2009 at 09:30 PM.
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  7. #7
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: Selecting multiple folders in a visual basic 6 application

    Yes, true. You have to use the standard multi-select operations using shift and control keys.
    But you can only select multiple folders in one specific folder. You can not include subfolders of one or more folders.
    The tree solution allows you to check any number of folders in any level of subfolders.

  8. #8
    Join Date
    Jul 2006
    Location
    Germany
    Posts
    3,725

    Re: Selecting multiple folders in a visual basic 6 application

    Hm... I played with the MultiSelect of the CommonDialog, but it seems that you can NOT return folder names ...

    If you mark some folders and click open, the first folder is opened but the name is not returned.
    If you mark multiple filas AND folders, the dialog returns only the filenames. folder names are not part of the result.

    Seems to me you can NOT use the CommonDialog to select multiple folders at all.

  9. #9
    Join Date
    Dec 2008
    Posts
    4

    Re: Selecting multiple folders in a visual basic 6 application

    Thanks WoF,

    i will look at your treeview sample.

  10. #10
    Join Date
    Dec 2008
    Posts
    4

    Resolved Re: Selecting multiple folders in a visual basic 6 application

    My problem has been resolved - thanks to all who replied with suggestions.
    Ryan

  11. #11
    Join Date
    Dec 2008
    Posts
    19

    Re: Selecting multiple folders in a visual basic 6 application

    Ryan:

    It would be helpful to others if you would tell us how your problem was resolved. Thanks.

  12. #12
    Join Date
    Mar 2016
    Posts
    6

    Re: Selecting multiple folders in a visual basic 6 application

    im having the same problem here .. could someone pls help me out

  13. #13
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,284

    Re: Selecting multiple folders in a visual basic 6 application

    aromalaryea: Please start a new thread as this thread is very very old

  14. #14
    Join Date
    Mar 2016
    Posts
    6

    Re: Selecting multiple folders in a visual basic 6 application

    @HanneSThEGreaT: Alright thnx

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

Featured