|
-
Aug 19th, 2012, 04:43 PM
#1
Thread Starter
New Member
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.
-
Aug 20th, 2012, 01:49 AM
#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.
-
Aug 20th, 2012, 03:21 AM
#3
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?
-
Aug 20th, 2012, 12:18 PM
#4
Thread Starter
New Member
Re: Cannot remove handler of an event with RemoveHandler
 Originally Posted by formlesstree4
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!
-
Aug 20th, 2012, 12:18 PM
#5
Thread Starter
New Member
Re: Cannot remove handler of an event with RemoveHandler
 Originally Posted by Evil_Giraffe
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.
-
Aug 20th, 2012, 12:19 PM
#6
Thread Starter
New Member
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.
-
Aug 20th, 2012, 12:29 PM
#7
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?
-
Aug 20th, 2012, 12:38 PM
#8
Thread Starter
New Member
Re: Cannot remove handler of an event with RemoveHandler
 Originally Posted by dunfiddlin
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.
-
Aug 20th, 2012, 12:39 PM
#9
Thread Starter
New Member
Re: Cannot remove handler of an event with RemoveHandler
 Originally Posted by dunfiddlin
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.
-
Aug 21st, 2012, 03:22 AM
#10
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:
Private _countdown As Integer ' Initialise this to 10 just before you start the timer
Private Sub Timer1_Tick(sender As System.Object, e As System.EventArgs) Handles Timer1.Tick
If _countdown > 0 Then
_countdown = _countdown - 1
Label4.Text = _countdown.ToString()
End If
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!)
-
Aug 21st, 2012, 06:46 AM
#11
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|