Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

FileListBox Selection problem

Status
Not open for further replies.

LFC8

Programmer
Nov 18, 2004
45
0
0
GB
Hi

I need to be able to display a msgbox asking the user to select files from a FileListBox when no files havent been selected, the code i have done looks fine to me but it bypasses it. Can anyone tell me why?

FileSelect = File1.FileName

If FileSelect = "" Then

MsgBox "Please select file(s) you wish to delete?",vbOKOnly, "File Deletion"

Exit Sub
End If

TIA
 
Have you put a breakpoint on it to see the value of File1.FileName in comparison to your expected value?

Cheers

HarleyQuinn
---------------------------------
Get the most out of Tek-Tips, read FAQ222-2244 before posting.
 
Hi HarleyQuinn

Thanks for your reply, I see were i'm going wrong with that code, FileSelect is always going to return a file hence why its bypassing the msgbox. I've tried using the File1.Selected method but i'm getting run time errors. Could you point me in the right direction at all?

TIA
 
Hi LFC8,

I tried the code you posted and that works for me.

I've tried it with MultiSelect set to 0 (none), 1 (simple) and 2 (extended) and it worked fine with all of them. The final two had the advantage of being able to deselect an item once you had clicked in the FileListBox, therefore allowing you to receive the msgbox after an initial click in the FileListBox.

The only reason I can think that your code is bypassing the msgbox is that there is actually a file selected.

Hope this helps

HarleyQuinn
---------------------------------
Get the most out of Tek-Tips, read FAQ222-2244 before posting.
 
Hi HarleyQuinn

Here is all the code, i think its the loop which returns the file.

Private Sub cmdDelete_Click()
Dim i As Integer
ReDim Alist(0) As String
Dim Response As String
Dim FileSelect As String

For i = 0 To File1.ListCount - 1
If File1.Selected(i) Then
ReDim Preserve Alist(UBound(Alist) + 1)
Alist(UBound(Alist)) = File1.List(i)
End If
Next

If cmdDelete = True Then
File1.Selected = False
MsgBox "Please select file(s) you wish to delete?", vbOKOnly, "File Deletion"
Exit Sub
End If

If cmdDelete = True Then
Response = MsgBox("Are you sure you want to delete these file(s)?", vbYesNo)
If Response = vbNo Then
Exit Sub
End If
End If

For i = 1 To UBound(Alist)
Kill File1.Path & "\" & Alist(i)
Next
File1.Refresh
End Sub

Thanks
 
Hi LFC8,

Here is a quick solution that should solve your current problem
Code:
Private Sub cmdDelete_Click()
    Dim i As Integer
    ReDim Alist(0) As String
    Dim Response As String
    Dim FileSelect As String
    [red]Dim Selects as Boolean[/red]

    [red]Selects = False[/red]
        
    For i = 0 To File1.ListCount - 1
        If File1.Selected(i) Then
            ReDim Preserve Alist(UBound(Alist) + 1)
            Alist(UBound(Alist)) = File1.List(i)
            [red]Selects = True[/red]
        End If
    Next
        
        If cmdDelete = True Then
             [red]Select Case Selects
                     Case True
                          Response = MsgBox("Are you sure you want to delete these file(s)?", vbYesNo)
                            If Response = vbNo Then Exit Sub
                     Case False
                          MsgBox "Please select file(s) you wish to delete?", vbOKOnly, "File Deletion"
                          Exit Sub
             End Select[/red]
        End If
        
                For i = 1 To UBound(Alist)
                    Kill File1.Path & "\" & Alist(i)
                Next
                    File1.Refresh
End Sub

That would change a Boolean value depending whether there are any selected files. You can then check the value of the Boolean to see if there are any files selected.

Hope this helps

HarleyQuinn
---------------------------------
Get the most out of Tek-Tips, read FAQ222-2244 before posting.
 
LFC8 said:
i think its the loop which returns the file.

The loop fills an array (Alist) with the names of the selected files.

Another way to see if any files are selected which would avoid using the Boolean value would be to test if there is any data in the aforementioned array.

Hope this helps

HarleyQuinn
---------------------------------
Get the most out of Tek-Tips, read FAQ222-2244 before posting.
 
Hi HarleyQuinn

Thanks very much for your help!

it worked famously

Ta
 
You're welcome, glad I could help [smile]

HarleyQuinn
---------------------------------
Get the most out of Tek-Tips, read FAQ222-2244 before posting.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top