Created
May 26, 2018 13:52
-
-
Save victortorrescosta/154bc5bb935b8d62b448843317461db9 to your computer and use it in GitHub Desktop.
[C#] Simple form application using timers to dynamically change text on screen
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Drawing; | |
using System.Windows.Forms; | |
namespace Game | |
{ | |
public class Form1 : Form | |
{ | |
// Declare the objects we will use | |
public Button button1; | |
public Label label1; | |
public Label label2; | |
public Label timeLabel; | |
public int timerTimeLeft; | |
public int waitingTime; | |
public Timer timer1; | |
public Timer timer2; | |
public Form1() | |
{ | |
// Define our PLAY button | |
button1 = new Button(); | |
button1.Size = new Size(100, 50); | |
button1.Location = new Point(30, 30); | |
button1.Text = "PLAY"; | |
button1.Click += new EventHandler(button1_Click); | |
// Define our STATUS label, which is constant | |
label1 = new Label(); | |
label1.Size = new Size(100, 30); | |
label1.Location = new Point(50, 100); | |
label1.Text = "STATUS:"; | |
// Define our actual status message, which will change over time | |
label2 = new Label(); | |
label2.Size = new Size(100, 30); | |
label2.Location = new Point(150, 100); | |
label2.Text = "Waiting to start..."; | |
// Define our timer label, which will display the remaining time for each operation | |
timeLabel = new Label(); | |
timeLabel.Size = new Size(100, 30); | |
timeLabel.Location = new Point(150, 150); | |
timeLabel.Text = "---"; | |
// Define the waiting time that we are going to wait before changing the state | |
waitingTime = 5; // Waiting time, in Seconds | |
// Define the first timer, responsible for the state change [Verifying] -> [ConnectionFailed-Attempt2] | |
timer1 = new Timer(); | |
timer1.Interval = (1000); // timer ticks every 1 second | |
timer1.Tick += new EventHandler(timer1_Tick); | |
// Define the second timer, responsible for the state change [ConnectionFailed-Attempt2] -> [ConnectionSuccesfull] | |
timer2 = new Timer(); | |
timer2.Interval = (1000); // timer ticks every 1 second | |
timer2.Tick += new EventHandler(timer2_Tick); | |
// Adding the buttons and labels to the form | |
this.Controls.Add(button1); | |
this.Controls.Add(label1); | |
this.Controls.Add(label2); | |
this.Controls.Add(timeLabel); | |
} | |
private void timer1_Tick(object sender, EventArgs e) | |
{ | |
if (timerTimeLeft > 0) | |
{ | |
// Count down the time at every tick, if timerTimeLeft is more than zero | |
timerTimeLeft = timerTimeLeft - 1; | |
} | |
else | |
{ | |
// When the timerTimeLeft is 0, we stop the timer and make the changes we want | |
timer1.Stop(); | |
label2.ForeColor = Color.Red; | |
label2.Text = "Connection Failed. Attempt 2"; | |
// Now we set a new amount of time to wait (in the variable timerTimeLeft), and start the second timer | |
timerTimeLeft = waitingTime; | |
timer2.Start(); | |
} | |
// Display the new time left by updating the Time Left label. | |
timeLabel.Text = timerTimeLeft + " seconds remaining"; | |
} | |
private void timer2_Tick(object sender, EventArgs e) | |
{ | |
if (timerTimeLeft > 0) | |
{ | |
// Display the new time left by updating the Time Left label. | |
timerTimeLeft = timerTimeLeft - 1; | |
timeLabel.Text = timerTimeLeft + " seconds remaining"; | |
} | |
else | |
{ | |
// When the timerTimeLeft is 0, we stop the timer and make the changes we want | |
timer2.Stop(); | |
label2.ForeColor = Color.Lime; | |
label2.Text = "Connection Succesful!"; | |
// Since we are done, set the timeLabel to show something neutral | |
timeLabel.Text = "---"; | |
} | |
} | |
private void button1_Click(object sender, EventArgs e) | |
{ | |
// When the button is clicked, we show a messagebox to the user with a warning | |
MessageBox.Show("Click OK to start the connection!"); | |
// Then we disable the button | |
button1.Enabled = false; | |
// Change the status label to show the state we are | |
label2.Text = "Verifing..."; | |
// Prepare the timer by setting an amount of time to wait and starting the timer | |
timerTimeLeft = waitingTime; | |
timer1.Start(); | |
} | |
[STAThread] | |
static void Main() | |
{ | |
// Run the application | |
Application.EnableVisualStyles(); | |
Application.Run(new Form1()); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment