Typing program

C# code

Step 2: Writing C# code

The C# code for typing program is shown below. You may need to read the explanation text in comments to understand what to be accomplished by the pieces of code.

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

namespace TypingCrazy
{
public partial class Typingform : Form
{

//Declaring and initializing variables
private int next = -1;
private Button t;
private int se;
private int correct;
private int incorrect;
public Typingform()
{
InitializeComponent();
}


//This method is to handle the textBox1 key press event.
//It accepts the key typed by the user and checks it to see whether it is the correct key or not.
private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{

char key = e.KeyChar;
char[] c = txtbox.Text.ToCharArray();
next++;
//check number keys and letters keys
//highlight character and button
for (int i = 0; i < this.panel1.Controls.Count; i++)
{
if (this.panel1.Controls[i].Name.Substring(0, 4).ToLower().CompareTo("bnt" + key.ToString().ToLower()) == 0 && this.panel1.Controls[i].Name.Length<=4)
highlight((Button)this.panel1.Controls[i],c, next, key);

}

//check symbols: ',~!, @ #, $ %,...

if ((int)key == 33) highlight(bnt1, c, next, key);
if ((int)key == 64) highlight(bnt2, c, next, key);
if ((int)key == 35) highlight(bnt3, c, next, key);
if ((int)key == 36) highlight(bnt4, c, next, key);
if ((int)key == 37) highlight(bnt5, c, next, key);
if ((int)key == 94) highlight(bnt6, c, next, key);
if ((int)key == 38) highlight(bnt7, c, next, key);
if ((int)key == 42) highlight(bnt8, c, next, key);
if ((int)key == 40) highlight(bnt9, c, next, key);
if ((int)key == 41) highlight(bnt0, c, next, key);


if ((int)key == 126 || (int)key == 96) highlight(bntaccent, c, next, key);

if ((int)key == 123 || (int)key == 91) highlight(bntopenbrace, c, next, key);
if ((int)key == 125 || (int)key == 93) highlight(bntclosebrace, c, next, key);
if ((int)key == 58 || (int)key == 59) highlight(bntsemi, c, next, key);
if ((int)key == 34 || (int)key == 39) highlight(bntquote, c, next, key);
if ((int)key == 60 || (int)key == 44) highlight(bntcomma, c, next, key);
if ((int)key == 62 || (int)key == 46) highlight(bntpoint, c, next, key);
if ((int)key == 63 || (int)key == 47) highlight(bntbackslash, c, next, key);
if ((int)key == 124 || (int)key == 92) highlight(bntforwardslash, c, next, key);
if ((int)key == 95 || (int)key ==45) highlight(bntminus, c, next, key);
if ((int)key == 43 || (int)key == 61) highlight(bntequal, c, next, key);

//check spacebar and enter keys
if ((int)key == 32) highlight(bntspacebar, c, next, key);
if ((int)key == 13) highlight(bntenter, c, next, key);

//stop timer when the number of keys pressed equal to the number of letters displayed in the box
if (c.Length == next + 1)
{
timer1.Stop();
txtcorrect.Text = correct.ToString();
txtincorrect.Text = incorrect.ToString();
}


}


//This method is to handle the form load event.
private void Form1_Load(object sender, EventArgs e)
{ //initialize controls and variables
txtbox.BackColor = Color.LightSkyBlue;
textBox1.TabIndex = 0;
se = 0;
correct = 0;
incorrect = 0;
//load text
txtbox.Text = loadtext("lesson1.txt");
//start timer and set its interval to 1000 milliseconds(1 second)
timer1.Interval = 1000;
timer1.Start();


}

//This method is to handle the Exit menu item click event

private void exitToolStripMenuItem_Click(object sender, EventArgs e)
{
Close();

}



//This method is to select and highlight character based on the key pressed
private void highlight(Button con,char[] c, int next, char key)
{

//set the color of current highlighted button to the normal color(white) when the new key is pressed
try
{
t.BackColor = Color.White;
}catch (Exception e) { }
//...............................................................................
try
{

if ((int)key == (int)c[next])
{

//correct key pressed will be in green color
con.BackColor = Color.Green;
txtbox.SelectionStart = next;
txtbox.SelectionLength = 1;
txtbox.SelectionColor = Color.Green;
//increase the number of correct keys pressed
correct++;
//record the current highlighted button
t = con;
}
//Enter key is pressed
else if ((int)key == 13 && (int)c[next] == 10)
{

bntenter.BackColor = Color.Green;
t = bntenter;
correct++;
}
else
{
//incorrect key pressed will be in red color
con.BackColor = Color.Red;
txtbox.SelectionStart = next;
txtbox.SelectionLength = 1;
txtbox.SelectionColor = Color.Red;
//increase the number of correct keys pressed
incorrect++;
//record the current highlighted button
t = con;
}

}
catch (Exception e) { }


}

//The user pressed reload button
private void bntreload_Click(object sender, EventArgs e)
{
reload("lesson1.txt");

}

//This method is to handle the Minimize menu item click event
private void minimizeToolStripMenuItem_Click(object sender, EventArgs e)
{
//minimize the form when the user click the Minimize menu item
this.WindowState = FormWindowState.Minimized;
}

// This method is to handle the timer event
private void timer1_Tick(object sender, EventArgs e)
{
//increase the number of seconds and show it
se += 1;
txttime.Text = se.ToString() ;

}

//This method is to read text of lesson from the text file based on file name provided
private string loadtext(string filename)
{
FileStream fs;
StreamReader fr;
//create file stream object
fs = new FileStream(filename , FileMode.Open, FileAccess.Read);
//create reader objec
fr = new StreamReader(fs);
string content = fr.ReadToEnd();

//close FileStream object
fs.Close();
return content;

}

//This method is to handle Lesson 2 button click event
private void bntlesson2_Click(object sender, EventArgs e)
{
//read the text of lesson 2
reload("lesson2.txt");
}

//This method is to handle Lesson1 button click event
private void bntlesson1_Click(object sender, EventArgs e)
{
//read the text of lesson 1
reload("lesson1.txt");
}

//This method is to handle Reload button click event
private void reload(string filename)
{
//set the color of current highlighted button to the normal color(white)
try
{
t.BackColor = Color.White;
}
catch (Exception ex) { }
//reset the variables and controls
next = -1;
se = 0;
correct = 0;
incorrect = 0;
txtincorrect.Text = "";
txtcorrect.Text = "";
timer1.Start();
textBox1.Focus();
txtbox.Clear();
txtbox.Text = loadtext(filename );
}



}
}





Comments




This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.