I dont know if it is the easiest or best way but, what i though, was to add a while loop, what I set to 30 and dercrmenting at every ticks (setting the timer at 1 sec). Here is my code without that feature. ( I know i could had use Switch for some of the if but, i am not there yet in the book. The countdown is not part of the book, it's just an idea i got)
using
System;using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace
Math_Game{
{
Random randomNo = new Random();
int no1, no2;
string problemType = "+"; public Form1()
{
InitializeComponent();
} private void StartGame_Click(object sender, EventArgs e)
{
timerReset();
StartGame.Visible = false;
CheckAnswer.Visible = true;
Question.Visible = true;
Answer.Visible = true;
NextQuestion.Visible = true;
Feedback.Visible = true;
Gen_Question();
} private void Gen_Question()
{
no1 = randomNo.Next(100);
no2 = randomNo.Next(100);
if (radioButton1.Checked)
{
problemType = "+";
} else if (radioButton2.Checked)
{
problemType = "-";
} else if (radioButton3.Checked)
{
problemType = "*";
}
Question.Text = no1 + problemType + no2 +
"=";} private void CheckAnswer_Click(object sender, EventArgs e)
{
timer1.Stop;
{
result = no1 + no2;
} else if (problemType == "-")
{
result = no1 - no2;
} else if (problemType == "*")
{
result = no1 * no2;
} if (Answer.Text == result.ToString())
{
Feedback.Text = "Correct";
} else
{
Feedback.Text = "Incorrect";
}
} private void NextQuestion_Click(object sender, EventArgs e)
{
Gen_Question();
timerReset();
Answer.Visible =
Answer.Text = "";
Feedback.Text = "";
} private void timer1_Tick(object sender, EventArgs e)
{
Answer.Visible =
Feedback.Text = "Trop tard";
timer1.Stop;
} private void timerReset()
{
timer1.Interval = 30000;
timer1.Start();
}
}
}
Posted Jun 9, 2007, 12:33 AM
My advice to you is to push yourself, say "I wonder if I can make it do this?" and try it, find ways to do it. Find creative work arounds and use different methods to do things. Itll really help you with it, it helped me alot!
Christian ParentPosted Jun 8, 2007, 11:03 PM
I want to learn it with as little help as possible tho.
I am doing that only as an hobby.
My goal is to create some apps to help my kids in their studies and also, create some little rpg starting with a text one for me and my son :)
Then i will look into a tile based one and maybe, one day, one with nice graphics. But i might be in my 60's by that time lol !!
Posted Jun 8, 2007, 9:26 PM
Christian ParentPosted Jun 8, 2007, 9:18 PM
Only thing, since that i had to click on a button to start the test or to get the next question, it was loosing focus so, i have inserted it in both button events.
Posted Jun 8, 2007, 8:49 PM
answerTextBox.Focus();
answerTextBox.SelectAll();
Now when you start the form, it should focus on the answer text box.
Christian ParentPosted Jun 8, 2007, 8:39 PM
Now another question, when i start it, the texbox with the countdown get the focus. How can i make it that the cursor starts in the answer textbox ?
Posted Jun 8, 2007, 7:33 PM
public partial class Main : Form
internal Int16 i = 30;
private void timer1_Tick(object sender, EventArgs e)
{
if(i != 0)
{
Answer.Visible = false;
Feedback.Text = "Trop tard";
timerBox.Text = Convert.ToString(i);
i--;
}
else if(i == 0)
{
timer1.Enabled = false;
i = 30;
//Display whatever you want to when the timer runs out
}
}
Mike GoldPosted Jun 8, 2007, 7:28 PM
you were close, I think if you just removed the while loop and reset the count, you were good to go.
Posted Jun 8, 2007, 7:24 PM
Actually I remember that Mike, while WOULD hang up the program. I dont have a compiler here with me at work, so I cant test this code out but Im positive Ive done it using a good ol' fashioned "tick". that was in VB though... Now I wanna see if I can remember how to do it. When I get home, C# Express, here I come!
Posted Jun 8, 2007, 7:22 PM
Try this out:
/*Put this variable at the top of your class right after the namespace declaration
I put the variable "i" here because if you put it in the timer1_tick void, it
will re-instate itself to 30 when the timer intervals.*/
internal Int16 i = 30;
private void timer1_Tick(object sender, EventArgs e)
{
/*For each tick of the timer, evaluate i and make sure it does not equal 0
If it equals 0, time has run out (obviously :P ) and the "if" statement will
pick it up and execute the code you want to execute when the timer is out.*/
while(i != 0)
{
Answer.Visible = false;
Feedback.Text = "Trop tard";
timerBox.Text = Convert.ToString(i);
i--;
if(i = 0)
{
timer1.Enabled = false;
//Any other things you want to happen or display when the time runs out
}
}
}
Keep in mind that I have not tested this code out in an actual compiler so you might have to tune it a bit. But its the general idea. Good luck!
Mike GoldPosted Jun 8, 2007, 7:19 PM
// change to 1 second interval
timer.Interval = 1000;
private void timer1_Tick(object sender, EventArgs e)
{
textBox.Text = _count.ToString();
_count--;
if (_count == 0)
{
_count = 30;
Answer.Visible = false;
Feedback.Text = "Trop tard";
timer.Stop();
}
}