Results 1 to 12 of 12

Thread: [RESOLVED] Receiving two mouse down messages instead of one...

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Resolved [RESOLVED] Receiving two mouse down messages instead of one...

    This is under the category of "this only happens to me". I have an icon that I had to use "AddHandler" to trap the Mouse down event and for some reason I'm getting two messages. Here's my test code...

    Code:
        Private Sub pb_MouseDown(sender As Object, e As MouseEventArgs) Handles pb.MouseDown
            If e.Button = MouseButtons.Left Then
                MsgBox("Left Mousebutton")
            Else
                MsgBox("Right Mousebutton")
            End If
        End Sub
    When I click the icon, I get two MsgBox's come up (one after the other). Both being the same message. Either two "Left Mousebutton" messages or two "Right Mousebutton" messages.

    Has anyone ever seen this behavior when having to use AddHandler for the Mouse Down event?

  2. #2

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: Receiving two mouse down messages instead of one...

    My apologies everyone. Since I was getting two messages, I wondered if maybe I didn't need the extra Handler anymore and it turns out I don't. After I removed the AddHandler, I only got one mouse down message. Earlier I HAD to use AddHandler. And for reasons unknown, I don't need it anymore.

    again, my apologies

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,299

    Re: [RESOLVED] Receiving two mouse down messages instead of one...

    If you had to use AddHandler then you must not have had a field declared WithEvents. Handles and WithEvents always go together. If you, for instance, create controls at run time, you will not have any dedicated field declared WithEvents for them so you must use AddHandler to attach event handlers. Be sure that, if you use AddHandler, you also use RemoveHandler when you're done with the object.

  4. #4

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: [RESOLVED] Receiving two mouse down messages instead of one...

    Hi Jmc, thank you for the information. Yes, I was creating controls at run time. I've never really did it before on my own so it was a learning experience. But I'm doing better with the coding. Just yesterday I started putting in my own WithEvents and today was my first AddHandler. Thanks for letting me know about RemoveHandler. Didn't even know there was one, haha. When do you use it? As the program exits? I guess I should just read up on it and I will know.

    Today I was also working on creating my own ContextMeunus at run time. Took me a while and it's still a bit hard to remember everything I have to do to set it up. Like knowing when I get to the point that it will show up in the pull down menu with all the other controls.

    Anyway, thanks for the info. I needed it

  5. #5
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: [RESOLVED] Receiving two mouse down messages instead of one...

    Quote Originally Posted by jumper77 View Post
    This is under the category of "this only happens to me". I have an icon that I had to use "AddHandler" to trap the Mouse down event and for some reason I'm getting two messages. Here's my test code...

    Code:
        Private Sub pb_MouseDown(sender As Object, e As MouseEventArgs) Handles pb.MouseDown
            If e.Button = MouseButtons.Left Then
                MsgBox("Left Mousebutton")
            Else
                MsgBox("Right Mousebutton")
            End If
        End Sub
    When I click the icon, I get two MsgBox's come up (one after the other). Both being the same message. Either two "Left Mousebutton" messages or two "Right Mousebutton" messages.

    Has anyone ever seen this behavior when having to use AddHandler for the Mouse Down event?
    Could it be because what you were clicking on was an object named pb?
    You have a "Handles pb.MouseDown" clause on the sub, so that will add one handler, and if you also had AddHandler for the same object that would add a second event handler for the same object.

  6. #6

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: [RESOLVED] Receiving two mouse down messages instead of one...

    Haha, you are right Passel. I think what happened was at first I made the pb (picturebox) mouse down event without the Handles clause. So of course, it wasn't working. Then sometime later after putting in the AddHandler, I realized my mistake and put the Handles clause back in. Then I had to event handlers causing the double event thing to happen. At least I think that's what really happened.

    Jmc mentioned that I should also call RemoveHandler after the event had been fired and I'm a bit confused about that. Does the AddHandler stay in play if I use RemoveHandler? Because if it doesn't, wouldn't I have to make a call to AddHandler again? Sorry, I know it's dumb!

  7. #7
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: [RESOLVED] Receiving two mouse down messages instead of one...

    No, he wasn't saying remove it immediately. Just when you no longer need it.

    For instance some people add buttons and handlers, then remove buttons but forget to remove the handlers associated with those buttons.
    If you remove controls, don't forget to move any handlers added for those controls.

  8. #8

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: [RESOLVED] Receiving two mouse down messages instead of one...

    Ok thanks for clearing that up. It's something that will be used the entire time the program is running so I will just add it to the cleanup routines I already have in place. But let me ask, does that mean that using it over and over without Releasing the Handler would make the memory being used to keep going up? If so, I can always remove the handler after it's used then start a new handler again.

  9. #9
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: [RESOLVED] Receiving two mouse down messages instead of one...

    What do you mean using it over and over?
    The event handler is just a sub that will be called when the event associated with it is raised.
    You're associated the MouseDown event of a control with the Sub you want to be called when the event is triggered.
    It doesn't matter if you click the mouse once, or you click it a hundred times, there shouldn't be any increase in memory use because the sub is being called for each event. You do have small amount of memory added for each association you add, and there will be some possible extra worked necessary for each association you create, because the list, or dictionary, or whatever hierarchy is used to store these relationships has to be searched and the calls done when the events occur. I'm sure this is done in a fairly efficient way, not just a simple linear list that has to be searched.

    As you saw in your case, you can add a handler multiple times to the same event.
    The fact is, this can be a many to many relationship here, i.e. you can associated the same event of a control to multiple handlers, so each handler will be called when the event occurs. This is essentially what you did, although in your case you had the same event call the same handler twice, rather than different handlers.
    Also, you can have multiple events (as long as the signature matches) from multiple controls call the same event handler.
    You can have all four cases.
    1. One event is associated with one handler.
    2. One event is associated with multiple handlers.
    3. Multiple events are associated with one handler.
    4. Multiple events are associated with multiple handlers.

    The normal case would be 1, and a common case, I think, would be 3.
    I haven't had any reason to consider using cases 2 or 4, but there could be a reason such as logging or some sort of testing I suppose.

  10. #10

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: [RESOLVED] Receiving two mouse down messages instead of one...

    Good morning Passel. Thanks for your reply. What I meant by "Using it over and over" was it's not something that's only going to occur once. It's basically like a button click. And if there is a cost of not releasing the event handler, I would guess it would be like not closing a file handle. All of the software I worked on before I retired stayed running for as long as possible. And if the software stayed up for months (and hopefully it did), a simple file handle leak could be the "kiss of death". I only mention this because someone thought I worry about memory leaks too much.

    If it causes no harm, I would release the handler then start a new one. Would that work?

    The four cases you mentioned are interesting. I will be giving it some thought after I finish this message. In the case of logging, I could see where someone could use case number 2. As I'm sure you can tell, this is new to me. I'm using a File System Watcher in my program now and I really like the way it works. It's like having code that knows when to execute itself and you don't have to think about it. I think that's pretty cool.

    I'm learning and I'm slowly getting there. With the input from you and others, I have learned a lot recently and for that, I thank you.

  11. #11
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    6,582

    Re: [RESOLVED] Receiving two mouse down messages instead of one...

    Quote Originally Posted by jumper77 View Post
    ...
    If it causes no harm, I would release the handler then start a new one. Would that work?
    ....
    It would work, but I still don't understand your use case that would require this.
    If you add a button in the IDE, and doubleclick on it, a click event handler sub is created automatically for you, and a Handles clause is added to the sub so that the click event for that control will associated with that sub and when the user causes a click event to occur on that control, e.g. by mouse, keyboard or code, then the code in that event handler will be run.

    That association of event to event handler exists for as long as that button exists.

    When you add your own controls dynamically, there is probably a 50/50 chance that you might want that control to do something if the user interacts with it, so you use AddHandler to tie the event you want to have an effect to the sub you want to process that event. If for some reason you decide you don't want to respond to that event any longer, i.e. essentially disabling that functionality, then I suppose you can remove the tie of the event to the handler, but that seems counter-intuitive to have a control work sometimes and appear to fail at other times. That is why a control has an enable property, so you can disable it and have an indication to the user that the control is disabled, AKA failing on purpose.

    If you were removing the control, then yes, you should remove the handlers you added first, but otherwise I see no benefit in thrashing the event processing mechanism by adding and removing ties from control events to the event handler methods that support them. You're not adding or subtracting any subs, so you are not adding or removing a bunch of code from memory when you add or remove handlers.

    You can do what you want, but I don't see the need for it.

    I don't know that this is the case, but I could see the situation where someone thinks they are improving things by constantly adding objects when they need them, and then removing them when done, only to add them again the next time needed, and removing them again, repeatedly. While that should be the case when you're dealing with local objects in a method and it is done that way by design, in the case of global objects that are outside the methods, adding and removing and re-adding objects can undo some efficiencies that are built into computer systems that are trying to improve processing, e.g. memory caching for instance. If you keep moving an object around in memory by removing it and recreating it, it makes it hard to have it readily at hand in a local cache, so you have multiple levels of slowdown built into your process. The time taken to remove an object, the time taken to recreate the object, and the probable memory access hit taken as the object is no longer cached in a fast local memory area.

  12. #12

    Thread Starter
    PowerPoster
    Join Date
    Feb 2016
    Location
    Tennessee
    Posts
    2,437

    Re: [RESOLVED] Receiving two mouse down messages instead of one...

    It would work, but I still don't understand your use case that would require this.
    If you add a button in the IDE, and doubleclick on it, a click event handler sub is created automatically for you, and a Handles clause is added to the sub so that the click event for that control will associated with that sub and when the user causes a click event to occur on that control, e.g. by mouse, keyboard or code, then the code in that event handler will be run.
    I don't require it anymore. This started out because I added a handler because I did something stupid (like creating a sub with no "handles" clause when there should have been. I didn't create the sub by double clicking on a control. It was after I fixed that sub that I started getting two messages. Then I realized what was happening and removed the Handler.

    And I understand that the Handler exists for as long as the control does. My question was would it be like not closing a file handle. Meaning memory would never be released until the application shut down. Although I have no use for the Handler anymore, I'm just trying to get information for future reference.

    When you add your own controls dynamically, there is probably a 50/50 chance that you might want that control to do something if the user interacts with it, so you use AddHandler to tie the event you want to have an effect to the sub you want to process that event. If for some reason you decide you don't want to respond to that event any longer, i.e. essentially disabling that functionality, then I suppose you can remove the tie of the event to the handler, but that seems counter-intuitive to have a control work sometimes and appear to fail at other times. That is why a control has an enable property, so you can disable it and have an indication to the user that the control is disabled, AKA failing on purpose.
    I never had any plan to disable any functionality. And I understand what the AddHandler is for. I was only asking about the memory implications for using the event handler repeatedly. Even if we are just talking about a small piece of memory, I would consider that important.

    You can do what you want, but I don't see the need for it.
    Haha, that's what I've been saying. I have no need for it. I just screwed up and did something dumb. When I realized it, I removed the "dumbness", lol. I've been trying to take advantage of the conversation for future use. Things I should know and keep in mind. I don't see me using two Handlers on purpose. I've never had the need for it, but like you said, there may be times when it offers a solution in a particular case.

    And I do thank you for the information. I will continue to look into it and try to learn

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