Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleQuickly view text files
Keywordsviewer, text files, view files, utilities
CategoriesUtilities, Files and Directories
 
Use the DriveListBox, DirListBox, and FileListBox controls. When the user selects a new drive, set the DirListBox's path to the new drive. When the user selects a new directory, set the FileListBox's path to the new directory. When the user selects a new file, display it.
 
Private Sub DriveList_Change()
    'On Error GoTo DriveError
    DirList.Path = DriveList.Drive
    Exit Sub

DriveError:
    DriveList.Drive = DirList.Path
    Exit Sub
End Sub

Private Sub DirList_Change()
    FileList.Path = DirList.Path
End Sub

Private Sub FileList_Click()
Dim fname As String
Dim fnum As Integer

    fname = FileList.Path & "\" & FileList.FileName
    MousePointer = vbHourglass
    DoEvents
    Caption = "ViewText [" & fname & "]"

    On Error GoTo LoadFileError
    fnum = FreeFile
    Open fname For Input As fnum
    txtFile.Text = Input(LOF(fnum), fnum)
    Close fnum
    On Error GoTo 0

    txtFile.SelStart = 0
    txtFile.SelLength = 0
    MousePointer = vbDefault
    Exit Sub

LoadFileError:
    Beep
    txtFile.Text = ""
    Caption = "ViewText [Invalid file format]"
    Resume Next
End Sub
 
In the form's KeyDown event handler, see if the user pressed Delete or Backspace.

If the user pressed Delete, use the SHFileOperation function to move the file into the wastebasket without prompting the user for confirmation.

If the user pressed Backspace, immediately kill the file. This means the user cannot recover the file later but it is faster.

 
' If the user presses Delete, move the current
' file into the wastebasket.
' If the user presses Backspace, kill the file.
Private Sub Form_KeyDown(KeyCode As Integer, Shift As _
    Integer)
Dim op As SHFILEOPSTRUCT

    ' Make sure we have a file name.
    If Len(m_FileName) = 0 Then Exit Sub

    ' See what key this is.
    If (KeyCode = vbKeyBack) Then
        ' Backspace. Kill the file.
        Kill m_FileName

        m_FileName = ""
        Caption = "ViewFile []"
        txtFile.Text = ""
        DirList_Change
    ElseIf (KeyCode = vbKeyDelete) And (Len(m_FileName) > _
        0) Then
        ' Delete. Move the file into the wastebasket.
        With op
            .wFunc = FO_DELETE
            .pFrom = m_FileName
            .fFlags = _
                FOF_ALLOWUNDO Or _
                FOF_NOCONFIRMATION
        End With

        On Error Resume Next
        SHFileOperation op

        If Err.Number <> 0 Then
            MsgBox "Error " & Format$(Err.Number) & _
                " deleting file " & m_FileName & _
                vbCrLf & Err.Description
        Else
            m_FileName = ""
            Caption = "ViewFile []"
            txtFile.Text = ""
            DirList_Change
        End If
        On Error GoTo 0
    End If
End Sub
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated