Lab Answer Key - Module 1 - Introducing Visual Basic and The
Lab Answer Key - Module 1 - Introducing Visual Basic and The
Lab Answer Key - Module 1 - Introducing Visual Basic and The
NET Framework
Click Start, point to All Programs, click Microsoft Visual Studio 2010, and
then click Microsoft Visual Studio 2010.
b. In the New Project dialog box, in the Installed Templates pane, expand
Visual Basic, and then click Windows.
d. Specify the following values for each of the properties in the dialog box,
and then click OK.
Name: ConsoleApplication
Location: D:\Labfiles\Lab01\Ex1\Starter
https://skillpipe.courseware-marketplace.com/reader/en-GB/Book/BookPrintView/b932d846-d99f-4edd-a713-e6e2d97c4a9c?ChapterNumber=18&FontSize=3&F 1/31
10/4/2014 Lab Answer Key: Module 1: Introducing Visual Basic and the .NET Framework
Task 2: Add code to read user input and write output to the console
1. In the Main procedure in the Module1 module, add code to read a line of text
from the keyboard and store it in a string variable named line.
Sub Main()
' Buffer to hold each line as it's read in
Dim line As String
line = Console.ReadLine()
' Loop until no more input (Ctrl-Z in a console or end-
of-file)
While Not line Is Nothing
' Format the data
line = line.Replace(",", " y:")
line = "x:" & line
line = Console.ReadLine()
End While
End Sub
This code uses the Console.ReadLine method to read the input, and includes
comments with each line of code that indicates its purpose.
2. Add code to write the text back to the console by using the Console.WriteLine
https://skillpipe.courseware-marketplace.com/reader/en-GB/Book/BookPrintView/b932d846-d99f-4edd-a713-e6e2d97c4a9c?ChapterNumber=18&FontSize=3&F 2/31
10/4/2014 Lab Answer Key: Module 1: Introducing Visual Basic and the .NET Framework
method.
Sub Main()
' Buffer to hold each line as it's read in
Dim line As String
line = Console.ReadLine()
' Write the results out to the console window
Console.WriteLine(line)
End Sub
4. Run the application and verify that it works as expected. You should be able to
enter a line of text and see that line written to the console.
a. Press Ctrl+F5.
b. In the console window, type some text, and then press Enter.
c. Verify that the text that you typed is echoed to the console.
Task 3: Modify the program to read and echo text until end-of-file is detected
1. In the Main procedure, modify the statement and comment shown in bold in the
following code example, which reads a line of text from the keyboard.
https://skillpipe.courseware-marketplace.com/reader/en-GB/Book/BookPrintView/b932d846-d99f-4edd-a713-e6e2d97c4a9c?ChapterNumber=18&FontSize=3&F 3/31
10/4/2014 Lab Answer Key: Module 1: Introducing Visual Basic and the .NET Framework
Sub Main()
' Buffer to hold each line as it's read in
Dim line As String
line = Console.ReadLine()
' Loop until no more input (Ctrl-Z in a console or
end-of-file)
While Not line Is Nothing
line = Console.ReadLine()
End While
This code incorporates the statement into a While loop that repeatedly reads
text from the keyboard until the Console.ReadLine method returns Nothing
(this happens when the Console.ReadLine method detects the end of a file, or
the user presses Ctrl+Z).
2. Move the Console.WriteLine statement into the body of the While loop as
shown in bold in the following code example. This statement echoes each line of
text that the user has entered.
Sub Main()
' Buffer to hold each line as it's read in
Dim line As String
line = Console.ReadLine()
' Loop until no more input (Ctrl-Z in a console or end-
https://skillpipe.courseware-marketplace.com/reader/en-GB/Book/BookPrintView/b932d846-d99f-4edd-a713-e6e2d97c4a9c?ChapterNumber=18&FontSize=3&F 4/31
10/4/2014 Lab Answer Key: Module 1: Introducing Visual Basic and the .NET Framework
of-file)
While Not line Is Nothing
' Write the results out to the console window
Console.WriteLine(line)
line = Console.ReadLine()
End While
End Sub
4. Run the application and verify that it works as expected. You should be able to
repeatedly enter lines of text and see those lines echoed to the console. The
application should only stop when you press Ctrl+Z.
a. Press Ctrl+F5.
b. In the console window, type some text, and then press Enter.
c. Verify that the text that you typed is echoed to the console.
1. In the body of the While loop, add the statement and comment shown in bold
https://skillpipe.courseware-marketplace.com/reader/en-GB/Book/BookPrintView/b932d846-d99f-4edd-a713-e6e2d97c4a9c?ChapterNumber=18&FontSize=3&F 5/31
10/4/2014 Lab Answer Key: Module 1: Introducing Visual Basic and the .NET Framework
Sub Main()
' Buffer to hold each line as it's read in
Dim line As String
line = Console.ReadLine()
' Loop until no more input (Ctrl-Z in a console or end-
of-file)
While Not line Is Nothing
' Format the data
line = line.Replace(",", " y:")
line = Console.ReadLine()
End While
End Sub
This code replaces each occurrence of the comma character "," in the input read
from the keyboard and replaces it with the text y:. It uses the Replace method of
the line string variable. The code then assigns the result back to the line variable.
2. Add the statement shown in bold in the following code example to the code in
the body of the While loop.
Sub Main()
' Buffer to hold each line as it's read in
Dim line As String
line = Console.ReadLine()
https://skillpipe.courseware-marketplace.com/reader/en-GB/Book/BookPrintView/b932d846-d99f-4edd-a713-e6e2d97c4a9c?ChapterNumber=18&FontSize=3&F 6/31
10/4/2014 Lab Answer Key: Module 1: Introducing Visual Basic and the .NET Framework
line = Console.ReadLine()
End While
End Sub
This code adds the prefix x: to the line variable by using the string concatenation
operator &, before the Console.WriteLine statement. The code then assigns the
result back to the line variable.
The application expects input that looks like the following code example.
23.54367,25.6789
Your code should format the output to look like the following code example.
x:23.54367 y:25.6789
https://skillpipe.courseware-marketplace.com/reader/en-GB/Book/BookPrintView/b932d846-d99f-4edd-a713-e6e2d97c4a9c?ChapterNumber=18&FontSize=3&F 7/31
10/4/2014 Lab Answer Key: Module 1: Introducing Visual Basic and the .NET Framework
a. Press Ctrl+F5.
d. Type some more text that consists of pairs of numbers that are separated
by a comma, and then press Enter again.
e. Verify that this data is correctly formatted and displayed on the console.
f. Press Ctrl+Z.
1. Add the DataFile.txt file that contains the sample data to the project. This file is
located in the D:\Labfiles\Lab01\Ex1\Starter folder, and you can copy it by
pointing to Add, and then click Existing Item on the context menu, which is
accessible by right-clicking the project in Solution Explorer. The file should be
copied to the build output folder when the project is built. You can do this by
setting the Build Action property to None, and the Copy to Output property
to Copy Always, for the DataFile.txt item in Solution Explorer.
the Build Action property to None, and then change the Copy to Output
property to Copy Always.
3. Open a Visual Studio Command Prompt window, and then move to the
D:\Labfiles\Lab01\Ex1\Starter\ConsoleApplication\bin\Debug folder.
a. Click Start, point to All Programs, click Microsoft Visual Studio 2010, click
Visual Studio Tools, and then click Visual Studio Command Prompt
(2010).
\ConsoleApplication\bin\Debug folder.
Verify that the output that is generated looks like the following code example.
x:23.8976 y:12.3218
x:25.7639 y:11.9463
x:24.8293 y:12.2134
In the Command Prompt window, type the command in the following code
example, and then press Enter.
https://skillpipe.courseware-marketplace.com/reader/en-GB/Book/BookPrintView/b932d846-d99f-4edd-a713-e6e2d97c4a9c?ChapterNumber=18&FontSize=3&F 9/31
10/4/2014 Lab Answer Key: Module 1: Introducing Visual Basic and the .NET Framework
5. Close the Command Prompt window, and then return to Visual Studio.
6. Modify the project properties to redirect input from the DataFile.txt file when the
project is run by using Visual Studio. You can do this by typing in < DataFile.txt
in the Command line arguments text box, on the Debug page, of the Project
Designer.
b. On the Debug page, in the Command line arguments text box, type <
DataFile.txt.
The application will run, but the console window will close immediately after the
output is generated. This is because Visual Studio only prompts the user to close
the console window when a program is run without debugging. When a
program is run in the Debug mode, Visual Studio automatically closes the
console window as soon as the program finishes.
8. Set a breakpoint on the End Sub statement that signals the end of the Main
procedure.
In the Module1.vb file, move the cursor to the End Sub statement for the
Main procedure, right-click, point to Breakpoint, and then click Insert
Breakpoint.
9. Run the application again in the Debug mode. Verify that the output that is
generated is the same as the output that is generated when the program runs
from the command line.
https://skillpipe.courseware-marketplace.com/reader/en-GB/Book/BookPrintView/b932d846-d99f-4edd-a713-e6e2d97c4a9c?ChapterNumber=18&FontSize=3& 10/31
10/4/2014 Lab Answer Key: Module 1: Introducing Visual Basic and the .NET Framework
The application will run, and then the program will stop when control reaches the
end of the Main procedure and Visual Studio has the focus.
d. Return to Visual Studio, and then on the Debug menu, click Continue. The
application will finish.
b. In the New Project dialog box, in the Project Types pane, expand Visual Basic,
and then click Windows.
d. Specify the following values for each of the properties in the dialog box, and
then click OK.
Name: WpfApplication
https://skillpipe.courseware-marketplace.com/reader/en-GB/Book/BookPrintView/b932d846-d99f-4edd-a713-e6e2d97c4a9c?ChapterNumber=18&FontSize=3& 11/31
10/4/2014 Lab Answer Key: Module 1: Introducing Visual Basic and the .NET Framework
Location: D:\Labfiles\Lab01\Ex2\Starter
e. Click and drag the Button control to anywhere in the MainWindow window.
2. Using the Properties window, set the properties of each control by using the
values in the following table. Leave any other properties at their default values.
Height 28
https://skillpipe.courseware-marketplace.com/reader/en-GB/Book/BookPrintView/b932d846-d99f-4edd-a713-e6e2d97c4a9c?ChapterNumber=18&FontSize=3& 12/31
10/4/2014 Lab Answer Key: Module 1: Introducing Visual Basic and the .NET Framework
HorizontalAlignment Left
Margin 12,12,0,0
VerticalAlignment Top
Width 302
Height 23
HorizontalAlignment Left
Margin 320,17,0,0
VerticalAlignment Top
Width 80
Height 238
HorizontalAlignment Left
Margin 14,50,0,0
Text
VerticalAlignment Top
Width 384
b. In the Properties window, click the textBox1 text adjacent to the TextBox
prompt, and then change the name to TestTextBox.
c. In the list of properties in the Properties window, locate the Height property,
and then change it to 28.
d. Repeat this process for the remaining properties of the TextBox control.
https://skillpipe.courseware-marketplace.com/reader/en-GB/Book/BookPrintView/b932d846-d99f-4edd-a713-e6e2d97c4a9c?ChapterNumber=18&FontSize=3& 13/31
10/4/2014 Lab Answer Key: Module 1: Introducing Visual Basic and the .NET Framework
f. Follow the procedure described in steps b to e to set the specified properties for
this control.
h. Follow the procedure described in steps b to e to set the specified properties for
this control.
Task 3: Add code to format the data that the user enters
2. Add code to the event-handler method that reads the contents of the TextBox
control into a string variable named line, formats this string in the same way as
the console application in Exercise 1, and then displays the formatted result in
the TextBlock control. Notice that you can access the contents of a TextBox
control and a TextBlock control by using the Text property. Your code should
resemble the following code.
4. Run the application and verify that it works in a similar manner to the original
console application in Exercise 1.
a. Press Ctrl+F5.
https://skillpipe.courseware-marketplace.com/reader/en-GB/Book/BookPrintView/b932d846-d99f-4edd-a713-e6e2d97c4a9c?ChapterNumber=18&FontSize=3& 15/31
10/4/2014 Lab Answer Key: Module 1: Introducing Visual Basic and the .NET Framework
c. Verify that x:23.654 y:67.823 appears in the TextBlock control below the
TextBox control.
1. Create an event handler for the Window_Loaded event. This event occurs when
the window is about to be displayed, just after the application has started up.
You can do this by clicking the title bar of the MainWindow window in the
MainWindow.xaml file, and then double-clicking the Loaded event in the list of
events in the Properties window.
2. In the event-handler method, add the code shown in bold in the following code
example.
line = Console.ReadLine()
https://skillpipe.courseware-marketplace.com/reader/en-GB/Book/BookPrintView/b932d846-d99f-4edd-a713-e6e2d97c4a9c?ChapterNumber=18&FontSize=3& 16/31
10/4/2014 Lab Answer Key: Module 1: Introducing Visual Basic and the .NET Framework
line = Console.ReadLine()
End While
End Sub
This code reads text from the standard input, formats it in the same manner as
Exercise 1, and then appends the results to the end of the TextBlock control. It
continues to read all text from the standard input until end-of-file is detected.
Notice that you can use the &= operator to append data to the Text property of
a TextBlock control, and you can add the newline character, vbNewLine,
between lines for formatted output to ensure that each item appears on a new
line in the TextBlock control.
3. Perform the following steps to modify the project settings to redirect standard
input to come from the DataFile.txt file. A copy of this file is available in the
D:\Labfiles\Lab01\Ex2\Starter folder.
https://skillpipe.courseware-marketplace.com/reader/en-GB/Book/BookPrintView/b932d846-d99f-4edd-a713-e6e2d97c4a9c?ChapterNumber=18&FontSize=3& 17/31
10/4/2014 Lab Answer Key: Module 1: Introducing Visual Basic and the .NET Framework
e. On the Debug tab, in the Command line arguments: text box, type
< DataFile.txt
4. Build and run the application in the Debug mode. Verify that when the
application starts, it reads the data from DataFile.txt and displays in the
TextBlock control the results in the following code example.
x:23.8976 y:12.3218
x:25.7639 y:11.9463
x:24.8293 y:12.2134
Modify the contents of the DataFile.txt file as the following code example shows.
https://skillpipe.courseware-marketplace.com/reader/en-GB/Book/BookPrintView/b932d846-d99f-4edd-a713-e6e2d97c4a9c?ChapterNumber=18&FontSize=3& 18/31
10/4/2014 Lab Answer Key: Module 1: Introducing Visual Basic and the .NET Framework
1.2543,0.342
32525.7639,99811.9463
24.8293,12.2135
23.8976,12.3218
25.7639,11.9463
24.8293,12.2135
b. Edit the data in the file so that it resembles the data shown.
Task 2: Step through the application by using the Visual Studio 2010 debugger
3. Step into the second statement in the Window_Loaded method that contains
executable code.
b. Repeat step a.
4. Examine the value of the line variable. It should be 1.2543,0.342. This is the text
from the first line of the DataFile.txt file.
https://skillpipe.courseware-marketplace.com/reader/en-GB/Book/BookPrintView/b932d846-d99f-4edd-a713-e6e2d97c4a9c?ChapterNumber=18&FontSize=3& 20/31
10/4/2014 Lab Answer Key: Module 1: Introducing Visual Basic and the .NET Framework
7. Examine the value of the line variable. It should now be 1.2543 y:0.342. This is
the result of calling the Replace method and assigning the result back to line.
In the Locals window, verify that the value of line is 1.2543 y:0.342.
9. Examine the value of the line variable. It should now be x:1.2543 y:0.342 . This is
the result of prefixing the text x: to line and suffixing a newline character. The
latter is displayed as a space.
In the Locals window, verify that the value of line is x:1.2543 y:0.342 .
b. Repeat step a.
11. In the Immediate window, examine the value of the Text property of the
FormattedTextTextBlock control. It should contain the same text as the line
variable.
https://skillpipe.courseware-marketplace.com/reader/en-GB/Book/BookPrintView/b932d846-d99f-4edd-a713-e6e2d97c4a9c?ChapterNumber=18&FontSize=3& 21/31
10/4/2014 Lab Answer Key: Module 1: Introducing Visual Basic and the .NET Framework
a. In the Immediate window, type the expression in the following code example
(including the question mark), and then press Enter.
?FormattedTextTextBlock.Text
Right-click End While, point to Breakpoint, and then click Insert Breakpoint.
13. Continue running the programming for the next iteration of the While loop. It
should stop when it reaches the breakpoint at the end of the loop.
14. Examine the value of the line variable. It should now be x:32525.7639
y:99811.9463. This is the data from the second line of DataFile.txt.
15. Remove the breakpoint from the end of the While loop.
16. Continue the programming running. The Window_Loaded method should now
run to completion and display the MainWindow window. The TextBlock control
should contain all of the data from DataFile.txt, formatted correctly.
https://skillpipe.courseware-marketplace.com/reader/en-GB/Book/BookPrintView/b932d846-d99f-4edd-a713-e6e2d97c4a9c?ChapterNumber=18&FontSize=3& 22/31
10/4/2014 Lab Answer Key: Module 1: Introducing Visual Basic and the .NET Framework
b. Verify that the TextBlock control displays the formatted results for every
line in the DataFile.txt file.
17. Close the MainWindow window, and then return to Visual Studio.
2. Add the XML comment in the following code example before the MainWindow
class declaration. There should be no blank line between the class declaration and
the last line of the XML comment.
''' <summary>
https://skillpipe.courseware-marketplace.com/reader/en-GB/Book/BookPrintView/b932d846-d99f-4edd-a713-e6e2d97c4a9c?ChapterNumber=18&FontSize=3& 23/31
10/4/2014 Lab Answer Key: Module 1: Introducing Visual Basic and the .NET Framework
''' <summary>
''' WPF application to read and format data
''' </summary>
''' <remarks></remarks>
Class MainWindow
3. Add the XML comment in the following code example before the
TestButton_Click method. There should be no blank line between the method
declaration and the last line of the XML comment.
''' <summary>
''' Read a line of data entered by the user.
''' Format the data and display the results in the
''' FormattedTextTextBlock control.
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
''' <remarks></remarks>
https://skillpipe.courseware-marketplace.com/reader/en-GB/Book/BookPrintView/b932d846-d99f-4edd-a713-e6e2d97c4a9c?ChapterNumber=18&FontSize=3& 24/31
10/4/2014 Lab Answer Key: Module 1: Introducing Visual Basic and the .NET Framework
''' <summary>
''' Read a line of data entered by the user.
''' Format the data and display the results in the
''' FormattedTextTextBlock control.
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
''' <remarks></remarks>
Private Sub TestButton_Click(ByVal sender As
System.Object...
4. Add the XML comment in the following code example before the
Window_Loaded method. There should be no blank line between the method
declaration and the last line of the XML comment.
''' <summary>
''' After the Window has loaded, read data from the standard
input.
''' Format each line and display the results in the
''' FormattedTextTextBlock control.
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
''' <remarks></remarks>
''' <summary>
https://skillpipe.courseware-marketplace.com/reader/en-GB/Book/BookPrintView/b932d846-d99f-4edd-a713-e6e2d97c4a9c?ChapterNumber=18&FontSize=3& 25/31
10/4/2014 Lab Answer Key: Module 1: Introducing Visual Basic and the .NET Framework
''' After the Window has loaded, read data from the
standard input.
''' Format each line and display the results in the
''' FormattedTextTextBlock control.
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
''' <remarks></remarks>
Private Sub Window_Loaded(ByVal sender As System.Object...
5. Save MainWindow.xaml.vb.
''' <summary>
''' WPF application to read and format data
''' </summary>
''' <remarks></remarks>
Class MainWindow
''' <summary>
''' Read a line of data entered by the user.
''' Format the data and display the results in the
''' FormattedTextTextBlock control.
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
''' <remarks></remarks>
Private Sub TestButton_Click(ByVal sender As
System.Object, ByVal e As
System.Windows.RoutedEventArgs) Handles TestButton.Click
' Copy the contents of the TextBox into a string
Dim line As String = TestTextBox.Text
https://skillpipe.courseware-marketplace.com/reader/en-GB/Book/BookPrintView/b932d846-d99f-4edd-a713-e6e2d97c4a9c?ChapterNumber=18&FontSize=3& 26/31
10/4/2014 Lab Answer Key: Module 1: Introducing Visual Basic and the .NET Framework
''' <summary>
''' After the Window has loaded, read data from the
standard input.
''' Format each line and display the results in the
''' FormattedTextTextBlock control.
''' </summary>
''' <param name="sender"></param>
''' <param name="e"></param>
''' <remarks></remarks>
Private Sub Window_Loaded(ByVal sender As System.Object,
ByVal e As
System.Windows.RoutedEventArgs) Handles MyBase.Loaded
' Buffer to hold a line read from the file on
standard input
Dim line As String
line = Console.ReadLine()
' Loop until the end of the file
While Not line Is Nothing
' Format the data in the buffer
line = line.Replace(",", " y:")
line = "x:" & line & vbNewLine
' Put the results into the TextBlock
https://skillpipe.courseware-marketplace.com/reader/en-GB/Book/BookPrintView/b932d846-d99f-4edd-a713-e6e2d97c4a9c?ChapterNumber=18&FontSize=3& 27/31
10/4/2014 Lab Answer Key: Module 1: Introducing Visual Basic and the .NET Framework
line = Console.ReadLine()
End While
End Sub
End Class
1. Set the project properties to generate an XML documentation file when the
project is built.
b. On the Compile tab, ensure the Generate XML documentation file check
box is selected.
Internet Explorer. Verify that it contains the text for the XML comments that
you added to the WPF application (it will also contain other comments that
Visual Studio has generated).
a. Click Start, point to All Programs, click Accessories, right-click Command Prompt,
as administrator.
b. In the User Account Control dialog box, in the Password text box, type Pa$$w0rd
a. In the Command Prompt window, type the command in the following code example,
Enter.
https://skillpipe.courseware-marketplace.com/reader/en-GB/Book/BookPrintView/b932d846-d99f-4edd-a713-e6e2d97c4a9c?ChapterNumber=18&FontSize=3& 29/31
10/4/2014 Lab Answer Key: Module 1: Introducing Visual Basic and the .NET Framework
4.Use Notepad to edit the builddoc.cmd script, and then edit the input variable to
D:\Labfiles\Lab01\Ex4\Starter\WpfApplication\bin\Debug
\WpfApplication.exe.
b.In the Command Prompt window, type the command in the following code example,
Enter.
notepad builddoc.cmd
set
input="D:\Labfiles\Lab01\Ex4\Starter\WpfApplication\bin\Debug\WpfAppl
a. In the Command Prompt window, type the command in the following code example.
builddoc.cmd
6.Open the test.chm file that the builddoc.cmd script generates in the D:\Labfiles\Lab01
\Ex4\HelpFile\Output folder.
\Ex4\HelpFile\Output folder.
https://skillpipe.courseware-marketplace.com/reader/en-GB/Book/BookPrintView/b932d846-d99f-4edd-a713-e6e2d97c4a9c?ChapterNumber=18&FontSize=3& 30/31
10/4/2014 Lab Answer Key: Module 1: Introducing Visual Basic and the .NET Framework
7.Browse documentation that is generated for your application, and then close test.chm.
https://skillpipe.courseware-marketplace.com/reader/en-GB/Book/BookPrintView/b932d846-d99f-4edd-a713-e6e2d97c4a9c?ChapterNumber=18&FontSize=3& 31/31