|
-
Dec 11th, 2005, 10:22 PM
#1
Thread Starter
Addicted Member
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
-
Dec 11th, 2005, 10:46 PM
#2
Thread Starter
Addicted Member
Re: Cancelling an event
I have tried something like this in my class module.
VB Code:
Private mPrevValue As Integer
Private mCurrValue As Integer
Public Event ValueChanged(PrevValue As Integer, NewValue As Integer, Cancel As Boolean)
Public Property Get Value() As Integer
Value = mCurrValue
End Property
Public Property Let Value(byval NewValue As Integer)
Dim Cancel As Boolean
Cancel = False
RaiseEvent ValueChanged(mPrevValue, NewValue, Cancel)
If (Not Cancel) then
mPrevValue = mCurrValue
mCurrValue = NewValue
End If
End Property
And the following in my project.
VB Code:
Dim WithEvents c As MyClass.Test
Private Sub c_ValueChanged(PrevValue As Integer, NewValue As Integer, Cancel As Boolean)
If (NewValue > 10) Then
Cancel = True
End If
End Sub
Private Sub Form_Load()
Set c = CreateObjects("MyClass.Test")
c.Value = 1
c.Value = 5
c.Value = 13
MsgBox c.Value
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
-
Dec 11th, 2005, 11:45 PM
#3
Hyperactive Member
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:
Public Property Let Value(byval NewValue As Integer)
If NewValue < = 10 Then
mCurrValue = NewValue
Else
'do nothing with your private variable representing the property, in this manner you retain the current value
'and you might want to have something like
MyPrivateErrorNo = 101 'you may have your own numbering
err.raise MyPrivateErrorNo , , "Invalid value."
End If
End Property
And in your project.
Dim WithEvents c As MyClass.Test
Private TestSub ()
On Error Goto err_handler
c.value = 20
on error goto 0
Exit sub
err_handler:
if err.number<>0 then msgbox err.description
on error goto 0
End Sub
Last edited by dRAMmer; Dec 11th, 2005 at 11:51 PM.
live, code and die...
-
Dec 12th, 2005, 01:19 AM
#4
Thread Starter
Addicted Member
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?
-
Dec 12th, 2005, 02:46 AM
#5
Thread Starter
Addicted Member
Re: Cancelling an event
Hello ...
I really need this help, can anyone help me?
-
Dec 12th, 2005, 11:33 AM
#6
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)
-
Dec 12th, 2005, 08:46 PM
#7
Thread Starter
Addicted Member
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.
-
Dec 12th, 2005, 10:12 PM
#8
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|