Jump to content

How to Count number of button has been Click?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
Guest_roger_*

Guest_roger_*
  • Guests
Hello
I would like to be able determine how many times the user has been click one button. Does any one how I could able to count how many times the button has been click. Note: No Timer

thank you

#2
Crane

Crane

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 398 posts
So every time a user clicks a certain button you would like to count the clicks? Why don't you create a global variable and then add one to it every time the button is clicked?

#3
brackett

brackett

    Programmer

  • Members
  • PipPipPipPip
  • 192 posts
Even better, from an OOP perspective, would be to inherit the Button class, override OnClick, and keep track of it in there. You could expose a ReadOnly Property to show the count.

But, yeah, you could just store it in a global.

#4
Lop

Lop

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,172 posts
Yup, either option would work. I think the proper way to do it would be in a class but the quick and dirty way would be a global.

Why is it such a bad thing to use globals anyway?

#5
brackett

brackett

    Programmer

  • Members
  • PipPipPipPip
  • 192 posts

Lop said:

Why is it such a bad thing to use globals anyway?

Globals are bad because they represent shared state. Shared state is bad because it means different pieces of code depend on each other. Obviously, you have to have shared state information. But, by encapsulating it into a class who's job it is to track and update the information, you've centralized the logic associated with that shared state.

Now, that being said, globals aren't really such a bad thing - they're just something to watch out for. Sometimes, the convenience of a global is worth it. But, then again, it's usually not too hard to replace a global with something (if only slightly) better.

Back to the original question, I should've tried to flesh out why the number of clicks needed to be tracked. I suspect that road would lead us to a class who's job would be to take some action based on clicks - and that job should probably be moved out of the Form (where it no doubt lives right now). By proposing a ReadOnly Property to expose that information, I think I probably proposed splitting out the logic that deals with the number of clicks - and that's not normally a good thing.

Just moving the tracking information inside a subclassed Button isn't really the whole solution, but based on the info given - it was the best I had at the moment.