Results 1 to 9 of 9

Thread: raise event question

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Dec 2011
    Posts
    109

    raise event question

    I have a piece of hardware wired to a switch. This interfaces with an existing piece of software. In the software there is a label that you can double click on to get to another screen, and that part works perfectly. What I want is when the switch is tripped the double click event is raised. I am trying to call the label's doubleclick event, because the code is in the event handler and not in a seperate function. I know that coding in the event handler is not best practice, and that I should call a function so it can easily be done, but this is a small change to a large piece of software, and there is likely never going to be another change needed.

    My problem is "ByVal sender As System.Object, ByVal e As System.EventArgs" is passed to the doubleclick as parameters and the switch is simply coming in over a com port and is calling the function like this:

    Code:
    Public Sub switch()
    
            If current_delta = 2 Then
    
                RaiseEvent Label2_DoubleClick()
    
            End If
    
        End Sub
    I don't have those parameters to pass to the event handler. I have read online to use the label as the object, and the action as the eventargs, but the label doesn't have a doubleclick argument that seems to be usable in this way.

    Thanks for any help.

  2. #2
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,712

    Re: raise event question

    You would not be raising the double_Click event, rather you will be calling the routine that is handled by the double click event. In order to call it, just use this
    Code:
    Label2_DoubleClick(nothing,nothing)
    And you are right, this is not the best practice, but sometimes, you have to do what you have to do.
    kevin
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  3. #3
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    32,972

    Re: raise event question

    OR.... you put all the code into a SEPERATE method, and call it when you need to... from the event handler, from somewhere else... etc.

    verrrrrrry last post in this thread https://social.msdn.microsoft.com/Fo...orum=vbgeneral illustrates the idea.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4
    PowerPoster
    Join Date
    Oct 2010
    Posts
    2,141

    Re: raise event question

    There is nothing magical about events or their handlers. The EventHandler for the DoubleClick event is nothing but a delegate that gets invokes by the Control.OnDoubleClick method. This method is called based on the receipt of windows messages received by the control's WndProc method.

    Code:
    Public Delegate Sub EventHandler(ByVal sender As Object, ByVal e As EventArgs)
    Code:
        <EditorBrowsable(EditorBrowsableState.Advanced)> _
        Protected Overridable Sub OnDoubleClick(ByVal e As EventArgs)
            Dim handler As EventHandler = DirectCast(MyBase.Events.Item(Control.EventDoubleClick), EventHandler)
            If (Not handler Is Nothing) Then
                handler.Invoke(Me, e)
            End If
        End Sub
    What people often refer to as the event handler is the method to be called when you subscribe to the event using:
    Code:
    AddHandler Label2.DoubleClick, AddressOf Label2_DoubleClick
    When you have a VB.Net Method defined as follows:
    Code:
    Private Sub Label2_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Label2.DoubleClick
       ' do something
    End Sub
    the compiler generates that AddHandler code for you and places it into the class's constructor (Sub New).

    So what I am getting at with all this is that if you call method "Label2_DoubleClick" directly, that it will have the same effect as raising the DoubleClick event on Label2.

    i.e.
    Code:
    Label2_DoubleClick(Nothing, Nothing)
    If you want to gain a better understanding of how events function, work through the example shown in:
    How to: Declare Custom Events To Avoid Blocking and set through the code in the debugger.

    That said, I do not understand your reluctance to restructure your code a bit.

  5. #5
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,712

    Re: raise event question

    Yea, having learned this by experience, coding in that way looks tempting because it's only adding one line and it's quick and easy... besides what can go wrong huh? It's only one line. Then you do it again, and again. Next thing you know you can't follow your code and when you do have to do bigger changes you end up fixing all the little shortcuts anyway.


    Quote Originally Posted by TnTinMN
    So what I am getting at with all this is that if you call method "Label2_DoubleClick" directly, that it will have the same effect as raising the DoubleClick event on Label2.
    Only if Label_DoubleClick is the only handler attached to the event. If there are multiple handlers, the others don't run.
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  6. #6
    PowerPoster
    Join Date
    Oct 2010
    Posts
    2,141

    Re: raise event question

    Quote Originally Posted by kebo View Post
    Only if Label_DoubleClick is the only handler attached to the event. If there are multiple handlers, the others don't run.
    Fair enough point, but my comment was written in the context of the OP's stated condition of wanting to run the code in the Label2_DoubleClick method (at least that is how I read it) and in that context calling the method directly as you also pointed out while writing my response will do the job.

  7. #7
    Still learning kebo's Avatar
    Join Date
    Apr 2004
    Location
    Gardnerville,nv
    Posts
    3,712

    Re: raise event question

    Quote Originally Posted by TnTinMN
    There is nothing magical about events or their handlers.
    No more magical than these event handlers created in laundry dryers
    Process control doesn't give you good quality, it gives you consistent quality.
    Good quality comes from consistently doing the right things.

    Vague general questions have vague general answers.
    A $100 donation is required for me to help you if you PM me asking for help. Instructions for donating to one of our local charities will be provided.

    ______________________________
    Last edited by kebo : Now. Reason: superfluous typo's

  8. #8
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: raise event question

    Quote Originally Posted by kebo View Post
    YAnd you are right, this is not the best practice, but sometimes, you have to do what you have to do.
    Sometimes, yes. Not in this case, however. What on earth is the problem with extracting a method from the handling method and calling it? Whether you expect the code to change again is somewhat irrelevant here. It's a quicker change than fretting about how you can satisfy the un-needed arguments in the current structure.

  9. #9
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    32,972

    Re: raise event question

    Well, it's a quick way to get more work I suppose... 'round here, calling the events directly like that will get you a big fat F on the code review and sent back for refactoring.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

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