VB.NET App: Shot Clock in Basketball Game

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Michelle Monty
    New Member
    • Oct 2007
    • 24

    VB.NET App: Shot Clock in Basketball Game

    Hello all, I'm trying to get a shot clock to work in my basketball game program. I've read a couple of tutorials but I'm still not getting it. I need it to start at 24 and go down to 0. As it is right now, it goes automatically to 0. Can anyone offer some advice? Thanks!

    Code:
    Timer1.Enabled = True
            Label78.Text = 24
            Do Until Label78.Text = 0
                If Label78.Text = 0 Then Timer1.Enabled = False And MessageBox.Show("Matchup Time is Up", "Basketball Mania", MessageBoxButtons.OK, MessageBoxIcon.None, MessageBoxDefaultButton.Button1) Else 
                Label78.Text = Label78.Text - 1
                Timer1.Interval = 24000
            Loop
  • Shashi Sadasivan
    Recognized Expert Top Contributor
    • Aug 2007
    • 1435

    #2
    What you need to look for is an event handler called Timer_Tick.
    Go onto your design view (if the timer is a control placed int he design view) and click its event handlers, and double click the timer_Tick event handler.

    this method which is created will be called every time tyour timer passes its time interval.

    So you wont need that loop anymore, and the rest of the code has to be placed in that event handler

    Comment

    • Michelle Monty
      New Member
      • Oct 2007
      • 24

      #3
      Is that the same as a timer? I tried using the timer but I can't place it on the form.

      Originally posted by Shashi Sadasivan
      What you need to look for is an event handler called Timer_Tick.
      Go onto your design view (if the timer is a control placed int he design view) and click its event handlers, and double click the timer_Tick event handler.

      this method which is created will be called every time tyour timer passes its time interval.

      So you wont need that loop anymore, and the rest of the code has to be placed in that event handler

      Comment

      • Shashi Sadasivan
        Recognized Expert Top Contributor
        • Aug 2007
        • 1435

        #4
        Yes its the same control.
        You can either declare a timer object from the form, or from the pure cs file

        drop the timer control onto the form (a lower bar is then created, with the timer control sitting on it)

        Comment

        • Michelle Monty
          New Member
          • Oct 2007
          • 24

          #5
          Ok, I have the timer on the form and I've set it at 24000 for 24 seconds. Is there any way to get a countdown actually to show when I run the program though? I don't necessary have to show it but I'd like to if there is a way. Thanks for your help!

          Originally posted by Shashi Sadasivan
          Yes its the same control.
          You can either declare a timer object from the form, or from the pure cs file

          drop the timer control onto the form (a lower bar is then created, with the timer control sitting on it)

          Comment

          • sigil
            New Member
            • Dec 2007
            • 4

            #6
            Set the interval on the timer the frequency you want the update to be.
            Then set a TextBox on the display on timer tick.
            When you start the timer store a DateTime dateTime = DateTime.Now.
            Within the timer tick function when (dateTime.AddSe conds(24) < DateTime.Now) you can notify of your ending event

            Comment

            • Michelle Monty
              New Member
              • Oct 2007
              • 24

              #7
              How do you set a textbox to timer tick? I'm looking in the properties box and I don't see that option.

              Originally posted by sigil
              Set the interval on the timer the frequency you want the update to be.
              Then set a TextBox on the display on timer tick.
              When you start the timer store a DateTime dateTime = DateTime.Now.
              Within the timer tick function when (dateTime.AddSe conds(24) < DateTime.Now) you can notify of your ending event

              Comment

              • Shashi Sadasivan
                Recognized Expert Top Contributor
                • Aug 2007
                • 1435

                #8
                Set the timer interval for 1000 (ms)
                in your form class, create a variable called time_elapsed and set it to 24;

                create the time tick event method and within that set the variable time_elapsed = time_elapsed - 1;
                and in the next line
                textBox.text = time_elapsed.To String();

                To check if the time has elapsed, check if time_elapsed == 0;
                if it is so, set the timer's Enabled Property to false so that it stops ticking.

                You cannot program by setting the properties of the control in desgin time.

                Comment

                • Michelle Monty
                  New Member
                  • Oct 2007
                  • 24

                  #9
                  Thanks for your help. I coded it like this:
                  Code:
                  Timer1.Enabled = True
                          Dim time_elapsed As Integer
                          time_elapsed = 24
                          time_elapsed = time_elapsed - 1
                          Label80.Text = time_elapsed.ToString()
                          If time_elapsed = 0 Then Timer1.Enabled = False
                  It's immediately going to 0 though instead of counting down. Sorry for all the questions, I've never used the timer function before. Thank you.



                  Originally posted by Shashi Sadasivan
                  Set the timer interval for 1000 (ms)
                  in your form class, create a variable called time_elapsed and set it to 24;

                  create the time tick event method and within that set the variable time_elapsed = time_elapsed - 1;
                  and in the next line
                  textBox.text = time_elapsed.To String();

                  To check if the time has elapsed, check if time_elapsed == 0;
                  if it is so, set the timer's Enabled Property to false so that it stops ticking.

                  You cannot program by setting the properties of the control in desgin time.

                  Comment

                  • CyberSoftHari
                    Recognized Expert Contributor
                    • Sep 2007
                    • 488

                    #10
                    Originally posted by Michelle Monty
                    Ok, I have the timer ...
                    Set the timer interval to 1000 (Which triggers each seconds)
                    Set a variable and its value to 24 (This is your countdown limit)
                    Enable the timer(enable when you want to start the countdown)
                    Inside Timer_Tick reduce the variable value -1(Start the countdown)
                    Then display it in label or textbox control
                    when variable limit is 0 then set the property enable = false to timer

                    Comment

                    Working...