Results 1 to 8 of 8

Thread: Cancelling an event

  1. #1

    Thread Starter
    Addicted Member LeonX's Avatar
    Join Date
    Dec 2004
    Location
    Jakarta, Indonesia
    Posts
    172

    Resolved Cancelling an event

    Hi guys,

    I need some help from you, I wonder how can I make a class with events like MDIForm_QueryUnload(Cancel as Integer, UnloadMode as Integer)? When I set Cancel to non zero value it can abort the unload method.

    Thanks in advance.
    Last edited by LeonX; Dec 12th, 2005 at 08:56 PM. Reason: Resolved
    Regards,
    Ferry

  2. #2

    Thread Starter
    Addicted Member LeonX's Avatar
    Join Date
    Dec 2004
    Location
    Jakarta, Indonesia
    Posts
    172

    Re: Cancelling an event

    I have tried something like this in my class module.

    VB Code:
    1. Private mPrevValue As Integer
    2. Private mCurrValue As Integer
    3.  
    4. Public Event ValueChanged(PrevValue As Integer, NewValue As Integer, Cancel As Boolean)
    5.  
    6. Public Property Get Value() As Integer
    7. Value = mCurrValue
    8. End Property
    9.  
    10. Public Property Let Value(byval NewValue As Integer)
    11. Dim Cancel As Boolean
    12. Cancel = False
    13. RaiseEvent ValueChanged(mPrevValue, NewValue, Cancel)
    14. If (Not Cancel) then
    15. mPrevValue = mCurrValue
    16. mCurrValue = NewValue
    17. End If
    18. End Property
    And the following in my project.

    VB Code:
    1. Dim WithEvents c As MyClass.Test
    2.  
    3. Private Sub c_ValueChanged(PrevValue As Integer, NewValue As Integer, Cancel As Boolean)
    4. If (NewValue > 10) Then
    5. Cancel = True
    6. End If
    7. End Sub
    8.  
    9. Private Sub Form_Load()
    10. Set c = CreateObjects("MyClass.Test")
    11. c.Value = 1
    12. c.Value = 5
    13. c.Value = 13
    14. MsgBox c.Value
    15. End Sub
    Why the value still 13? I want to Cancel the value from changing if the new value greater than 10. What should I do to get this?

    Thanks in advance.
    Last edited by LeonX; Dec 11th, 2005 at 11:51 PM.
    Regards,
    Ferry

  3. #3
    Hyperactive Member dRAMmer's Avatar
    Join Date
    Oct 2001
    Location
    strangelans
    Posts
    463

    Re: Cancelling an event

    You do neet and event if that is all you are trying to do. WHat you may want for you event rather is to be able to raise a message.

    VB Code:
    1. Public Property Let Value(byval NewValue As Integer)
    2. If NewValue < = 10 Then
    3.    mCurrValue = NewValue
    4. Else
    5.    'do nothing with your private variable representing the property, in this manner you retain the current value
    6.    'and you might want to have something like
    7.    MyPrivateErrorNo = 101 'you may have your own numbering
    8.    err.raise MyPrivateErrorNo , , "Invalid value."
    9. End If
    10. End Property
    11.  
    12. And in your project.
    13.  
    14. Dim WithEvents c As MyClass.Test
    15. Private TestSub ()
    16.    On Error Goto err_handler
    17.    c.value = 20
    18.   on error goto 0
    19.   Exit sub
    20. err_handler:
    21.    if err.number<>0 then msgbox err.description
    22.   on error goto 0
    23. End Sub
    Last edited by dRAMmer; Dec 11th, 2005 at 11:51 PM.
    live, code and die...

  4. #4

    Thread Starter
    Addicted Member LeonX's Avatar
    Join Date
    Dec 2004
    Location
    Jakarta, Indonesia
    Posts
    172

    Re: Cancelling an event

    Yes, I can do it that way, but it has a fixed validation rule, and everytime the rule change I must change my code.

    And furthermore its just a sample of condition, the point is I want to rollback the value, if it is not satisfied the condition given.

    Anyway thanks dRAMmer.
    Have any ideas to help me?
    Regards,
    Ferry

  5. #5

    Thread Starter
    Addicted Member LeonX's Avatar
    Join Date
    Dec 2004
    Location
    Jakarta, Indonesia
    Posts
    172

    Re: Cancelling an event

    Hello ...
    I really need this help, can anyone help me?
    Regards,
    Ferry

  6. #6
    PowerPoster
    Join Date
    Oct 2002
    Location
    British Columbia
    Posts
    9,758

    Re: Cancelling an event

    Why the value still 13? I want to Cancel the value from changing if the new value greater than 10. What should I do to get this?
    I ran some tests and there is nothing wrong with the code you posted. It works as expected. However, I only tested within the IDE.

    Is the s on the end of CreateObjects just a typo, or is that your own procedure? If your own procedure can you post the code.

    One recommendation. Change the declaration of the Event procedure to pass the Value variables ByVal (this has nothing to do with your problem). Otherwise, the value can be changed without using the Property Let procedure.

    Public Event ValueChanged(ByVal PrevValue As Integer, ByVal NewValue As Integer, Cancel As Boolean)

  7. #7

    Thread Starter
    Addicted Member LeonX's Avatar
    Join Date
    Dec 2004
    Location
    Jakarta, Indonesia
    Posts
    172

    Re: Cancelling an event

    Upss, sorry the "s" its a mistyping. And I'm embrassed to say that the code its working now, maybe I just mistyping the code before.
    But thanks to you brucevde, I will change the declaration of the parameter with ByVal.
    Thanks bruce.
    Regards,
    Ferry

  8. #8
    Frenzied Member pnish's Avatar
    Join Date
    Aug 2002
    Location
    Tassie, Oz
    Posts
    1,918

    Re: Cancelling an event

    If you put Option Explicit in the general declarations section of each form & module etc you won't have these sort of problems. This will cause the compiler to pick up any spelling errors or undefined varaibles.

    You can also get VB to do this for you automatically. Go to the Tools menu and select Options, go to the Editor tab and check the box labelled Require Variable Declaration
    Pete

    No trees were harmed in the making of this post, however a large number of electrons were greatly inconvenienced.

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