Results 1 to 18 of 18

Thread: Resolved - Need a cancel button

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2005
    Posts
    89

    Resolved Resolved - Need a cancel button

    I need a cancel button for a program which goes into this loop searching the entire hard drive. It can be a lengthy process waiting for it to stop on its own. Where do I put the code and what code is best to use?

    VB Code:
    1. Private Function SearchForFiles(FP As FILE_PARAMS) As Double
    2.  
    3.   'local working variables
    4.    Dim WFD As WIN32_FIND_DATA
    5.    Dim hFile As Long
    6.    Dim nSize As Long
    7.    Dim sPath As String
    8.    Dim sRoot As String
    9.    Dim sTmp As String
    10.        
    11.    sRoot = QualifyPath(FP.sFileRoot)
    12.    sPath = sRoot & "*.*"
    13.    
    14.   'obtain handle to the first match
    15.    hFile = FindFirstFile(sPath, WFD)
    16.    
    17.   'if valid ...
    18.    If hFile <> INVALID_HANDLE_VALUE Then
    19.    
    20.      'This is where the method obtains the file
    21.      'list and data for the folder passed.
    22.      '
    23.      'GetFileInformation function returns the size,
    24.      'in bytes, of the files found matching the
    25.      'filespec in the passed folder, so it is
    26.      'assigned to nSize. It is not directly assigned
    27.      'to FP.nFileSize because nSize is incremented
    28.      'below if a recursive search was specified.
    29.       nSize = GetFileInformation(FP)
    30.       FP.nFileSize = nSize
    31.  
    32.       Do
    33.      
    34.         'if the returned item is a folder...
    35.          If (WFD.dwFileAttributes And FILE_ATTRIBUTE_DIRECTORY) Then
    36.            
    37.            '..and the Recurse flag was specified
    38.             If FP.bRecurse Then
    39.            
    40.               'remove trailing nulls
    41.                sTmp = TrimNull(WFD.cFileName)
    42.                
    43.               'and if the folder is not the default
    44.               'self and parent folders...
    45.                If sTmp <> "." And sTmp <> ".." Then
    46.                
    47.                  '..then the item is a real folder, which
    48.                  'may contain other sub folders, so assign
    49.                  'the new folder name to FP.sFileRoot and
    50.                  'recursively call this function again with
    51.                  'the ammended information.
    52.                  '
    53.                  'Since nSize is a local variable, whose value
    54.                  'is both set above as well as returned as the
    55.                  'function call value, the nSize needs to be
    56.                  'added to previous calls in order to maintain accuracy.
    57.                  '
    58.                  'However, because the nFileSize member of
    59.                  'FILE_PARAMS is passed back and forth through
    60.                  'the calls, nSize is simply assigned to it
    61.                  'after the recursive call finishes.
    62.                   FP.sFileRoot = sRoot & sTmp
    63.                   nSize = nSize + SearchForFiles(FP)
    64.                   FP.nFileSize = nSize
    65.                  
    66.                End If
    67.                
    68.             End If
    69.            
    70.          End If
    71.          
    72.      'continue looping until FindNextFile returns
    73.      '0 (no more matches)
    74.       Loop While FindNextFile(hFile, WFD)
    75.      
    76.      'close the find handle
    77.       hFile = FindClose(hFile)
    78.    
    79.    End If
    80.    
    81.   'because this routine is recursive, return
    82.   'the size of matching files
    83.    SearchForFiles = nSize
    84.    
    85. End Function

    Thanks in advance!
    Last edited by imperialdata; Feb 15th, 2006 at 07:55 AM. Reason: Resolved

  2. #2
    Super Moderator manavo11's Avatar
    Join Date
    Nov 2002
    Location
    Around the corner from si_the_geek
    Posts
    7,171

    Re: Need a cancel button

    Create a button named cmdCancel (or something like that), add a variable at the top of the form named bCancel. What you need to do is before you start the Do-Loop set bCancel to False, and in the click event of the cancel button set bCancel to True. Also change the Do-Loop to check :

    VB Code:
    1. Loop While FindNextFile(hFile, WFD) And bCancel = False

    I also added the vbcode tags to your code to make it easier to read.


    Has someone helped you? Then you can Rate their helpful post.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jun 2005
    Posts
    89

    Re: Need a cancel button

    Excuse my ignorance on this one, I get a 'Variable not defined' error.
    If a Function is Private and I Dim a variable in the main form code, then do I have to dim it again in the function?

    Thanks for tidying up the code with the tags, I'll do that next time.

  4. #4
    Frenzied Member oh1mie's Avatar
    Join Date
    Sep 2001
    Location
    Finland
    Posts
    1,043

    Re: Need a cancel button

    You can use also DoEvents

    VB Code:
    1. 'Declare on general section
    2. Public bCancel As Boolean
    3.  
    4.  
    5. bCancel = False
    6.      
    7. Do
    8.  
    9.  DoEvents
    10.  '....
    11.  If bCancel Then Exit Do
    12.  
    13. Loop While FindNextFile(hFile, WFD)
    14.  
    15. Private Sub cmdCancel_Click()
    16.     bCancel = True
    17. End Sub
    oh1mie/Vic


  5. #5
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Need a cancel button

    Depending on the code happening in the Do...Loop the DoEvents could slow the whole thing down quite a bit, so it might be worth sticking in a counter and only firing DoEvents every nth loop.

  6. #6
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Need a cancel button

    as oh1mie stated:

    bCancel is declared in the General Declarations section of the form...
    unless this function is running in a module.. then u will need to put the bCancel as publice in the mod..

    Public bCancel as Boolean

    then in the click event of the cance button set the bCancel = True

    be sure to set bCancel back to false after its done "canceling"
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  7. #7

    Thread Starter
    Lively Member
    Join Date
    Jun 2005
    Posts
    89

    Re: Need a cancel button

    The code is not in a module, just a private function but I still get the 'Variable not defined' error despite having Dimmed it in the Form Load area

  8. #8
    PowerPoster Static's Avatar
    Join Date
    Oct 2000
    Location
    Rochester, NY
    Posts
    9,390

    Re: Need a cancel button

    and its highlighting bCancel??

    and.. not the form load... u want it in the General Declarations section...
    thats ALL the way at the top.. above all other subs..

    Left dropdown in the code window will say (General) and the right dropdown will say (Declarations)
    JPnyc rocks!! (Just ask him!)
    If u have your answer please go to the thread tools and click "Mark Thread Resolved"

  9. #9
    Frenzied Member oh1mie's Avatar
    Join Date
    Sep 2001
    Location
    Finland
    Posts
    1,043

    Re: Need a cancel button

    Quote Originally Posted by imperialdata
    The code is not in a module, just a private function but I still get the 'Variable not defined' error despite having Dimmed it in the Form Load area
    If you dim variable on form load section, it works only on the form load prosedure.

    Move it to the general section to works in all prosedures on the that form
    oh1mie/Vic


  10. #10

    Thread Starter
    Lively Member
    Join Date
    Jun 2005
    Posts
    89

    Re: Need a cancel button

    Thanks for that guys. As someone who comes back to VB after a long time not doing anything, the basics can get a bit rusty.

    Tried what you suggested and got 'Invalid outside procedure' error on this code (where I put it in the General Declarations area as you said). The word false was highlighted:

    VB Code:
    1. Option Explicit
    2.  
    3. 'Cancel button
    4. Dim bCancel As Boolean
    5.  
    6. 'Set cancel button to false
    7. bCancel = False

  11. #11
    Oi, fat-rag! bushmobile's Avatar
    Join Date
    Mar 2004
    Location
    on the poop deck
    Posts
    5,592

    Re: Need a cancel button

    if the variable is in the general declarations section then you should declare it 'Private bCancel As Boolean'.

    It looks like you probably have bCancel = False outside of any procedures, but it is unnecessary to set it to false because it defaults to that anyway.

  12. #12

    Thread Starter
    Lively Member
    Join Date
    Jun 2005
    Posts
    89

    Re: Need a cancel button

    Removing bCancel = false allowed me to get past the error but cancel button has no effect. It seems to have no focus.
    Last edited by imperialdata; Feb 13th, 2006 at 06:26 PM.

  13. #13
    Frenzied Member oh1mie's Avatar
    Join Date
    Sep 2001
    Location
    Finland
    Posts
    1,043

    Re: Need a cancel button

    Quote Originally Posted by imperialdata
    Removing bCancel = false allowed me to get past the error but cancel button has no effect. It seems to have no focus.
    Did you forget DoEvents?
    oh1mie/Vic


  14. #14

    Thread Starter
    Lively Member
    Join Date
    Jun 2005
    Posts
    89

    Re: Need a cancel button

    Yup, wasn't sure where it went, had it in the wrong part of the code. Cancelling seems fine now but as Bushmobile said, it has slowed the whole thing down. I'm checking for files across large hard drives and searching is very slow.
    How would I fire the Do Events every, say, 10th loop?

  15. #15
    Frenzied Member oh1mie's Avatar
    Join Date
    Sep 2001
    Location
    Finland
    Posts
    1,043

    Re: Need a cancel button

    Quote Originally Posted by imperialdata
    Yup, wasn't sure where it went, had it in the wrong part of the code. Cancelling seems fine now but as Bushmobile said, it has slowed the whole thing down. I'm checking for files across large hard drives and searching is very slow.
    How would I fire the Do Events every, say, 10th loop?
    VB Code:
    1. Dim iCount As Integer
    2.  
    3. Do
    4.  
    5.     iCount = iCount + 1
    6.     If iCount = 10 Then
    7.        DoEvents
    8.        iCount = 0
    9.     End If
    10.  
    11. Loop While FindNextFile(hFile, WFD)
    oh1mie/Vic


  16. #16
    coder. Lord Orwell's Avatar
    Join Date
    Feb 2001
    Location
    Elberfeld, IN
    Posts
    7,628

    Re: Need a cancel button

    if you have a loop like this with doevents in it, you need to be careful that someone
    doesn't close your program while you are running the loop. It will continue to run even though the main form is closed. Since doevents has a return value equal to the number of visible forms, you should change the code to something like this:
    'this code will call doevents once for every 10 counts
    DIM Cnt as Integer 'place at beginning of sub

    'then in your loop (in fact, put this right before the Loop keyword)
    Cnt = Cnt + 1
    if Cnt = 10 then
    cnt = 1
    if doevents() = 0 then exit Do ' or exit Loop, depending if you do cleanup routines
    ' in this case, you still need to close a file, so exit do will do...
    end if

    I hope this helps...
    My light show youtube page (it's made the news) www.youtube.com/@lightsofelberfeld
    Contact me on the socials www.facebook.com/lordorwell

  17. #17

    Thread Starter
    Lively Member
    Join Date
    Jun 2005
    Posts
    89

    Re: Need a cancel button

    Thanks guys, excellent help.

  18. #18
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Resolved - Need a cancel button

    If this is resolved, please pull down the Thread Tools menu and click the Mark Thread Resolved button. That will let everyone know that you have your answer.

    Thank you.

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