Results 1 to 11 of 11

Thread: Cannot remove handler of an event with RemoveHandler

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2012
    Posts
    6

    Red face Cannot remove handler of an event with RemoveHandler

    Hello everyone!

    This is Marcos from Spain. This is my first post to tell you this little problem annoying me.

    I am doing a very simple game, and it has a 10 seconds countdown. There is a big button for clicking and each time you click you get one point. The timer starts, and the countdown starts like this: (the following is a part of the Timer1.Tick event handler)

    Code:
    If Label4.Text > 0 Then
                Label4.Text = Label4.Text - 1
    End If
    assuming

    Code:
    Timer1.Interval = 1000
    So, when the countdown reaches zero, what I'd like to do is to STOP handling the Button2.Click event and what I wrote was this:

    Code:
    If Label4.Text = 0 Then
                RemoveHandler Button2.Click, Button2_Click()
                Timer1.Stop()
            End If
    Basicly, it ain't working. When I click on the button it keeps adding points.

    Keep in mind that after playing one time, the user must be able to play more times. The program was fully working with a Start/Stop button, but 1. I need challenge and learning and 2.The user loses time clicking the button. So please stick to this solution, because I've thought on a MouseHover starting system.

    Thank you so much from a noob.

  2. #2

    Re: Cannot remove handler of an event with RemoveHandler

    You're probably going about this the wrong way. Why not just disable the button? Also, subtracting like that is a very bad idea and can cause all sorts of problems.

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

    Re: Cannot remove handler of an event with RemoveHandler

    I would assume RemoveHandler only removes handlers added with AddHandler. I'm guessing you've got a 'Handles Button2.Click' to wire it up, yes?

  4. #4

    Thread Starter
    New Member
    Join Date
    Aug 2012
    Posts
    6

    Re: Cannot remove handler of an event with RemoveHandler

    Quote Originally Posted by formlesstree4 View Post
    You're probably going about this the wrong way. Why not just disable the button? Also, subtracting like that is a very bad idea and can cause all sorts of problems.
    The problem with disabling the button is that I the MouseHover event wouldn't work. I tried and I didn't, but if you can tell me how with the Button disabling method I'd be really happy!

  5. #5

    Thread Starter
    New Member
    Join Date
    Aug 2012
    Posts
    6

    Re: Cannot remove handler of an event with RemoveHandler

    Quote Originally Posted by Evil_Giraffe View Post
    I would assume RemoveHandler only removes handlers added with AddHandler. I'm guessing you've got a 'Handles Button2.Click' to wire it up, yes?
    Sure, I have a Handles Button2.Click on the Sub.

  6. #6

    Thread Starter
    New Member
    Join Date
    Aug 2012
    Posts
    6

    Re: Cannot remove handler of an event with RemoveHandler

    Aaand... I'd love to know how to substract on a better way. Or a different way for the countdown.

  7. #7
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Cannot remove handler of an event with RemoveHandler

    Am I missing something? Wouldn't a simple ...

    If Label4.Text <> "0"

    ... or similar at the head of all the code in the Click event effectively enable/disable it?

  8. #8

    Thread Starter
    New Member
    Join Date
    Aug 2012
    Posts
    6

    Re: Cannot remove handler of an event with RemoveHandler

    Quote Originally Posted by dunfiddlin View Post
    Am I missing something? Wouldn't a simple ...

    If Label4.Text <> "0"

    ... or similar at the head of all the code in the Click event effectively enable/disable it?
    Ehr... I think so. And I'm stupid. Let me have a try, maybe I am missing something.

  9. #9

    Thread Starter
    New Member
    Join Date
    Aug 2012
    Posts
    6

    Re: Cannot remove handler of an event with RemoveHandler

    Quote Originally Posted by dunfiddlin View Post
    Am I missing something? Wouldn't a simple ...

    If Label4.Text <> "0"

    ... or similar at the head of all the code in the Click event effectively enable/disable it?
    Did work indeed. Would love to hear from formlesstree4 about the proper way for countdowns.

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

    Re: Cannot remove handler of an event with RemoveHandler

    There are two issues with the subtraction. The first is that you're doing arithmetic on strings. You should be keeping the numeric value of the countdown in a numeric field, something like this:

    vbnet Code:
    1. Private _countdown As Integer ' Initialise this to 10 just before you start the timer
    2.  
    3. Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
    4.     If _countdown > 0 Then
    5.         _countdown = _countdown - 1
    6.         Label4.Text = _countdown.ToString()
    7.     End If
    8. End Sub

    The second issue is that you're not guaranteed to get the timer to tick at exactly 1 second intervals. We're talking microseconds difference, but still, worth bearing in mind. There are ways around this if it is very important but they up the complexity of the code by quite a degree so really decide whether it's worth getting in to the quagmire (especially given that you can't actually guarantee exactly 10s whatever way you do it)
    Last edited by Evil_Giraffe; Aug 21st, 2012 at 03:35 AM. Reason: (Forgot to decrement the _countdown var... whoops!)

  11. #11
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Cannot remove handler of an event with RemoveHandler

    The removeHandler is syntatically wrong.... if one was to check the documentation, one would find that the two parameters is 1) the Event, and 2) the DELEGATE to the event handler... NOT the name of the event handler. Eh? What's that mean? Means you need to pass it the ADDRESS of the handler...

    RemoveHandler Button2.Click, AddressOf Button2_Click

    -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