|
-
Aug 2nd, 2007, 05:01 AM
#1
Thread Starter
Hyperactive Member
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.
-
Aug 2nd, 2007, 05:26 AM
#2
Re: Simple event raising/handling.
I don't understand exactly what you mean, can you post your code?
-
Aug 2nd, 2007, 05:52 AM
#3
Thread Starter
Hyperactive Member
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
-
Aug 2nd, 2007, 06:06 AM
#4
Re: Simple event raising/handling.
The class should also declare and raise the event in order to pass it.
-
Aug 2nd, 2007, 06:25 AM
#5
Thread Starter
Hyperactive Member
Re: Simple event raising/handling.
 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?
-
Aug 2nd, 2007, 06:29 AM
#6
New Member
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.
-
Aug 2nd, 2007, 06:35 AM
#7
Thread Starter
Hyperactive Member
Re: Simple event raising/handling.
 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?
-
Aug 2nd, 2007, 06:42 AM
#8
New Member
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".
-
Aug 2nd, 2007, 06:46 AM
#9
Thread Starter
Hyperactive Member
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?
-
Aug 2nd, 2007, 06:47 AM
#10
New Member
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.
-
Aug 2nd, 2007, 06:52 AM
#11
New Member
Re: Simple event raising/handling.
 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.
-
Aug 2nd, 2007, 06:57 AM
#12
Thread Starter
Hyperactive Member
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?
-
Aug 3rd, 2007, 07:24 AM
#13
Thread Starter
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|