0

I need to write a Windows Application in C# but I don't know how to do some things. I'm very fresh in C# Programming.

What should do my app ?

  • First step : I need to do from my radiobuttons or from something else colorful icons, these icon should random light up and I must to power off this with corresponding buttons (for exaple red radiobutton = red button).
  • Second step : There is a timer, which start counting my reflex from lit button to power off this button.

What I've done ?

  • Implemented radio buttons, button and two labels

  • Responsible for every component clicked empty methods

Which program I'm using ?

  • Visual Studio 2008 Express

What I don't know how to do ?

  • How to implement timer and set counting to check my reflex

  • How to set radio button automatically checked in random order and turn this off with other button

  • How to show in label my timer

My source code :

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Ling;
using System.Text;
using System.Windows.Forms;

namespace App{
public partial class Form1 : Form{ 
public Form1(){
InitializeComponent();
}
private void label1Click(object sender, EventArgs e){
}
private void radioButton1_CheckedChanged(object sender, EventArgs e){
}

private void radioButton2_CheckedChanged(object sender, EventArgs e){
}

private void radioButton3_CheckedChanged(object sender, EventArgs e){
}

private void GreenButton_Click(object sender, EventArgs e){
GreenButton.Backcolor = Color.gray;
}
private void RedButton_Click(object sender, EventArgs e){
RedButton.Backcolor = Color.gray;
}

private void BlueButton_Click(object sender, EventArgs e){
BlueButton.Backcolor = Color.gray;
}
private void label2Click(object sender, EventArgs e){

}

I will be very thankful for every kind of help <3

Julian Mann
  • 6,256
  • 5
  • 31
  • 43

2 Answers2

2

Instead of a System.Windows.Forms.Timer, you should use a System.Diagnostics.Stopwatch to measure the reflex time.

Just create a stopwatch at class level:

private Stopwatch stopwatch = new Stopwatch();

After you light up the radio button, you start the stopwatch:

stopwatch.Start();

When the correct button is clicked by the user, you stop the stopwatch and get the elapsed time:

stopwatch.Stop();
var elapsedTime = stopwatch.ElapsedMilliseconds;

Also, you should uncheck all radio buttons here:

radioButton1.Checked = false;
radioButton2.Checked = false;
radioButton3.Checked = false;

Then you can display this variable to the user by doing something like this:

<insert your label's name here>.Text = "Your time: " + elapsedTime;

I have already answered the first and third of your difficulties. For the second one, here's a simple approach.

First we need to decide when to light up a radio button. It can be light up after some delay after the user clicks on a start button. To create this delay, use System.Windows.Forms.Timer. You can learn how to use it by going to here.

Now create a Random object at class level. This is used to randomly light up the radio buttons

private Random random = new Random();

In the Timer.Tick event handler, first stop the timer because we only want it to fire once. Then you can first generate a random number between 0 and 2:

var randomNumber = random.Next(0, 3);

Then depending on the random number, we light up radio buttons:

if (randomNumber == 0) {
    radioButton1.Checked = true;
} else if (randomNumber == 1) {
    radioButton2.Checked = true;
} else {
    radioButton3.Checked = true;
}
// this is where you want to start the stopwatch!
stopwatch.Start();
Graham
  • 7,431
  • 18
  • 59
  • 84
Sweeper
  • 213,210
  • 22
  • 193
  • 313
0
  1. Log the time when the light turns on and log the time when you clicked to turn it of, using DateTime.Now. Compare those to times to get the time passed.
  2. I understand your question as "How do I get random numbers?". There is plenty of answers on the Internet on that. Here's one: How do I generate a random int number in C#? Also, have a look at the Timer.Tick event.
  3. Use the Text-property of your label to show the time passed (see #1). Convert.ToString() might come in handy for you.
Community
  • 1
  • 1
Björn Larsson
  • 317
  • 1
  • 5
  • 19