You can control the execution of a program by using the Program object in Small Basic. Notice the example on the screen. First, you use the Directory operation to display the current directory of the executing program. To display the path of the directory, you use the TextWindowobject with the WriteLine operation. Next, you want to delay the execution of the program by five seconds, so you use the Delay operation and define the parameter for time in milliseconds.The Program.End() statement forcefully ends the program execution. Check the output of your program by clicking the Run button on the toolbar or pressing F5 on the keyboard. The text window displays the program directory and, after a delay of five seconds, the program ends.
You can also use operations in the program to determine information about the arguments passed to a program. For example, you can use the ArgumentCount operation to find out the number of command-line arguments passed to the program. Similarly, you can use the GetArgument operation that returns the specified argument. You need to provide the index value of the required argument as a parameter to the operation.
To play a sound in your program, you use the Play operation of the Sound object. You specify the path of the local file or the URL of the file on the network as a parameter to the Play operation. Similarly, you use the Pause operation with the path of the file as the parameter. To stop playing the sound, use the Stop operation. In addition, observe the use of the Delay operation of the Program object in the example. The Delay operation helps you delay the execution of commands. You can use various sound-file formats such as .mp3, .wav, or .wma in your Small Basic programs.
Solution:TextWindow.Title = "Let’s take a quiz…" TextWindow.WriteLine("Name the third planet from the Sun: ")answer = TextWindow.Read() If (Text.StartsWith(answer, "Earth") Or Text.StartsWith(answer, "earth")) ThenSound.PlayChime()TextWindow.WriteLine("You are absolutely right!")ElseTextWindow.WriteLine("That is incorrect.")Sound.PlayBellRing()EndIf
2.4 sound, program, and text objects
Microsoft® Small Basic<br />Sound, Program, and Text Objects<br />Estimated time to complete this lesson: 1 hour<br />
Sound, Program, and Text Objects<br />In this lesson, you will learn about:<br />Using the Program object to manage execution of Small Basic programs.<br />Using the Text object to work with text-related functions in Small Basic programs.<br />Using the Sound object to enable playback of sounds in Small Basic programs.<br />
Introduction to the Program Object<br />To better understand the Program object, let’s take a look at the example displayed.<br />output<br />In this example, notice the use of Delay and End operations. The Delay operation sets a fixed time delay for the program to execute, and the End operation ends the program execution.<br />When creating programs and working with various objects and operations offered by Small Basic, you might want to control the execution of your program. <br />You can use the Program object to manipulate how your program executes.<br />
Exploring the Program Object<br />The Program object also allows you to retrieve information about the arguments passed to your program.<br />Let’s take a look at an example to understand how you can use the Program object to determine the count and value of arguments passed to a program during execution.<br />
Introduction to the Sound Object<br />After working with arguments and using the Program object, let’s explore some other interesting aspects of Small Basic.<br />Did you know that you could include sounds in your Small Basic programs?<br />In fact, you can choose from sample sounds provided in the Small Basic library.<br />Look at the example displayed. You can work with sounds in your program by using the Sound object with operations such as Play, Pause, and Stop. <br />The output of this example plays, pauses, and stops the specified sound file at regular intervals.<br />
Now, let’s take a look at an example to understand how you can play specific sounds, such as a bell ring, chime, or click, in your Small Basic program.<br />Exploring the Sound Object<br />The output will play the sound of a bell ringing, a chime, and a click, at regular intervals.<br />Observe the use of the PlayAndWait operation in the example. The PlayAndWait operation plays an audio file and waits until the audio has finished playing.<br />
Introduction to the Text Object<br />Small Basic also offers useful operations that help you work with text.<br />In Small Basic, when working with text, you can use the Text object and its various functions and methods to perform operations on the text. <br />For example, to find out the length of a text string, you can use the GetLength operation.<br />Let’s learn some more operations of the Text object with an example... <br />For example, you may want to convert all names to uppercase. Alternatively, you may want to find out whether a portion of text contains specific information.<br />
More on the Text Object<br />In this example, the user inputs an e-mail address. Using the IsSubText operation of the Text object, you can determine whether the e-mail address is valid or not.<br />Let’s take a look at another example to understand some more operations of the Text object.<br />
Operations of the Text Object<br />Another operation of the Text object is the GetSubText operation. This operation takes three parametersthe text from which you want to derive the subtext, the location from where you want to derive the subtext, and the length up to which you want to derive the subtext.<br />Here are some more uses of the Text object…<br /><ul><li>To determine whether given text starts with specified subtext, you canuse the StartWithorEndWithoperations.
To get the character code for a specific Unicode character, you can use theGetCharacterCodeoperation.
To determine the index position of specific subtext, you can use the GetIndexOf operation.</li></li></ul><li>Let’s Summarize…<br />Congratulations! Now you know how to:<br />Use the Program object to manage the execution of a Small Basic program.<br />Use the various operations of the Sound object to include sounds in your Small Basic program.<br />Use the Text object to operate on text in your Small Basic program.<br />
It’s Time to Apply Your Learning…<br />Write a program that displays a text window and perform the following steps:<br /><ul><li>Ask the user to type an answer to a simple question.
If the answer is correct, the program must display a success message along with a chime sound.
If the answer is incorrect, the program must display the appropriate message along with the sound of a bell. </li>