|
-
Mar 18th, 2007, 03:49 PM
#1
Thread Starter
Hyperactive Member
How do you create your own Events?
I just wondered, as VB is an event driven programming language, how would you go about defining and creating your own event?
btw, I have googled this with no success
-
Mar 18th, 2007, 04:13 PM
#2
Frenzied Member
Re: How do you create your own Events?
Sorry singularis, you completly misunderstand events.
If you have no idea how to use events then look it up!
-
Mar 18th, 2007, 04:27 PM
#3
Re: How do you create your own Events?
You can only use existing ones. Some events are "hidden" from VB, but can be used with a lot of work... typically tho the existing ones are more than enough, they just need to be used appropriately.
What are you actually trying to achieve?
-
Mar 18th, 2007, 04:37 PM
#4
Frenzied Member
Re: How do you create your own Events?
Sorry singularis, but after reading your comment to XShadow, I couldn't resist posting your words back to you.
I am sure si the geek is correct, but I did find this in MSDN concerning creating custome events for forms:
Adding an Event to a Form
The following step by step procedure shows how you can create custom events for forms. To try this exercise, open a new Standard Exe project and do the following:
To add an event to Form1
On the Project menu, select Add Class Module to add a class module to the project. Place the following code in the Declarations section of Class1:
Code:
Public Property Get Form1() As Form1
Set Form1 = mForm1
End Property
Public Property Set Form1(ByVal NewForm1 As Form1)
Set mForm1 = NewForm1
End Property
If you're using Procedure View, the property procedures can't be viewed at the same time. Click the Full Module View button at the bottom left corner of the code window to switch to Full Module View. You can return to Procedure View by clicking the Procedure View button next to it. (Hover the mouse over the buttons to see which is which.)
Add the following code to the Declarations section of Form1:
Code:
Event Gong
Private mc1 As Class1
Now that Class1 has been created, it's possible to create a variable of type Class1. This procedure switches between Form1 and Class1 several times, because a step in one module requires first adding code to the other.
Go back to Class1 and add the following code to the Declarations section.
Code:
Private WithEvents mForm1 As Form1
As discussed in "Adding Events to a Class," the WithEvents keyword means this instance of Form1 is associated with events. Note that this step wasn't possible until the Gong event had been created.
In the left-hand (Object) drop down on Class1's Code window, select mForm1 to get the event procedure for the Gong event. Add the following code to the event procedure:
Code:
Private Sub mForm1_Gong()
MsgBox "Gong!"
End Sub
Go back to Form1. In the Object drop down, select Form. In the right-hand (Procedure) drop down, select Load. Add the following code to the event procedure:
Code:
Private Sub Form_Load()
Set mc1 = New Class1
Set mc1.Form1 = Me
End Sub
The first line creates a Class1 object, and the second assigns to its Form1 property (created in step 1) a reference to Form1 (that is, Me — when you're in Form1's Code window, Me refers to Form1; when you're in Class1's Code window, Me refers to Class1).
Put three text boxes on Form1. Use the Object and Procedure drop downs to select the Change event procedure for each control in turn, and place the same line of code in each:
Code:
Private Sub Text1_Change()
RaiseEvent Gong
End Sub
Each time the contents of a text box change, the form's Gong event will be raised.
Press F5 to run the project. Each time you type a character in one of the text boxes, the message box rings a bell. It's very annoying, but it shows how you can add an event to a form, and thus get notifications from several controls.
I don't have any idea if this is what you were looking for.
Good Luck
-
Mar 18th, 2007, 07:25 PM
#5
Thread Starter
Hyperactive Member
Re: How do you create your own Events?
Sorry singularis, but after reading your comment to XShadow, I couldn't resist posting your words back to you.
@AIS4U: Thank you for that witty comment, but I do understand events. I can create my own custom events in C# and wondered if I could do the same in VB. The reason I was so harsh on XShadow was because he/she made the same mistake I have seem countless people make in that they think that they can edit documents by line numbers. I was in fact not trying to act like a .
By the way thanks for digging out the information from the MDSN.
Yes, creating a custom event was what I had in mind. I wanted to create my own events so that I could make an event based engine that fired an event when the user presses a key or when two objects collide with each other.
-
Mar 18th, 2007, 09:49 PM
#6
Re: How do you create your own Events?
If you're creating your own class/user control then it's just a matter of defining your own events, ie:
vb Code:
Option Explicit
Public Event DoubleClick()
Private Sub UserControl1_DblClick()
RaiseEvent DoubleClick()
End Sub
If you want to extend on the events, for say, a form, then you will probably have to use Subclassing.
If you're doing it for a game engine, then you could create a class called, "clsGameEvents" and put the events in there. You would need a Subclass_Start method to subclass the form you use the class on, ie:
vb Code:
'Class module code.
Option Explicit
Public Event Game_Clicked()
Public Sub Subclass_Start(byval hWnd as long)
'Start subclass code here
End Sub
'Window proc callback function
Public Function WindowProc() As Long
'This is where the subclassing actually takes place, where we receive events from Windows.
If wMsg = WM_SOME_MESSAGE_HERE then
RaiseEvent Game_Clicked()
End If
End Function
Then the code for your form:
vb Code:
Option Explicit
'WithEvents will make the class's events show in the drop down boxes.
Dim WithEvents objGameEvents as clsGameEvents
Private sub Form_Load()
Set objGameEvents = New clsGameEvents
objGameEvents.Subclass_Start me.Hwnd
End Sub
Private sub Form_Unload()
objGameEvents.Subclass_Stop
Set objGameEvents = Nothing
End Sub
'Event raised by class
Private Sub objGameEvents_GameClicked()
Debug.Print "User clicked in the game"
End Sub
Obviously that's a pretty vague explanation, but if you're familiar with subclassing then it should make sense. If you're not then go look for some subclassing code. There's a lot out there.
-
Mar 19th, 2007, 03:50 PM
#7
Re: How do you create your own Events?
 Originally Posted by AIS4U
I am sure si the geek is correct, ..
What I intended was correct, but what I actually posted was technically wrong!
What I meant was that you cannot create automatically fired events (like MouseMove etc), unless they exist already within Windows (in that case you can subclass them as DigiRev showed).
As both of you showed, events can be created for your own classes, but these must be fired using RaiseEvent, rather than firing automatically.
-
Mar 19th, 2007, 04:09 PM
#8
Thread Starter
Hyperactive Member
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
|