Connecting Tech Pros Worldwide Forums | Help | Site Map

count click

Newbie
 
Join Date: Mar 2007
Posts: 11
#1: Mar 28 '07
I am trying to count the amount of times that a button is clicked and when it is clicked 10 times I want to bring a message box.

I have tried doing this with an if statement and a loop. with the if statement i can't seem to assign it to the button.

If count < 10 Then
Else
finalscore = score
MessageBox.Show("you final score is" & finalscore)
end if


can any1 help?
Familiar Sight
 
Join Date: Mar 2007
Posts: 150
#2: Mar 28 '07

re: count click


Quote:

Originally Posted by spud379

I am trying to count the amount of times that a button is clicked and when it is clicked 10 times I want to bring a message box.

I have tried doing this with an if statement and a loop. with the if statement i can't seem to assign it to the button.

If count < 10 Then
Else
finalscore = score
MessageBox.Show("you final score is" & finalscore)
end if


can any1 help?

Hi,
Can we have a global variable and have that incremented each time the button is cliecked. Then when the value is 10 we can display a message that the final score is ...

thanks
ansuman sahu
Newbie
 
Join Date: Feb 2007
Location: India
Posts: 20
#3: Mar 28 '07

re: count click


Quote:

Originally Posted by spud379

I am trying to count the amount of times that a button is clicked and when it is clicked 10 times I want to bring a message box.

I have tried doing this with an if statement and a loop. with the if statement i can't seem to assign it to the button.

If count < 10 Then
Else
finalscore = score
MessageBox.Show("you final score is" & finalscore)
end if


can any1 help?

try out dis
if count=10 then
msgbox("score is"&finalscore)
else
count=count+1
end if
Newbie
 
Join Date: Mar 2007
Posts: 16
#4: Mar 28 '07

re: count click


Halu! :-) Create a project in vb. Add 1 button on the form, also add a textbox.
The textbox just show you the number of clicked you've done. If the number of clicked is equal to 10, a msgbox will be displayed, and the counter variable A is set back to its initial value which is zero(0).


Dim a As Integer

Private Sub Command1_Click()
a = a + 1
Text1.Text = a
If a = 10 Then
MsgBox "You click the button " & a & " times"
a = 0
Text1.Text = a
End If
End Sub

Private Sub Form_Load()
a = 0
End Sub
Newbie
 
Join Date: Mar 2007
Posts: 11
#5: Mar 28 '07

re: count click


hi
thanks for all you help but still not quite what i want. the thing is im doing it for a assignment at uni. although most of the thing u been saying work does not quite fit in with the assignment.

I have emailed my tutor and he has emailed this back

Each time the user submits there answer to a question (ie clicks
on a button) treat this as one step or increment of a counter. Thus the
counter starts at 0 and the first time they click the button the
counter is incremented ie counter=counter + 1. If you increment the
counter inside the event handler ie the click event then every time the
user submits the answer the counter will increase by one.

If you add a selection statement within the event handler to disable
the button after 10 clicks (ie the counter reaches 10) and then print
the result you have the necessary control structures to complete the
test.


still not sure what to do with event handler. any help would be great thanks
SammyB's Avatar
Moderator
 
Join Date: Mar 2007
Location: Springfield, Ohio
Posts: 729
#6: Mar 28 '07

re: count click


>not sure what to do with event handler
While you are designing your form, if you double-click on the button, VB creats an event handler that is called whenever you press the button. That is where you put your original If Then Else statement.

>the counter starts at 0
That is what you called "count". It should be dimmed as static at the top of the event handler. Normally variables are reinitialized each time a routine is called, but VB static variables are initialized to 0 the first time and kept from call to call.

>click the button the counter is incremented ie counter=counter + 1
You forgot to do this in your If

>disable the button after 10 clicks
This would replace the stuff in your else clause. Do you remember how to disable a button?
Moderator
 
Join Date: Oct 2006
Location: Australia
Posts: 7,748
#7: Mar 29 '07

re: count click


The experts on this site are more than happy to help you with your problems but they cannot do your assignment/program for you. Attempt the assignment/program yourself first and post questions regarding any difficulties you have or about a particular function of the code that you don't know how to achieve.

Please read the Posting Guidelines and particularly the Coursework Posting Guidlines.

Then when you are ready post a new question in this thread.

MODERATOR
Newbie
 
Join Date: Mar 2007
Posts: 11
#8: Apr 4 '07

re: count click


