Results 1 to 6 of 6

Thread: Creating Events for my Classes

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    May 2005
    Posts
    258

    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

  2. #2
    Frenzied Member Zakary's Avatar
    Join Date
    Mar 2005
    Location
    Canada, Quebec, Montreal
    Posts
    1,654

    Re: Creating Events for my Classes

    You may proceed with something like this

    VB Code:
    1. Public Class MyTestClass
    2.  
    3.     Public Event BeforeAdd(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs)
    4.     Public Event AfterAdd(ByVal sender As Object, ByVal e As System.EventArgs)
    5.  
    6.     Public Sub Add(ByVal pValueToAdd As Object)
    7.  
    8.         Dim MyCancelEventArg As New System.ComponentModel.CancelEventArgs(False)
    9.  
    10.         RaiseEvent BeforeAdd(Me, MyCancelEventArg)
    11.  
    12.         If MyCancelEventArg.Cancel Then
    13.             'Do not add the value of pValueToAdd
    14.         Else
    15.             'Do add the value of pValueToAdd
    16.             RaiseEvent AfterAdd(Me, New EventArgs)
    17.         End If
    18.  
    19.     End Sub
    20. End Class

    then in form form or something

    VB Code:
    1. Public Class Form2
    2.     Inherits System.Windows.Forms.Form
    3.  
    4. #Region " Windows Form Designer generated code "
    5.  
    6.     Private WithEvents mMyTestClass As New MyTestClass
    7.  
    8.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    9.         Dim MyValue As Object
    10.         mMyTestClass.Add(MyValue)
    11.  
    12.     End Sub
    13.  
    14.  
    15.     Private Sub mMyTestClass_AfterAdd(ByVal sender As Object, ByVal e As System.EventArgs) Handles mMyTestClass.AfterAdd
    16.        
    17.     End Sub
    18.  
    19.     Private Sub mMyTestClass_BeforeAdd(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles mMyTestClass.BeforeAdd
    20.         'Check if i should add or not
    21.         'Do not add
    22.         e.Cancel = True
    23.     End Sub
    24.  
    25. End Class
    Last edited by Zakary; Dec 29th, 2005 at 01:20 PM.
    Using VS 2010 on Fw4.0

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    May 2005
    Posts
    258

    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

  4. #4
    Frenzied Member Zakary's Avatar
    Join Date
    Mar 2005
    Location
    Canada, Quebec, Montreal
    Posts
    1,654

    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.
    Using VS 2010 on Fw4.0

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    May 2005
    Posts
    258

    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

  6. #6
    Frenzied Member Zakary's Avatar
    Join Date
    Mar 2005
    Location
    Canada, Quebec, Montreal
    Posts
    1,654

    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
    Using VS 2010 on Fw4.0

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