Designing the Form

Text to Speech: For C# and VB NET Students


Create a new Windows Forms project in either Visual Basic .Net or C#. Call it anything you like.

The form we'll design for this project looks like this:

A Windows Form for a Text to Speech program

When you click the dropdown box at the top, you'll see a list of Voices on the system:

ComboBox with a list of Speech Voices

Select a voice from the list and then click Speak. The program will say whatever is in the text box at the bottom. There are buttons to pause, resume and stop. There is also a button where you can open a file. The opened file (DOCX, PDF, TXT) will appear in the text box in place of the default text. You can change the speed of the voice and the volume by adjusting the sliders.

Notice the button at the bottom, under the text box. In our Program, we'll give the user the option to highlight any word. When the bottom button is clicked, just this word will be pronounced (or words).

So, resize your form. We made ours 700 wide by 720 high. Add a button and a combo box near the top. Rename the button to BtnSpeech. Our button has the size 90 by 40. Set the Text property to Speak.

Rename the combo box to cmbVoice. The height of a combo box is changed by setting the font. We changed it to 20 point bold. The width of our combo box was 440. Set the Text property to Select Voice.

Now add the following four buttons under your combo box (don't forget, you can copy and paste butons):

Button 1

Name: BtnOpenFile
Size: 90 for the Width and 40 for the Height
Text: Open File

Button 2

Name: BtnPause
Size: 90 for the Width and 40 for the Height
Text: Pause

Button 3

Name: BtnResume
Size: 90 for the Width and 40 for the Height
Text: Resume

Button 4

Name: BtnStop
Size: 90 for the Width and 40 for the Height
Text: Stop

Now let's add the first slider.



Sliders in Visual Studio are called Track Bars. They don't come with the option of adding labels, unfortunately. So we'll add our Track Bar to a GroupBox and add the labels ourselves.

Locate the GroupBox control in the Toolbox on the left of Visual Studio, under the Containers category. Add one to your form. Set the following properties for it:

Name: Leave it on the default of GroupBox1
Text: Voice Speed
Width: 600
Height: 95

Now locate the Track Bar control. The easiest way to do this is to search for it. Click inside the Search Toolbar box at the top of the Toolbar. Enter Track Bar:

The TrackBar control in the Visual Studio toolbox

Now add one to your GroupBox. Change the height and width so that it fits neatly in the GroupBox. But leave some space for some labels. Your Track Bar should look like this:

A TrackBar control in a GroupBox on a Visual Studio Windows Form

Now have a look at the properties for your trackbar. Locate the Minimum property. When you're changing the speed of the Voice, the values range from -10 to 10. Set the Minimum property to -10:

The Properties window for a TrackBar showing the Minimum property set to -10

Locate the Maximum property and set it to 10. The slider on your Track Bar will then jump to the middle:

TrackBar control set to the middle value

What this means is that when you move the slider, the value can't go below a value of -10 and above a value of 10.

Now add three labels to your GroupBox. Change the Text property of the labels to Slow, Normal and Fast. Position them to the left, in the middle and to the right. Your Voice Speed GroupBox will then look like this:

The TrackBar control with labels added

Select the entire GroupBox. Now press CTRL + C on your keyboard to copy it. Press CTRL + V to paste a new one.

Move the new one down under the first one. Now set the Text property of the new GroupBox to Volume. For the three labels, change the Text to Low, Normal and Maximum.

Now click on your TrackBar. Set the Minimum value for this new TrackBar to 0 and the Maximum to 100. Locate the Value property and set it to 50. This will shift the slider to the middle as the default. The two controls on your form should look like this:

Two TrackBars, one for Voice Speed and one for Volume

Now add a text box at the bottom. Set the MultiLine property to true so that you can adjust the height. Make it big enough for your speech input. We made ours 600 by 200 and changed the font size. We also set the ScrollBars property to Vertical. Add some default text. Change the Name property to txtSpechText.

The final control to add is a button at the bottom so that you can highlight a word (or words) in your text box and just have that pronounced, rather than the whole text. Change the Name of the button to BtnPronounce.

Now that we have the form designed, we can do the coding. We'll start with a Form Load event in the next lesson.

Load System Voices >>

Back to the C# NET Contents Page

Back to the VB NET Contents Page