Results 1 to 7 of 7
  1. #1
    4 Star Lounger
    Join Date
    Feb 2001
    Location
    Gillingham, Kent, England
    Posts
    511
    Thanks
    0
    Thanked 0 Times in 0 Posts

    WithEvents rules (VB6 SR5)

    Just a quick question.

    When using an object as with events as a variable i.e Dim WithEvents ZipFile As ChilkatZip goes at the top of the forms code and event handlers can be added to the form's code, can the object be passed to a module or does it all have to be written against the form?

    Not sure I explained that well, so if any further explanation is needed, please reply and ask.
    Any help to keep my code tidy would be much appreciated.

  2. #2
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: WithEvents rules (VB6 SR5)

    Could you explain what you mean by "can the object be passed to a module"? WithEvents is only valid in a class module. What you're doing is monitoring the object declared WithEvents and mirroring its specified events within the current class.
    Charlotte

  3. #3
    4 Star Lounger
    Join Date
    Feb 2001
    Location
    Gillingham, Kent, England
    Posts
    511
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: WithEvents rules (VB6 SR5)

    Before I start, I was taught to have as little code as possible behind the events on a form, and that its good practice to keep everything in modules and call it.
    I havent really used class modules at all as yet.

    Basically, I have a form and in that forms code I have an object defined with WithEvents
    I currently have all of the code that deals with that object on a click event of a button. There is also a small piece of code for one of the objects events.

    Is it possible to move the code behind the click event to a module and pass its parameters to it, in the same way as usual - the difference being that one of the objects passed is the object with the WithEvents.
    Currently, if I pass the code to a module, it runs fine, but doesn't trigger its events. Hopefully thats made things a little clearer. In the mean time, ill see if I can read up a bit on class modules.

    Forms code:

    Dim WithEvents ZipFile As ChilkatZip

    Private Sub ZipFile_UnzipPercentDone(ByVal percentDone As Long)
    ProgressBar1.Value = percentDone
    End Sub

    Private Sub Command1_Click()
    'Theres a whole load of code on this event, but I took it out and left the main one that uses the object here.
    ZipFile.Extract DBPath.Text
    End Sub

  4. #4
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: WithEvents rules (VB6 SR5)

    Form modules are a kind of class modules. I'm sorry but I still don't understand. Where is the button that has the code? When you use WithEvents, you need the event procedures you want to handle in the class that declared the WithEvents. So if ZipFile_UnzipPercentDone is an event of the ZipFile object, then you are using WithEvents to capture that information directly on in the calling class. You can't move that code to a standard module, so what are you trying to do?
    Charlotte

  5. #5
    4 Star Lounger
    Join Date
    Feb 2001
    Location
    Gillingham, Kent, England
    Posts
    511
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: WithEvents rules (VB6 SR5)

    I was trying to do what you have just said can't be done, which has answered my question perfectly thanks Charlotte.

    I was trying to pass the zipfile object out to a module to deal with it there instead of in the onclick event of the button, which is on the same form as the zipfile object.
    Mental note: I must learn more about class modules.

  6. #6
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post

    Re: WithEvents rules (VB6 SR5)

    I didn't say it couldn't be done, I said it couldn't be done the way you described. In fact, you can create a class that handles the WithEvents tracking of the object, declare your object variable as an instance of that class, and bind the object variable to that class through an initialization routine in the class. I'm not clear on where the button comes into this, so that's as specific as I can get.
    Charlotte

  7. #7
    4 Star Lounger
    Join Date
    Feb 2001
    Location
    Gillingham, Kent, England
    Posts
    511
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: WithEvents rules (VB6 SR5)

    Sorry Charlotte, I did mean can't be done the way I was doing it as opposed to can't be done at all. After you'd said about using a class module I suspected i'd be able to solve my problem using one, but due to typical urgency of the job and the fact that the code isn't too big that it can't be in a form's module - for now, its staying where it is.

    I will look into classes as soon as the urgency of the current job is out of the way though as I've read some solutions to various problems which were helped by the use of classes so I think its about time I learnt. Thanks for the push in the right direction though.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •