Results 1 to 13 of 13

Thread: Simple event raising/handling.

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2007
    Posts
    269

    Simple event raising/handling.

    I create a proj with a form containing a button and 3 textboxes.
    I declare an event named ValidateCont passing a control as a parameter. For each textboxes validate method( i have causes validation true on each) i raise the ValidateCont passing the textbox as the param.

    I add a class module to the proj and declare my form in here with events and add code in the event handler of this to do something but this is never executed. What have I done wrong?

    Cheers.

  2. #2
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: Simple event raising/handling.

    I don't understand exactly what you mean, can you post your code?

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2007
    Posts
    269

    Re: Simple event raising/handling.

    Hi Dig

    Form code
    Code:
    Option Explicit
    
    Public Event ButtonClicked()
    Public Event ValidateCont(objCont As Control, ByRef Cancel As Boolean)
    
    Private Sub txtAge_Validate(Cancel As Boolean)
        RaiseEvent ValidateCont(Me.txtAge, Cancel)
    End Sub
    
    Private Sub txtFirstname_Validate(Cancel As Boolean)
        RaiseEvent ValidateCont(Me.txtFirstname, Cancel)
    End Sub
    
    Private Sub txtSurname_Validate(Cancel As Boolean)
        RaiseEvent ValidateCont(Me.txtSurname, Cancel)
    End Sub
    Handling class code
    Code:
    Option Explicit
    
    Dim WithEvents f As frmMaint
    
    Private Sub Class_Initialize()
        Set f = New frmMaint
        f.Show
    End Sub
    
    Private Sub f_ButtonClicked()
        End
    End Sub
    
    
    Private Sub f_ValidateCont(objCont As Control, ByRef Cancel As Boolean)
        Select Case LCase(objCont.Name)
            Case "txtfirstname"
                If Len(objCont.Text) < 2 Then
                    Cancel = True
                End If
            Case "txtsurname"
                If Len(objCont.Text) < 3 Then
                    Cancel = True
                End If
            Case "txtage"
                If IsNumeric(objCont.Text) = False Then
                    Cancel = True
                End If
        End Select
    End Sub

  4. #4
    PowerPoster
    Join Date
    Nov 2002
    Location
    Manila
    Posts
    7,629

    Re: Simple event raising/handling.

    The class should also declare and raise the event in order to pass it.

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2007
    Posts
    269

    Re: Simple event raising/handling.

    Quote Originally Posted by leinad31
    The class should also declare and raise the event in order to pass it.
    But I want the class to handle the forms event?

  6. #6
    New Member
    Join Date
    Jul 2007
    Posts
    10

    Re: Simple event raising/handling.

    You can create custom events only in user defined classes.

    But what you want can be achieved another way. Whenever you wish to raise the ValidateCont event, call the <class name>.ValidateCont(...) function instead.

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2007
    Posts
    269

    Re: Simple event raising/handling.

    Quote Originally Posted by icanflyiwillfly
    You can create custom events only in user defined classes.

    But what you want can be achieved another way. Whenever you wish to raise the ValidateCont event, call the <class name>.ValidateCont(...) function instead.
    The why does a form allow me to declare and raise an event?

  8. #8
    New Member
    Join Date
    Jul 2007
    Posts
    10

    Re: Simple event raising/handling.

    Your are right, but it is meant to be used the other way -- "declare and raise the events in a class, and catch them in a form or another class".

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2007
    Posts
    269

    Re: Simple event raising/handling.

    But if I wanted business logic, validation etc.. outside of the form I need to be able to raise an event from my form and handle it in a class?

    What if I want my form to able to tell the outside world when a button's been clicked or textbox validate event is fired not have code inside form to handle? This must be do-able surely?

  10. #10
    New Member
    Join Date
    Jul 2007
    Posts
    10

    Re: Simple event raising/handling.

    You can also declare and raise events in a custom-control. A custom control is also like a class, though it has a graphical interface.

    The reason you can't raise events from a form is that a form, eg. Form1 in your project is an instance of a class Form. You may declare events in this Form class. But since you haven't created the Form class, you can't modify it.

  11. #11
    New Member
    Join Date
    Jul 2007
    Posts
    10

    Re: Simple event raising/handling.

    Quote Originally Posted by Moorzee
    What if I want my form to able to tell the outside world when a button's been clicked or textbox validate event is fired not have code inside form to handle? This must be do-able surely?
    Then you can create a custom user-control, add validation functions into that user-control, and then use those controls in the form.

    Otherwise as I mentioned, raising an event is just like calling a function. When catch the event in the form, just call the function in the class.

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2007
    Posts
    269

    Re: Simple event raising/handling.

    Why in the code posted above does the f_ValidateCont not get triggered when I raise the event from the form created in the class modules' initialize?

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    Aug 2007
    Posts
    269

    Re: Simple event raising/handling.

    Is anyone able to tell me why the event is not handled in my class above?

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