Quote:

Originally Posted by Killer42

The experts on this site are more than happy to help you with your problems but they cannot do your assignment/program for you. Attempt the assignment/program yourself first and post questions regarding any difficulties you have or about a particular function of the code that you don't know how to achieve.

Please read the Posting Guidelines and particularly the Coursework Posting Guidlines.

Then when you are ready post a new question in this thread.

MODERATOR


I was not looking for the answers more to b pointed in the right direction and to understand some of the termonology.

I will be quoting this website in my biblography.
SammyB's Avatar
Moderator
 
Join Date: Mar 2007
Location: Springfield, Ohio
Posts: 729
#9: Apr 4 '07

re: count click


Quote:

Originally Posted by spud379

I was not looking for the answers more to b pointed in the right direction and to understand some of the termonology.

I will be quoting this website in my biblography.

But, does it work? :D --Sam
vijaydiwakar's Avatar
Site Addict
 
Join Date: Feb 2007
Posts: 579
#10: Apr 4 '07

re: count click


Quote:

Originally Posted by SammyB

But, does it work? :D --Sam

:D nice Jo
Newbie
 
Join Date: Mar 2007
Posts: 11
#11: Apr 11 '07

re: count click


Quote:

Originally Posted by SammyB

But, does it work? :D --Sam

sorry mate have not tried it yet had a lot on. i try it 2day.
Newbie
 
Join Date: Mar 2007
Posts: 11
#12: Apr 11 '07

re: count click


no its not working i tried count=count+1 and declairing as static. i must be doing something simple wrong i no that it is going into the if statement because it comes up with the messagebox after statement. and buttons disabled but not looping 10 times.
SammyB's Avatar
Moderator
 
Join Date: Mar 2007
Location: Springfield, Ohio
Posts: 729
#13: Apr 11 '07

re: count click


Quote:

Originally Posted by spud379

no its not working i tried count=count+1 and declairing as static. i must be doing something simple wrong i no that it is going into the if statement because it comes up with the messagebox after statement. and buttons disabled but not looping 10 times.

So, show us the code.
Moderator
 
Join Date: Oct 2006
Location: Australia
Posts: 7,748
#14: Apr 11 '07

re: count click


Quote:

Originally Posted by spud379

no its not working i tried count=count+1 and declairing as static. i must be doing something simple wrong i no that it is going into the if statement because it comes up with the messagebox after statement. and buttons disabled but not looping 10 times.

You do realise, I hope, that a variable declared within a procedure (a Sub or Function) only exists within that procedure? It may be static (remains between calls to the procedure, so doesn't get wiped each time) but it still cannot be accessed from outside of that procedure.

If you have declared a static variable within a procedure and then used the same name outside of the procedure, VB is probably just automatically creating a new variable for you, with the specified name.

You should always keep the "require explicit variable declaration" option turned on, to prevent this sort of problem.

On a simpler note, did you realise that in the code Lavs originally posted, a was set to zero just before displaying it?
SammyB's Avatar
Moderator
 
Join Date: Mar 2007
Location: Springfield, Ohio
Posts: 729
#15: Apr 16 '07

re: count click


Quote:

Originally Posted by spud379

no its not working i tried count=count+1 and declairing as static. i must be doing something simple wrong i no that it is going into the if statement because it comes up with the messagebox after statement. and buttons disabled but not looping 10 times.

Make sure you followed Killer's suggestions and show us the code.
Newbie
 
Join Date: Jan 2007
Posts: 10
#16: May 3 '07

re: count click


Exactly what i was looking for. Thanks dude.


Quote:

Originally Posted by Lavs

Halu! :-) Create a project in vb. Add 1 button on the form, also add a textbox.
The textbox just show you the number of clicked you've done. If the number of clicked is equal to 10, a msgbox will be displayed, and the counter variable A is set back to its initial value which is zero(0).


Dim a As Integer

Private Sub Command1_Click()
a = a + 1
Text1.Text = a
If a = 10 Then
MsgBox "You click the button " & a & " times"
a = 0
Text1.Text = a
End If
End Sub

Private Sub Form_Load()
a = 0
End Sub

Newbie
 
Join Date: Sep 2008
Posts: 1
#17: Sep 7 '08

re: count click


Thanks guys. I am new to this site, but I read some messages in this forum and it helped me complete my assignment. I knew the concepts of counting clicks, I just didn't know how to do it. Thanks! This forum helped a lot!
Reply