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.
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.
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.
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.
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.
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.
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.
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.