Results 1 to 8 of 8

Thread: How do you create your own Events?

  1. #1

    Thread Starter
    Hyperactive Member singularis's Avatar
    Join Date
    Nov 2006
    Location
    Over There!
    Posts
    372

    Exclamation 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
    If what I said was helpful, give me rep!

    My Complete Games: -- 2D Zone (Space Shooter game) || _2D Zone 2_ || Ninja Blob (2D platformer) || Dren (Co-op up to 4 player base defence game)

    My Projects: -- The Dread Engine (2D VB game Engine) || A* Path Finding


    An excellent site for learning DirectX7, 8 & 9 (for VB6, C# & VB.net) would be: directx4vb.vbgamer.com --- For my projects and games see: pieper.freehostia.com

  2. #2
    Frenzied Member
    Join Date
    Aug 2005
    Posts
    1,042

    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!

  3. #3
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    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?

  4. #4
    Frenzied Member
    Join Date
    Aug 2005
    Posts
    1,042

    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

  5. #5

    Thread Starter
    Hyperactive Member singularis's Avatar
    Join Date
    Nov 2006
    Location
    Over There!
    Posts
    372

    Talking 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.
    If what I said was helpful, give me rep!

    My Complete Games: -- 2D Zone (Space Shooter game) || _2D Zone 2_ || Ninja Blob (2D platformer) || Dren (Co-op up to 4 player base defence game)

    My Projects: -- The Dread Engine (2D VB game Engine) || A* Path Finding


    An excellent site for learning DirectX7, 8 & 9 (for VB6, C# & VB.net) would be: directx4vb.vbgamer.com --- For my projects and games see: pieper.freehostia.com

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

    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:
    1. Option Explicit
    2.  
    3. Public Event DoubleClick()
    4.  
    5. Private Sub UserControl1_DblClick()
    6.     RaiseEvent DoubleClick()
    7. 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:
    1. 'Class module code.
    2. Option Explicit
    3.  
    4. Public Event Game_Clicked()
    5.  
    6. Public Sub Subclass_Start(byval hWnd as long)
    7.     'Start subclass code here
    8. End Sub
    9.  
    10. 'Window proc callback function
    11. Public Function WindowProc() As Long
    12.     'This is where the subclassing actually takes place, where we receive events from Windows.
    13.     If wMsg = WM_SOME_MESSAGE_HERE then
    14.         RaiseEvent Game_Clicked()
    15.     End If
    16. End Function
    Then the code for your form:

    vb Code:
    1. Option Explicit
    2.  
    3. 'WithEvents will make the class's events show in the drop down boxes.
    4. Dim WithEvents objGameEvents as clsGameEvents
    5.  
    6. Private sub Form_Load()
    7.     Set objGameEvents = New clsGameEvents
    8.     objGameEvents.Subclass_Start me.Hwnd
    9. End Sub
    10.  
    11. Private sub Form_Unload()
    12.     objGameEvents.Subclass_Stop
    13.     Set objGameEvents = Nothing
    14. End Sub
    15.  
    16. 'Event raised by class
    17. Private Sub objGameEvents_GameClicked()
    18.     Debug.Print "User clicked in the game"
    19. 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.

  7. #7
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: How do you create your own Events?

    Quote 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.

  8. #8

    Thread Starter
    Hyperactive Member singularis's Avatar
    Join Date
    Nov 2006
    Location
    Over There!
    Posts
    372

    Talking Re: How do you create your own Events?

    @AIS4U: Yep, that was very interesting....

    @DigiRev: Thanks for the reply, but I got away with only having to create the object rather than starting it and ending it. Good ol' VB.

    @si_the_geek: Yes, you skim read my post and thought I was learning how to receive built-in events
    If what I said was helpful, give me rep!

    My Complete Games: -- 2D Zone (Space Shooter game) || _2D Zone 2_ || Ninja Blob (2D platformer) || Dren (Co-op up to 4 player base defence game)

    My Projects: -- The Dread Engine (2D VB game Engine) || A* Path Finding


    An excellent site for learning DirectX7, 8 & 9 (for VB6, C# & VB.net) would be: directx4vb.vbgamer.com --- For my projects and games see: pieper.freehostia.com

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