Advance Text Reader
In this tutorial we will learn to create Advance Text Reader with some advanced features, Like auto scroll, displaying text while reading and some basic features to play, pause, resume, stop.
This project is divided into 2-parts in the 1st-part we will be designing a user interface in which there will be some controls for the users to operate Advance Text Reader and after that we will move to the 2nd part in which we will write the code for all the controls that we have designed ,so just follow these steps one by one as following to make it happens. So let's start.
Part-1 -Creating user Interface.
Step-1-Start a new project in Visual Studio > choose Visual C# then click on
Windows Desktop then select WindowsForm App(.Net Framework) and name it as Advance Text Reader then press ok.
The new project will be created now go to the Form1 properties Section and set the size as following.
Size-700,500
Text-Advance Text Reader
Now Add 1 panel from the toolbox and set the properties as follows.
Anchor-Top, Left, Right
BackColor-64,64,64
Now Add 5-buttons from the toolbox and then go to the properties of each button one by one and set the properties as follows.
Once it will be done then arrange the buttons as follows.
I have already stored these images in my Computer to add, But don’t worry you can download your favorite images from the internet, so now what we have to do is we have to go to the properties of each button one by one and click on Background Image then go to the project Resource file then import images as following.
Now go to Toolbox and add 1 label, then go to the properties section and set the properties as follows.
Well, we are done with the 1st part now we will move to the coding part.
Part-2-Coding part
Step-1-Adding Refrence.
First, go to the top menu bar and click on the project then go to Add Refrence.. then select System. Speech and press ok.
Now go to the form1 code view and add copy these namespaces as following.
using System;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.IO;
using System.Speech;
using System.Speech.Synthesis;
Now go to the form1 class section and copy the red highlighted code as following.
public partial class Form1:Form
{
SpeechSynthesizer reader = new SpeechSynthesizer();
{
SpeechSynthesizer reader = new SpeechSynthesizer();
public Form1()
{
InitializeComponent();
}
Step-2-To open a text file.
Go to to the form 1 design view and double click on button1 which name is btn_open.
Now copy the red highlighted code as following.
private void btn_open_Click(object sender, EventArgs e)
{
Stream str;
lbl_txt.Text = "";
OpenFileDialog openfiledialog = new OpenFileDialog();
if (openfiledialog.ShowDialog() ==
System.Windows.Forms.DialogResult.OK)
{
if ((str = openfiledialog.OpenFile()) != null)
{
string fname = openfiledialog.FileName;
string filetxt = File.ReadAllText(fname);
lbl_txt.Text = filetxt;
}
}
}
Output
Step-3-To Read Text file.
private void btn_speak_Click(object sender, EventArgs e)
{
reader.SpeakAsync(lbl_txt.Text);
}
Now run the program and then click on the speak button to check its working.
Output
Step-4-To pause text file while reading.
Go to to the form 1 design view and double click on button3 which name is btn_pause .
Now copy the red highlighted code as following.
private void btn_pause_Click(object sender, EventArgs e)
{
if (reader != null)
{
if (reader.State == SynthesizerState.Speaking)
{
reader.Pause();
}
}
Step-5-To Resume text file while pausing mode.
Go to to the form 1 design view and double click on button4 which name is btn_resume .
Now copy the red highlighted code as following.
private void btn_resume_Click(object sender, EventArgs e)
{
if (reader != null)
{
if (reader.State == SynthesizerState.Paused)
{
reader.Resume();
}
}
}
Step-6-To Stop text file while reading mode.
Go to to the form 1 design view and double click on button5 which name is btn_stop.
Now copy the red highlighted code as following.
private void btn_stop_Click(object sender, EventArgs e)
{
if (reader != null)
{
reader.Dispose();
}
}
Now Run the program and check all the buttons are working correctly or not.
Step-7-Creating Auto scroll function.
To create auto-scroll function we need one component from the toolbox that is a timer which will play the important role for the auto scroll on the user screen, so add 1-timer from the toolbox and set the properties as follows.
Now we will declare the height for scrolling label to the top of the screen which name is lbl_txt.
So go the public Form1 Class and copy the red highlighted code as following.
public partial class Form1: Form
{
public Form1()
{
InitializeComponent();
lbl_txt.Top = 80;
}
At the bottom of the form 1 design view double click the timer1 and copy the red highlighted code as following.
private void timer1_Tick(object sender, EventArgs e)
{
lbl_txt.Top -= 1;
//To change the text color whenever timer starts//
lbl_txt.ForeColor = Color.Yellow;
}
Now to work this code we need to start the timer so for that go to the btn_speak click event and copy the red highlighted code as following.
private void btn_speak_Click(object sender, EventArgs e)
{
timer1.Start();
reader.SpeakAsync(lbl_txt.Text);
}
Now Run the program and check its working fine or not, apart from this if you want to increase or decrease the speed of auto-scroll you can simply change the timer1 interval value as per your requirement. Similarly, go to the btn_pause click event and copy the red highlighted code as following.
private void btn_pause_Click(object sender, EventArgs e)
{
if (reader != null)
{
if (reader.State == SynthesizerState.Speaking)
{
reader.Pause();
timer1.Stop();
}
}
}
Similarly, go to the btn_resume click event and copy the red highlighted code as following.
private void btn_resume_Click(object sender, EventArgs e)
{
if (reader != null)
{
if (reader.State == SynthesizerState.Paused)
{
reader.Resume();
timer1.Start();
}
}
}
Similarly, go to the btn_stop click event and copy the red highlighted code as following.
private void btn_stop_Click(object sender, EventArgs e)
{
if (reader != null)
{
reader.Dispose();
timer1.Stop();
}
}
Step-8- Displaying text while reading.
To display text while reading we need one more timer which will handle this task so add timer2 from the toolbox and set the properties as follows.
Now we will declare the text length, so go to the Form1 class section and copy the red highlighted code as following.
public Form1()
{
InitializeComponent();
}
int len = 0;
int counter = 0;
At the bottom of the form 1 design view double click on timer2 and copy the red highlighted code as following.
private void timer2_Tick_1(object sender, EventArgs e)
{
counter++;
if (counter > len)
{
counter = 0;
lbl_txt.Text = txt;
lbl_txt.Top = 50;
timer1.Stop();
timer2.Stop();
}
else
{
lbl_txt.Text = txt.Substring(0, counter);
}
}
Now to work this code we need to start the timer first so for that go to the btn_speak click event and copy the red highlighted code as following.
private void btn_speak_Click(object sender, EventArgs e)
{
txt = lbl_txt.Text;
len = txt.Length;
timer1.Start();
timer2.Start();
reader.SpeakAsync(lbl_txt.Text);
}
Similarly, go to the btn_pause click event and copy the red highlighted code as following.
private void btn_pause_Click(object sender, EventArgs e)
{
if (reader != null)
{
if (reader.State == SynthesizerState.Speaking)
{
reader.Pause();
timer1.Stop();
timer2.Stop();
}
}
}
Similarly, go to the btn_resume click event and copy the red highlighted code as following.
private void btn_resume_Click(object sender, EventArgs e)
{
if (reader != null)
{
if (reader.State == SynthesizerState.Paused)
{
reader.Resume();
timer1.Start();
timer2.Start();
}
}
}
Similarly, go to the btn_stop click event and copy the red highlighted code as following.
private void btn_stop_Click(object sender, EventArgs e)
{
if (reader != null)
{
reader.Dispose();
timer1.Stop();
timer2.Stop();
}
}
Finally, we are done now, go through of all the source code ones and then Run the program and check it working correctly or not if any errors occur then recheck it Full source is given below.
Full Source Code
using System;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.IO;
using System.Speech;
using System.Speech.Synthesis;
namespace Text_Reader_By_Rohit_Programming_Zone
{
public partial class Form1: Form
{
SpeechSynthesizer reader = new SpeechSynthesizer();
public Form1()
{
InitializeComponent();
lbl_txt.Top = 80;
}
int len = 0;
int counter = 0;
string txt;
private void Form1_Load(object sender, EventArgs e)
{
}
private void btn_open_Click(object sender, EventArgs e)
{
Stream str;
lbl_txt.Text = "";
OpenFileDialog openfiledialog = new OpenFileDialog();
if (openfiledialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
if ((str = openfiledialog.OpenFile()) != null)
{
string fname = openfiledialog.FileName;
string filetxt = File.ReadAllText(fname);
lbl_txt.Text = filetxt;
}
}
}
private void btn_speak_Click(object sender, EventArgs e)
{
txt = lbl_txt.Text;
len = txt.Length;
timer1.Start();
timer2.Start();
reader.SpeakAsync(lbl_txt.Text);
}
private void btn_pause_Click(object sender, EventArgs e)
{
if (reader != null)
{
if (reader.State == SynthesizerState.Speaking)
{
reader.Pause();
timer1.Stop();
timer2.Stop();
}
}
}
private void btn_resume_Click(object sender, EventArgs e)
{
if (reader != null)
{
if (reader.State == SynthesizerState.Paused)
{
reader.Resume();
timer1.Start();
timer2.Start();
}
}
}
private void btn_stop_Click(object sender, EventArgs e)
{
if (reader != null)
{
reader.Dispose();
timer1.Stop();
timer2.Stop();
lbl_txt.Text = "";
}
}
private void timer1_Tick(object sender, EventArgs e)
{
lbl_txt.ForeColor = Color.Yellow;
lbl_txt.Top -= 1;
}
private void timer2_Tick_1(object sender, EventArgs e)
{
counter++;
if (counter > len)
{
counter = 0;
lbl_txt.Text = txt;
lbl_txt.Top = 50;
timer1.Stop();
timer2.Stop();
}
else
{
lbl_txt.Text = txt.Substring(0, counter);
}
}
}
}