|
-
Dec 29th, 2005, 12:26 PM
#1
Thread Starter
Hyperactive Member
Creating Events for my Classes
I see that there are BeforeDelete events in MS Office applications. If these events are set to false you can stop the program from deleting a value, page, document, or whatever event this event is fired before.
I was wondering how I couldcreate an event like this? I have a class that, of course, adds data to a collection. I want to be able to fire a BeforeAdd event that will wait for a cancel boolean. This way I will be able to validate my data in different ways and makes this class more rebust between applications.
Thanks all!
Currently Using: VS 2005 Professional
-
Dec 29th, 2005, 01:13 PM
#2
Re: Creating Events for my Classes
You may proceed with something like this
VB Code:
Public Class MyTestClass
Public Event BeforeAdd(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs)
Public Event AfterAdd(ByVal sender As Object, ByVal e As System.EventArgs)
Public Sub Add(ByVal pValueToAdd As Object)
Dim MyCancelEventArg As New System.ComponentModel.CancelEventArgs(False)
RaiseEvent BeforeAdd(Me, MyCancelEventArg)
If MyCancelEventArg.Cancel Then
'Do not add the value of pValueToAdd
Else
'Do add the value of pValueToAdd
RaiseEvent AfterAdd(Me, New EventArgs)
End If
End Sub
End Class
then in form form or something
VB Code:
Public Class Form2
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Private WithEvents mMyTestClass As New MyTestClass
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim MyValue As Object
mMyTestClass.Add(MyValue)
End Sub
Private Sub mMyTestClass_AfterAdd(ByVal sender As Object, ByVal e As System.EventArgs) Handles mMyTestClass.AfterAdd
End Sub
Private Sub mMyTestClass_BeforeAdd(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles mMyTestClass.BeforeAdd
'Check if i should add or not
'Do not add
e.Cancel = True
End Sub
End Class
Last edited by Zakary; Dec 29th, 2005 at 01:20 PM.
Using VS 2010 on Fw4.0
-
Dec 29th, 2005, 01:37 PM
#3
Thread Starter
Hyperactive Member
Re: Creating Events for my Classes
I have attempted this before. What happens is that the event fires but the code continues even while I am handling the event in my form.
Currently Using: VS 2005 Professional
-
Dec 29th, 2005, 01:46 PM
#4
Re: Creating Events for my Classes
The add method will wait for the event to finish before continuing, a bit like Calling a Function or a Sub.
-
Dec 29th, 2005, 01:55 PM
#5
Thread Starter
Hyperactive Member
Re: Creating Events for my Classes
Another question along the same lines, When I dim withevents myVar, I cannot use myVar in a for loop. I get this error: "Loop control variable cannot be a property or a late-bound indexed array."
What does this mean and how can I get past it and still use my variable in a loop?
Currently Using: VS 2005 Professional
-
Dec 29th, 2005, 02:01 PM
#6
Re: Creating Events for my Classes
I think that mean that you can't use Array in this way in the For Next Loop .. but show me some code, to be sure, of what we are talking about
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
|