By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,791 Members | 1,314 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,791 IT Pros & Developers. It's quick & easy.

count click

P: 11
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?
Mar 28 '07 #1
Share this Question
Share on Google+
16 Replies


100+
P: 149
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
Mar 28 '07 #2

P: 20
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
Mar 28 '07 #3

P: 16
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
Mar 28 '07 #4

P: 11
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
Mar 28 '07 #5

SammyB
Expert 100+
P: 807
>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?
Mar 28 '07 #6

Expert 5K+
P: 8,434
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
Mar 29 '07 #7

P: 11
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.
Apr 4 '07 #8

SammyB
Expert 100+
P: 807
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
Apr 4 '07 #9

vijaydiwakar
100+
P: 579
But, does it work? :D --Sam
:D nice Jo
Apr 4 '07 #10

P: 11
But, does it work? :D --Sam
sorry mate have not tried it yet had a lot on. i try it 2day.
Apr 11 '07 #11

P: 11
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.
Apr 11 '07 #12

SammyB
Expert 100+
P: 807
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.
Apr 11 '07 #13

Expert 5K+
P: 8,434
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?
Apr 11 '07 #14

SammyB
Expert 100+
P: 807
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.
Apr 16 '07 #15

P: 10
Exactly what i was looking for. Thanks dude.


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
May 3 '07 #16

P: 1
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!
Sep 7 '08 #17

Post your reply

Sign in to post your reply or Sign up for a free account.