count click

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • spud379
    New Member
    • Mar 2007
    • 11

    count click

    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?
  • ansumansahu
    New Member
    • Mar 2007
    • 149

    #2
    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

    Comment

    • ChillUmesh
      New Member
      • Feb 2007
      • 20

      #3
      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"&finalscor e)
      else
      count=count+1
      end if

      Comment

      • Lavs
        New Member
        • Mar 2007
        • 16

        #4
        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

        Comment

        • spud379
          New Member
          • Mar 2007
          • 11

          #5
          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

          Comment

          • SammyB
            Recognized Expert Contributor
            • Mar 2007
            • 807

            #6
            >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?

            Comment

            • Killer42
              Recognized Expert Expert
              • Oct 2006
              • 8429

              #7
              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

              Comment

              • spud379
                New Member
                • Mar 2007
                • 11

                #8
                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.

                Comment

                • SammyB
                  Recognized Expert Contributor
                  • Mar 2007
                  • 807

                  #9
                  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

                  Comment

                  • vijaydiwakar
                    Contributor
                    • Feb 2007
                    • 579

                    #10
                    Originally posted by SammyB
                    But, does it work? :D --Sam
                    :D nice Jo

                    Comment

                    • spud379
                      New Member
                      • Mar 2007
                      • 11

                      #11
                      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.

                      Comment

                      • spud379
                        New Member
                        • Mar 2007
                        • 11

                        #12
                        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.

                        Comment

                        • SammyB
                          Recognized Expert Contributor
                          • Mar 2007
                          • 807

                          #13
                          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.

                          Comment

                          • Killer42
                            Recognized Expert Expert
                            • Oct 2006
                            • 8429

                            #14
                            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?

                            Comment

                            • SammyB
                              Recognized Expert Contributor
                              • Mar 2007
                              • 807

                              #15
                              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.

                              Comment

                              Working...