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.
Printable View
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.
I have tried something like this in my class module.
And the following in my project.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
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?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
Thanks in advance.
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
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?
Hello ...
I really need this help, can anyone help me?
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.Quote:
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?
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)
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.
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