Results 1 to 11 of 11

Thread: Cannot remove handler of an event with RemoveHandler

  1. #1
    New Member
    Join Date
    Aug 12
    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
    PowerPoster formlesstree4's Avatar
    Join Date
    Jun 08
    Location
    On the Internet
    Posts
    2,845

    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
    Frenzied Member Evil_Giraffe's Avatar
    Join Date
    Aug 02
    Location
    Suffolk, UK
    Posts
    1,876

    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
    New Member
    Join Date
    Aug 12
    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
    New Member
    Join Date
    Aug 12
    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
    New Member
    Join Date
    Aug 12
    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
    I'm about to be a PowerPoster! dunfiddlin's Avatar
    Join Date
    Jun 12
    Posts
    5,465

    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
    New Member
    Join Date
    Aug 12
    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
    New Member
    Join Date
    Aug 12
    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
    Frenzied Member Evil_Giraffe's Avatar
    Join Date
    Aug 02
    Location
    Suffolk, UK
    Posts
    1,876

    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 02
    Posts
    21,636

    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 subscribe to all threads I participate, so there's no need to pm when there's an update.*
    *Proof positive that searching the forums does work: View Thread *
    * 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??? *
    * Use Offensive Programming, not Defensive Programming. * On Error Resume Next is error ignoring, not error handling(tm).
    "There is a major problem with your code, and VB wants to tell you what it is.. but you have decided to put your fingers in your ears and shout 'I'm not listening!'" - si_the_geek on using OERN

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •