Help With timer

need to add a time limit to IQ game.

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 2023 Views - Last Post: 24 April 2007 - 12:09 PM Rate Topic: -----

#1 bmiller234  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 23-April 07

Help With timer

Posted 23 April 2007 - 12:34 PM

I can't seem to find code for this which I thought would be easy.

I need to add a time limit to display in a label that counts down in every form that I have.


For example once you start the game the timer will begin and you will be able to see in the top right corner. Once the user clicks next the time from that form will carry over from the next.

Just some hints on how to do this would be great. I want a 10 Minute time limit.

Public Class question6Form


	Private Sub wordComboBox_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles wordComboBox.SelectedIndexChanged


		wordListBox.Items.Add(wordComboBox.SelectedItem)


	End Sub

	Private Sub nextButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles nextButton.Click


		'data validation
		If wordListBox.Items.Count > 4 Then
			MessageBox.Show("Cannot use more than 4 words", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
		Else
			question7Form.ShowDialog()
		End If


		'checks for alphabetization
		If wordListBox.Items(0) = "Kempthorn" And wordListBox.Items(1) = "Kensington" And wordListBox.Items(2) = "Kentfield" And wordListBox.Items(3) = "Kirkwood" Then
			mainForm.scoreInteger = mainForm.scoreInteger + 20
		Else
			mainForm.scoreInteger = mainForm.scoreInteger - 5
		End If




	End Sub


	Private Sub clearButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles clearButton.Click

		'clears listbox
		wordListBox.Items.Clear()


	End Sub

	Private Sub question6Form_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

		question5Form.Hide()




This is just some code from my 6th question. Just not sure what to use, time or counter and how to go about making it 10 minutes.

Thanks ahead of time if this is really simple which I'm sure it is. I just couldn't find any code snippets or help.

Is This A Good Question/Topic? 0
  • +

Replies To: Help With timer

#2 bmiller234  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 23-April 07

Re: Help With timer

Posted 23 April 2007 - 12:49 PM

Would adding a timer and a counter, and having the timer tick every 1,000 ms add to the Counter +1.

and putting it into a Do Until Counter = 600 which would be 10 minutes?

This post has been edited by bmiller234: 23 April 2007 - 12:52 PM

Was This Post Helpful? 0
  • +
  • -

#3 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Help With timer

Posted 23 April 2007 - 12:53 PM

You hit the nail right on the head. That is exactly what you need to do.
Was This Post Helpful? 0
  • +
  • -

#4 bmiller234  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 23-April 07

Re: Help With timer

Posted 23 April 2007 - 01:14 PM

View Postjayman9, on 23 Apr, 2007 - 12:53 PM, said:

You hit the nail right on the head. That is exactly what you need to do.



Well I have the idea, but just messing it up now I believe haha.

This is the code I'm using and I realized that its backwards and it needs some work. When the program starts I want it to count down from 10 Minutes. 10:00 - 9:59 - 9:58 etc.

I'm just messing around with it now to see if I can just get it to go in seconds and I'll work from there.

	 Private Sub question8Form_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

		question7Form.Hide()

		Dim counter As Integer

		counter = 0
		timerOne.Enabled = True

		Do Until counter = 600
			If timerOne.Interval = 1000 Then
				counter = counter + 1
			End If
		Loop


		timeLabel.Text = counter




Now when I run the program it just goes straight to 600. I'm guessing my code with the timer is off or completely wrong. I haven't worked with timers before and I'm just looking around at things guessing.

Any help to touch this up would be great. As mentioned before I need it to count down from 10:00 eventually.


Thanks again for any help or suggestions.
Was This Post Helpful? 0
  • +
  • -

#5 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Help With timer

Posted 23 April 2007 - 02:24 PM

Make your counter a global variable, you will need it to be able to retain its' value.

You will decrement the counter inside the Tick event of the timer, not inside the Load event of your form. And since you want to count backwards it makes sense that you must set the initial value of counter to 600 and subtract 1 inside the Tick event.

This statement If timerOne.Interval = 1000 Then does nothing but verify that you have set the Interval of the Timer to 1000ms. So it will always evaluate to true.

For display, you can mathematically calculate what the number of minutes and seconds is using division and the Mod operator.

See if that gets you on the right path, if not post whatever problem you are having and we will help you straighten it out.

Keep in mind the Tick event of the timer is where all the code for the timer will exist, including displaying it in the Label, with the exception of the declaration of your counter itself.
Was This Post Helpful? 0
  • +
  • -

#6 bmiller234  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 23-April 07

Re: Help With timer

Posted 23 April 2007 - 02:50 PM

Update:


I've been finishing up most of the questions, and I've decided to add 3 difficulties.

Instead of making an overall timer I'm going to add a timer for each individual question.

On easy difficulty the user will have 15 seconds to answer the question, medium 10, and hard 5.


I'm still having problems understanding the tick event. When I type out for example

timerOne. all that appears is Interval that makes any sense. I've set the tick on the timer properties to 1000ms. So now I know I need to make the counter = counter - 1 for every 1000ms tick. I realize the Irony that I'm making an IQ test and I can't even get this haha.

Never-the-less these are some of my ideas.


  Private Sub timerOne_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles timerOne.Tick

		Do Until counter = 0
			counter = 15
			Do While timerOne.Interval
				counter = counter - 1
			Loop
		Loop

		timeLabel.Text = counter

	End Sub




Since I've changed up how I'm using the timer I'm guessing I won't need to make it Friend and can use Dim for each question.

I'm going to continue to mess with this, and again thanks for any help you can give me.
Was This Post Helpful? 0
  • +
  • -

#7 bmiller234  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 23-April 07

Re: Help With timer

Post icon  Posted 23 April 2007 - 03:28 PM

Still messing with it, and still no countdown.

It has my start time but no decrement. Just stays at 15.


Code:

	Private Sub timerOne_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles timerOne.Tick
		Dim counterInteger

		counterInteger = 15
		timeLabel.Text = "Time Left: " & counterInteger

		If counterInteger = 15 Then
			timerOne.Enabled = True
			counterInteger -= 1
		End If


	End Sub



I've also tried:

	Private Sub timerOne_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles timerOne.Tick
		Dim counterInteger

		counterInteger = 15
		timeLabel.Text = "Time Left: " & counterInteger

		Do Until counterInteger = 0
			timerOne.Enabled = True
			counterInteger = counterInteger - 1
		Loop


	End Sub




And:


   Private Sub timerOne_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles timerOne.Tick
		Dim counterInteger

		counterInteger = 15
		timeLabel.Text = "Time Left: " & counterInteger

		Do Until counterInteger = 0
			timerOne.Enabled = True
			counterInteger -= 1
		Loop


	End Sub




Stuck
Was This Post Helpful? 0
  • +
  • -

#8 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Help With timer

Posted 23 April 2007 - 03:31 PM

Don't mess with the Interval unless you want to change how often the Tick event occurs.

Look at the following example:

Declare counter at Class level.
Public Class Form1

	Dim counter As Integer = 600


Now start the Timer, since I'm not making a game I just started it in the Load event. But depending on when you need to start the timer will determine where to put the following code.
Timer1.Start()
In this example Timer is called Timer1. Now the timer is running.


The first step inside the Tick event is to subtract 1 from your counter. The next step is you need to determine if the counter has reached 0, if it has then you need to stop the timer, because 10 minutes has passed.

Now depending on how you want the display to look. I have an IF statement that checks if the timer is at less than a minute. If it is it will display in the Label as 0:xx, with xx being the number of seconds remaining.

As I mention previously you can use math to determine what number should be displayed in the minute and seconds position.

Simple divide the counter by 60 and subtract 1 to determine how many minutes remain. You will need to convert it to an Integer after the calculation, otherwise you will get a decimal point in the minutes position, hence the CInt type conversion. Now concatenate the minutes with the number of seconds remaining.
To determine the number of seconds you use the Mod operator to divide by 60 and find out what the remainder is, this will be how many seconds remain.

Now here is the completed code that goes inside the Tick event.
	Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick

		'decrement the counter for countdown
		counter = counter - 1
		'check if the counter has reached 0, if it has then stop the timer.
		If counter <= 0 Then
			Timer1.Stop()
		End If
		'determine if minutes still needs to be displayed
		If (counter / 60) >= 1 Then
			'diplay minutes and seconds as #:##
			Me.Label1.Text = CInt(counter / 60 - 1) & ":" & (counter Mod 60)
		Else
			'there are no minutes remaining, only seconds
			'so display as 0:##
			Me.Label1.Text = "0:" & (counter Mod 60)
		End If

	End Sub


You still only need one Timer to handle Hard, Medium, Easy questions. Now is when you will modify the value of counter based on what type of questions the user wants.
Was This Post Helpful? 0
  • +
  • -

#9 bmiller234  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 23-April 07

Re: Help With timer

Posted 23 April 2007 - 03:54 PM

Thanks tons jay


I'm doing each question on easy with 15 seconds now and its working great.

I know what I was doing wrong now, I didn't realize that in the tick event it is going constantly so there is no need to make a loop to make the timer work at the interval.


Again thanks for your help I should have figured that out : /.
Was This Post Helpful? 0
  • +
  • -

#10 bmiller234  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 23-April 07

Re: Help With timer

Posted 23 April 2007 - 05:55 PM

Well I've ran into another problem and this is my fault for not debugging as often as I should.

I debugged the first questions, and now when I went back to do it almost an hour later I have a problem.


Still with the timer.


It is counting down now, and I think I know the problem. I think it is keeping the counterInteger that is left from the previous form.

For example


Public Class mainForm

	Friend scoreInteger As Integer
	Friend counterInteger As Integer




	Private Sub easyButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles easyButton.Click
		counterInteger = 16
		'loads first question
		question1Form.ShowDialog()
		'resets score
		scoreInteger = 0


	End Sub

	
   
	Private Sub mediumButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mediumButton.Click
		counterInteger = 11
		'loads first question
		question1Form.ShowDialog()
		'resets score
		scoreInteger = 0

	End Sub

	Private Sub hardButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles hardButton.Click
		counterInteger = 6
		'loads first question
		question1Form.ShowDialog()
		'resets score
		scoreInteger = 0

	End Sub
End Class



When the user clicks easy 15 second time for each question, medium 10 sec, and hard 5 sec.

and heres the code for the timer:



	Private Sub timerOne_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles timerOne.Tick

		mainForm.counterInteger = mainForm.counterInteger - 1

		question2Form.Visible = False


		If mainForm.counterInteger <= 0 Then
			timerOne.Stop()
			MessageBox.Show("You have ran out of time on this Question and Receive no Points", "Out of Time", MessageBoxButtons.OK, MessageBoxIcon.Stop)
			mainForm.scoreInteger = mainForm.scoreInteger
			question2Form.ShowDialog()
		End If

		If mainForm.counterInteger >= 0 Then
			timeLabel.Text = "Time Left: " & mainForm.counterInteger
		End If



	End Sub



I'm using this same code for all the forms and I'm guessing that counterInteger is not resetting to 16, 11, or 6 depending on what the user presses. The message box just starts popping up really fast after the first 2 forms and crashes the program.

So what should I do to make sure that counterInteger returns to its originally stated amount?

Thanks
Was This Post Helpful? 0
  • +
  • -

#11 bmiller234  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 23-April 07

Re: Help With timer

Posted 23 April 2007 - 06:44 PM

Well I think I found out why the program was crashing.

I didn't stop the timer when the next button was clicked to go onto the next question.


Still I know it will crash if I keep the timer since it is keeping the same counter value from the previous form instead of keeping 16, 11, or 6.


I was thinking of making an If-Then statement for If the user clicked the easyButton Then counterInteger = 16 but it will not let me use mainForm.easyButton.Select or anything like that to use that. I'm really stuck at this point.


I need to have more than one difficulty but I need the counterInteger to stay the constant value that I placed at the beginning.


Sorry again for the numerous posts, but the Timer works I just need counterInteger to remain 16 when the easy button is clicked, 11 for medium, and 6 for hard.


Thanks for any help you can give me.
Was This Post Helpful? 0
  • +
  • -

#12 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Help With timer

Posted 23 April 2007 - 08:52 PM

Can you zip up your project and upload to this topic as an attachment?

I'm not sure I understand the problem completely. This way I can see exactly what is going on.
Was This Post Helpful? 0
  • +
  • -

#13 bmiller234  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 23-April 07

Re: Help With timer

Posted 24 April 2007 - 09:48 AM

You can tell when you watch the counter in the top right corner that it is not resetting.


counterInteger is not going back to the designated value in the easy, medium, and hard buttons. Which is understandable its declared friend and the value can be changed. But constant variables cannot be used throughout the entire project right?

And if they did I would have to have someway to determine what button the user selected and then have that counter used for every question in an If-Then statement I'm guessing. I tried doing that but there is no way to say

If mainForm.easyButton.?selected?,?clicked?, Then
easyCounterInteger (which will be a constant) = 16
End If


Sorry if this is confusing but I think you will be able to tell once you see it happening.

Here is the code for the mainForm with the easy, medium, and hard buttons. Also the code for the next question.

Public Class mainForm

	Friend scoreInteger As Integer
	Friend counterInteger As Integer




	Private Sub easyButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles easyButton.Click
		counterInteger = 16
		'loads first question
		question1Form.ShowDialog()
		'resets score
		scoreInteger = 0


	End Sub

	
   
	Private Sub mediumButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mediumButton.Click
		counterInteger = 11
		'loads first question
		question1Form.ShowDialog()
		'resets score
		scoreInteger = 0

	End Sub

	Private Sub hardButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles hardButton.Click
		counterInteger = 6
		'loads first question
		question1Form.ShowDialog()
		'resets score
		scoreInteger = 0

	End Sub
End Class




Next Form:

Public Class question1Form



	Private Sub question1Form_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load


		mainForm.Hide()


		'starts timer for question
		timerOne.Start()

		'shows score
		scoreLabel.Text = "Score: " & mainForm.scoreInteger





	End Sub

	Private Sub nextButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles nextButton.Click

		'stops timer
		timerOne.Stop()


		'data validation
		If aRadioButton.Checked = False And bRadioButton.Checked = False _
		And cRadioButton.Checked = False And dRadioButton.Checked = False _
		And eRadioButton.Checked = False And fRadioButton.Checked = False Then
			MessageBox.Show("Please select an answer before clicking Next", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

		End If



		'adds 20 if correct subtracts 5 if incorrect
		If aRadioButton.Checked = True Then
			mainForm.scoreInteger = mainForm.scoreInteger + 20
		Else
			mainForm.scoreInteger = mainForm.scoreInteger - 5
		End If

		'continues to next question
		question2Form.ShowDialog()



	End Sub

	Private Sub NewGameToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewGameToolStripMenuItem.Click
		'starts new game
		If NewGameToolStripMenuItem.Checked Then
			mainForm.ShowDialog()
			Me.Hide()
		End If
	End Sub


	Private Sub timerOne_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles timerOne.Tick

		mainForm.counterInteger = mainForm.counterInteger - 1

		question2Form.Visible = False


		If mainForm.counterInteger <= 0 Then
			timerOne.Stop()
			MessageBox.Show("You have ran out of time on this Question and Receive no Points", "Out of Time", MessageBoxButtons.OK, MessageBoxIcon.Stop)
			mainForm.scoreInteger = mainForm.scoreInteger
			question2Form.ShowDialog()
		End If

		If mainForm.counterInteger >= 0 Then
			timeLabel.Text = "Time Left: " & mainForm.counterInteger
		End If



	End Sub
End Class




Once the next button is clicked the next Question will show and heres the code for that:

Public Class question2Form


	Private Sub nextButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles nextButton.Click

		'data validation
		If radioButton1.Checked = False And radioButton2.Checked = False _
		And radioButton3.Checked = False And radioButton4.Checked = False Then
			MessageBox.Show("Please select an answer before clicking Next", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

		End If

		'increases or decreases score
		If radioButton3.Checked = True Then
			mainForm.scoreInteger = mainForm.scoreInteger + 20
		Else
			mainForm.scoreInteger = mainForm.scoreInteger - 5
		End If

		question3Form.ShowDialog()


	End Sub

	Private Sub NewGameToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewGameToolStripMenuItem.Click
		'starts new game

		mainForm.ShowDialog()




	End Sub

	Private Sub question2Form_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

		'starts timer
		timerOne.Start()

		'displays score 
		scoreLabel.Text = "Score: " & mainForm.scoreInteger

		'hides previous form
		question1Form.Hide()

	End Sub


	Private Sub timerOne_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles timerOne.Tick
		'decrements counter to start
		mainForm.counterInteger = mainForm.counterInteger - 1

		'stops timer once time runs out and no score is given
		If mainForm.counterInteger <= 0 Then
			timerOne.Stop()
			MessageBox.Show("You have ran out of time on this Question and Receive no Points", "Out of Time", MessageBoxButtons.OK, MessageBoxIcon.Stop)
			mainForm.scoreInteger = mainForm.scoreInteger
			question3Form.ShowDialog()
		End If

		'displays the time while it is left
		If mainForm.counterInteger >= 0 Then
			timeLabel.Text = "Time Left: " & mainForm.counterInteger
		End If


	End Sub

	Private Sub stopButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles stopButton.Click

'added to see what counterInteger's value was.


		' timerOne.Stop()

		' integerLabel.Text = mainForm.counterInteger

	End Sub
End Class

Was This Post Helpful? 0
  • +
  • -

#14 bmiller234  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 23-April 07

Re: Help With timer

Posted 24 April 2007 - 09:57 AM

Heres the File. Sorry for double Post been up all night.

Attached File(s)

  • Attached File  IQ.zip (669.93K)
    Number of downloads: 65

Was This Post Helpful? 0
  • +
  • -

#15 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Help With timer

Posted 24 April 2007 - 11:22 AM

You have got some serious problems with your project and I'm not talking about the code.

For whatever reason, that I cannot determine, no matter if I delete all the code on all the forms, it continues to run on a old build. That is why you are having so many problems with it crashing. It is never recompiling the new or changed code.

I literally deleted all the code on each of the forms. And it still runs in the same manner, which without any code, should not run at all.

I even deleted all the build files I could find, which just creates an error when compiling because it is trying to find the old build and copy to the Debug folder.

At this point I would suggest creating a new project from scratch. I know that sucks considering the time you've spent, but believe me I have had to do it before, too.

I'm sorry I wish I could provide a solution, but I do not see one here.

When you build the new project, keep in mind you need to Close each of the forms when you are done with them. Don't just Hide the form, because it is still running when it is hidden and so is the Timer. This is why you get several Out of Time messages and eventually it crashes.

Also rather than reusing the counterInteger from the main form on each form, create an new variable on each form and just copy the value stored in the Main forms counterInteger into the local variable on each form. This will save you a whole lot of trouble and keep the scope of the variable to only the instance of each form.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2