Counting times a control is clicked.

  • Thread starter Thread starter Al@n
  • Start date Start date
A

Al@n

Can anyone tell me if there's a way to return the amount
of times a button is "clicked"

I need to change text in a text box and that text changes
each time the button is clicked-up to 6 times with 6
different text values.

Thanks in anticipation.

Al@n
 
Al@n said:
Can anyone tell me if there's a way to return the amount
of times a button is "clicked"

Is this a trick question :-) Wouldn't you just keep track by incrementing a
counter?
 
Create a module level variablle and increment it via the Buttons Click
event. Other than that, i'm not sure there's such a property.
 
* "Al@n said:
Can anyone tell me if there's a way to return the amount
of times a button is "clicked"

I need to change text in a text box and that text changes
each time the button is clicked-up to 6 times with 6
different text values.

\\\
Private Sub Button1_Click(...) Handles...
Static n As Integer
n = n + 1
Me.Label1.Text = n.ToString()
End Sub
///
 
Hi Tom.

No, I dont think it is a trick question. The OP needs a static ( Shared )
integer doesent he/she ?, The problem is that the OP has asked an innocent
question, your mind has told you that there must be more to it !

BTW, I saw you struggling with J# the other day in the J# newsgroup, I was
also struggling with J# at the same time thats why I saw you there. It's a
pain from the documentation point of view isnt it ?

Regards - OHM
 
One Handed Man said:
No, I dont think it is a trick question. The OP needs a static ( Shared )
integer doesent he/she ?, The problem is that the OP has asked an innocent
question, your mind has told you that there must be more to it !

I thought the OP needed to keep track of how many times a button on a form
was clicked. Any old integer property declared in the form will do it I
think but I could be wrong.
 
Gents
What I'm trying to achieve is a simulation of a controlpanel used in
work-for training purposes.

In pushing a particular button a number of times it will alter the "display
panel" each click event until
interrupted by another button is clicked

I need to retain the number of clicks on the original button to recommence
the display at the point it was interrupted.

I'm sorry if the question seemed silly but am new to this game and couldn't
work out how to achieve my goal.

Thanks for the assistance offered

Al@n
 
Al@n said:
Gents
What I'm trying to achieve is a simulation of a controlpanel used in
work-for training purposes.

In pushing a particular button a number of times it will alter the "display
panel" each click event until
interrupted by another button is clicked

I need to retain the number of clicks on the original button to recommence
the display at the point it was interrupted.

I'm sorry if the question seemed silly but am new to this game and couldn't
work out how to achieve my goal.

Thanks for the assistance offered

It isn't that your question is silly I just thought there must be more to
it. Each time a button is clicked you want to increment a variable to keep
track of the number of clicks right? You've surely assigned values to
variables in other places and you must have placed code in a click event at
some point. Isn't your solution a combination of these things?

I figure I must have something wrong. Place an integer variable/property on
your form and increment it in your button "click" handler.

I'm not ragging on you but "new" to programming or "new" to .Net? It's hard
to know how much detail to give in a response...
 
Well, Tom is right you could increment a form class level integer and
increment that, but using a static integer in the method is tidier I think.

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click

Static Dim count As Integer = 0

count += 1

lblCount.Text = count.ToString

End Sub

Regards - OHM
 
I am pretty certain the OP posted something about needing to reset it after
6 presses or something like that. That logic can also be written into the
click handler but roughly 90% of the postings suggest that placing business
logic into an event handler is not a great idea. And if he wants to click
Button2 to have the value reset then what?

I still don't know what he's ultimately trying to do but I will suggest (as
an alternative) that he model his app in a class designed to
handle/process/maintain whatever states his "little machine" needs to keep
track of.

Al@n: You can clearly scatter static variables throughout your UI code...
I'll recommend against it. Have you developed a class before? If you have
then it's time to do it again, if not, it might be a good time to consider
it. Your object will contain all the data related to itself, it can raise
events, etc., etc.
 
On reflection, I think I agree with you, a Private Integer field is probably
better than using a static in the event handler, where you put it is another
matter and therefore whether it is static or not. If the button is wrapped
then you might want a property of the class you wrap it in which internally
refers to a static field. If not then at form level as an integer.

OHM
 
Tom

Thanks to the advice given by yourself and OHM, I think I can sort out the
issue on my form.
I'm cetainly new to .Net and this is the first project I've attempted. I've
some experience in VBA, particularly user input in Word.
Thanks both again

Al@n
 
Back
Top