Make a new event for a form?

How to make a new event for a form something like what can be done in an ActiveX control: declare an event, raiseevent.....
learnAsked:
Who is Participating?
 
AnswerTheManConnect With a Mentor Commented:
1. start a new project
2. add 2 forms (Form1, Form2)
3. paste this code to Form1 :
Option Explicit

Private WithEvents frmTest As Form2
Private Sub Form_Load()
    Set frmTest = New Form2
    Unload Form2
    Set Form2 = Nothing
    frmTest.Show
End Sub

Private Sub frmTest_OPS(str As String)
    MsgBox "There was a click in form2"
End Sub

4. paste this code to form2 :
Option Explicit

Public Event OPS(str As String)

Private Sub Form_Click()
    RaiseEvent OPS("click")
End Sub

5. run the project
6. click on form2
7. the event raised from form2 to form1, form1 displays a MsgBox about that.

0
 
SicilianCommented:
Hi learn
   Couldn't you just declare your event in a class, Implement it in any form you want to use it in by declaring the class module with the WithEvents keyword, raise it appropriately and write code to the event?
    Perhaps you have no way of raising it appropriately? Maybe if you told us what the new event you want to implement is?

Sicilian
0
 
learnAuthor Commented:
To Sicilian:

Thank you.

Instesd a class I need a form which has got a "paper" to draw something :-)
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
learnAuthor Commented:
To  AnswerTheMan:

Your answer looks like what I need :-), but

1. You mens we can not create an event and list it in the event list of the form so that a user can code somthing into it in design time?

2. Why
   Unload Form2
   Set Form2 = Nothing
Form2 was loaded in your code?
0
 
AnswerTheManCommented:
1. you have not noticed well.
   look at Form1 Up menu and you'll see
   that there is an Object called
   'frmTest' there with all other controls. click on it, and on the right
   list you'll get it's public events
   which are the OPS event.
2. that is just a precaution. after you
   obtain a Refernce to Form2 by frmTest
   , you don't need FOrm2 anymore.
   in this example it may seems not in
   place, but in other cases it is
   recomended. making a habit to do it
   (won't raise any error here), is a
   good for preventing future bugs.
3. don't forget to unload and set nothing 'frmTest' on Form1_Unload event.
0
 
learnAuthor Commented:
Hi AnswerTheMan,

That is great! Thank you very much for your excellent answer and comment.
0
 
AnswerTheManCommented:
you welcome
0
All Courses

From novice to tech pro — start learning today.