• WithEvents rules (VB6 SR5)

    • This topic has 12 replies, 2 voices, and was last updated 21 years ago.
    Author
    Topic
    #405530

    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.

    Viewing 1 reply thread
    Author
    Replies
    • #833975

      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.

      • #833987

        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

        • #834336

          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?

          • #835274

            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.

            • #835312

              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.

            • #835321

              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.

            • #835322

              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.

            • #835313

              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.

          • #835275

            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.

        • #834337

          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?

      • #833988

        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

    • #833976

      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.

    Viewing 1 reply thread
    Reply To: WithEvents rules (VB6 SR5)

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: