Tải bản đầy đủ (.pdf) (119 trang)

Mastering Microsoft Visual Basic 2008 phần 10 pot

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (1.48 MB, 119 trang )

Petroutsos bapp01.tex V3 - 01/28/2008 5:29pm Page 1000
1000 APPENDIX A THE BOTTOM LINE
Use arrays. Arrays are structures for storing sets of data, as opposed to single-valued
variables.
Master It How would you declare an array for storing 12 names and another one for
storing 100 names and Social Security numbers?
Solution The first array stores a set of single-valued data (names) and it has a single
dimension. Because the indexing of the array’s elements starts at 0, the last element’s index
for the first array is 11, and it must be declared as
Dim Names(11) As String
The second array stores a set of pair values (names and SSNs), and it must be declared as a
two-dimensional array:
Dim Persons(99,1) As String
Chapter 3: Programming Fundamentals
Use Visual Basic’s flow-control statements. Visual Basic provides several statements for
controlling the sequence in which statements are executed: decision statements, which change
the course of execution based on the outcome of a comparison, and loop statements, which
repeat a number of statements while a condition is true or false.
Master It Explain briefly the decision statements of Visual Basic.
Solution The basic decision statement in VB is the If End If statement, which executes
the statements between the If and End If keywords if the condition specified in the If
part is True. A variation of this statement is the If Then Else End If statements.
If the same expression must be compared to multiple values and the program should
execute different statements depending on the outcome of the comparison, use the
Select Case statement.
Write subroutines and functions. To manage large applications, we break our code into
small, manageable units. These units of code are the subroutines and functions. Subroutines
perform actions and don’t return any values. Functions, on the other hand, perform calcula-
tions and return values. Most of the language’s built-in functionality is in the form of
functions.
Master It How will you create multiple overloaded forms of the same function?


Solution Overloaded functions are variations of the same function with different argu-
ments. All overloaded forms of a function have the same name, and they’re prefixed with
the Overloads keyword. Their lists of arguments, however, are different — either in the
number of arguments or in their types.
Pass arguments to subroutines and functions. Procedures and functions communicate with
one another via arguments, which are listed in a pair of parentheses following the procedure’s
Petroutsos bapp01.tex V3 - 01/28/2008 5:29pm Page 1001
CHAPTER 4: GUI DESIGN AND EVENT-DRIVEN PROGRAMMING 1001
name. Each argument has a name and a type. When you call the procedure, you must supply
values for each argument and the types of the values should match the types listed in the pro-
cedure’s definition.
Master It Explain the difference between passing arguments by value and passing argu-
ments by reference.
Solution The first mechanism, which was the default mechanism with earlier versions
of the language, passes a reference to the argument. Arguments passed by reference are
prefixed by the keyword ByRef in the procedure’s definition. The procedure has access
to the original values of the arguments passed by reference and can modify them.
The second mechanism passes to the procedure a copy of the original value. Arguments
passed by value are prefixed with the keyword ByVal in the procedure’s definition. The
procedure may change the values of the arguments passed by value, but the changes won’t
affect the value of the original variable.
Chapter 4: GUI Design and Event-Driven Programming
Design graphical user interfaces. A Windows application consists of a graphical user inter-
face and code. The interface of the application is designed with visual tools and consists of
controls that are common to all Windows applications. You drop controls from the Toolbox
window onto the form, size and align the controls on the form, and finally set their properties
through the Properties window. The controls include quite a bit of functionality right out of
the box, and this functionality is readily available to your application without a single line
of code.
Master It Describe the process of aligning controls on a form.

Solution To align controls on a form, you should select them in groups, according to their
alignment. Controls can be aligned to the left, right, top, and bottom. After selecting a group
of controls with a common alignment, apply the proper alignment with one of the
commands of the Format  Align menu. Before aligning multiple controls, you should
adjust their spacing. Select the controls you want to space vertically or horizontally
and adjust their spacing with one of the commands of the Format  Horizontal Spacing and
Format  Vertical Spacing methods. You can also align controls visually, by moving them
with the mouse. As you move a control around, a blue snap line appears every time the con-
trol is aligned with another one on the form.
Program events. Windows applications follow an event-driven model: We code the events
to which we want our application to respond. The Click events of the various buttons are
typical events to which an application reacts. You select the actions to which you want your
application to react and program these events accordingly.
When an event is fired, the appropriate event handler is automatically invoked. Event han-
dlers are subroutines that pass two arguments to the application: the senderobject (which is
an object that represents the control that fired the event) and the e argument (which carries
additional information about the event).
Petroutsos bapp01.tex V3 - 01/28/2008 5:29pm Page 1002
1002 APPENDIX A THE BOTTOM LINE
Master It How will you handle certain keystrokes regardless of the control that receives
them?
Solution You can intercept all keystrokes at the form’s level by setting the form’s
KeyPreview property to True. Then insert some code in the form’s KeyPress event han-
dler to examine the keystroke passed to the event handler and process it. To detect the
key presses in the KeyPress event handler, use an If statement like the following:
If e.KeyChar = ”A” Then
’ process the A key
End If
Write robust applications with error handling. Numerous conditions can cause an applica-
tion to crash, but a professional application should be able to detect abnormal conditions and

handle them gracefully. To begin with, you should always vali date your data before you attempt
to use them in your code. A well-known computer term is ‘‘garbage in, garbage out’’, which
means you shouldn’t perform any calculations on invalid data.
Master It How will you execute one or more statements in the context of a structured
exception handler?
Solution A structured exception handler has the following syntax:
Try
{statements}
Catch ex As Exception
{statements to handle exception}
End Try
The statements you want to execute must be inserted in the Try block of the statement. If
executed successfully, program execution continues with the statements following the End
Try statement. If an error occurs, the Catch block is activated, where you can display the
appropriate message and take the proper actions. At the very least, you should save the
user data and then terminate the application. In many cases, it’s even possible to remedy
the situation that caused the exception in the first place.
Chapter 5: The Vista Interface
Create a simple WPF application. WPF is a new and powerful technology for creating user
interfaces. WPF is one of the core technologies in the .NET Framework 3.5 and is integrated
into Windows Vista. WPF is also supported on Windows XP. WPF takes advantage of the
graphics engines and display capabilities of the modern computer and is vector based and res-
olution independent.
Master It Develop a simple ‘‘Hello World’’ type of WPF application that displays a But-
ton control and Label control. Clicking the button should set the content property of a Label
control to Hi There!
Petroutsos bapp01.tex V3 - 01/28/2008 5:29pm Page 1003
CHAPTER 5: THE VISTA INTERFACE 1003
Solution Complete the following steps.
1. Open Visual Studio 2008 and choose File  New Project.

2. From the New Project dialog box, select WPF Application and click OK.
3. From the Toolbox, drag a Button control and Label control to Window1 on the design
surface.
4. Double-click the Button control and add the following line of code to the Button1 Click
event in code-behind:
Label1.Content = ”Hi There!”
Data-bind controls in WPF. The ability to bind controls to a data source is an essential aspect
of separating the UI from the business logic in an application.
Master It Data-bind a Label control to one field in a record returned from a database on
your computer.
Solution Complete the following steps.
1. Open Visual Studio 2008 and create a new WPF project.
2. Establish a link to an existing database on your system by opening the Server Explorer
window (click the appropriate tab at the bottom of the Toolbox area of Visual Studio)
and right-clicking Data Connections. Choose Add Connection from the context
menu and follow the prompts. Note that you use the Microsoft SQL Server Database File
option if you are planning to connect to a database created by SQL Server Express (the
default database system that ships with Visual Studio 2008).
3. Open the Data Sources window by clicking the tab at the bottom of the Server Explorer
window, and then click the Add New Data Source link. This opens the Data Source Con-
figuration Wizard. Follow the prompts to set up the dataset.
4. Switch to code-behind (Window1.xaml.vb)forWindow1.xaml. Add the following code.
Note that the ContactsDataSet, ContactsDataSetTableAdapters, and CustomersTable-
Adapter names will vary according to the actual names that you have set up for
your dataset.
Class Window1
Dim mydataset As New ContactsDataSet
Dim mydataAdapter As New
ContactsDataSetTableAdapters.CustomersTableAdapter
Private Sub Window1

Loaded(ByVal sender As Object,
ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
mydataAdapter.Fill(mydataset.Tables(0))
Me.DataContext = mydataset.Tables(0).Rows(0)
End Sub
End Class
Petroutsos bapp01.tex V3 - 01/28/2008 5:29pm Page 1004
1004 APPENDIX A THE BOTTOM LINE
5. Switch back to XAML view for Window1.xaml and add the following markup (without
line breaks). You may need to change the FirstName reference in the binding for Label1
to whichever database field that you want displayed:
<Window x:Class=”Window1”
xmlns=” />xmlns:x=” />Title=”Window1” Height=”300” Width=”300”>
<Grid>
<Label Margin=”38,129,47,90” Name=”Label1” Content=”{Binding
Path=FirstName}” ></Label>
</Grid>
</Window>
6. Run the application. The contents of your nominated field from the first table indexed in
your database (0) should be displayed in the Label control.
Use a data template to control data presentation. WPF enables a very flexible approach to
presenting data by using data templates. The developer can create and fully customize data
templates for data formatting.
Master It Create a data template to display a Name, Surname, Gender combination in
a horizontal row in a ComboBox control. Create a simple array and class of data to feed
the application.
Solution Complete the following steps.
1. Add the following code to the XAML source for Window1.xaml (delete the line breaks):
<Window x:Class=”Window1”
xmlns=” />xmlns:x=” />Title=”Window1” Height=”300” Width=”300”>

<Grid Name=”myGrid”>
<Grid.Resources>
<DataTemplate x:Key=”NameStyle”>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width=”60” />
<ColumnDefinition Width=”60” />
<ColumnDefinition Width=”*” />
</Grid.ColumnDefinitions>
<TextBlock Grid.Column=”0”
Text=”{Binding Path=FirstName}”/>
<TextBlock Grid.Column=”1”
Text=”{Binding Path=Surname}”/>
Petroutsos bapp01.tex V3 - 01/28/2008 5:29pm Page 1005
CHAPTER 5: THE VISTA INTERFACE 1005
<TextBlock Grid.Column=”2”
Text=”{Binding Path=Gender}”/>
</Grid>
</DataTemplate>
</Grid.Resources>
<ComboBox
ItemTemplate=”{StaticResource NameStyle}”
ItemsSource=”{Binding }” IsSynchronizedWithCurrentItem=”true”
Height=”25” VerticalAlignment=”Top” Name=”ComboBox1” />
</Grid>
</Window>
2. Switch to code-behind for Window1.xaml (Window1.xaml.vb) and add the following
code:
Class Window1
Private Class myList

Dim
name As String
Dim
surname As String
Dim
gender As String
Public Sub New(ByVal FirstName As String, ByVal
Surname As String, ByVal Gender As String)
name = FirstName
surname = Surname
gender = Gender
End Sub
Public ReadOnly Property FirstName() As String
Get
Return
name
End Get
End Property
Public ReadOnly Property Surname() As String
Get
Return
surname
End Get
End Property
Public ReadOnly Property Gender() As String
Get
Return
gender
End Get
End Property

End Class
Private Sub Window1
Loaded(ByVal sender As Object,
ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
Petroutsos bapp01.tex V3 - 01/28/2008 5:29pm Page 1006
1006 APPENDIX A THE BOTTOM LINE
Dim myArray As New ArrayList
myArray.Add(New MyList(”Fred”, ”Bloggs”, ”M”))
myArray.Add(New MyList(”Mary”, ”Green”, ”F”))
myArray.Add(New MyList(”Sally”, ”Smith”, ”F”))
myArray.Add(New MyList(”John”, ”Doe”, ”M”))
myArray.Add(New MyList(”Jemma”, ”Bloggs”, ”F”))
Me.DataContext = myArray
End Sub
End Class
3. Run the application. A combo box containing a list of the contacts should be displayed
as FirstName, Surname, and Gender.
Chapter 6: Basic Windows Controls
Use the TextBox control as a data-entry and text-editing tool. The TextBox control is the
most common element of the Windows interface, short of the Button control, and it’s used
to display and edit text. You can use a TextBox control to prompt users for a single line of
text (such as a product name) or a small document (a product’s detailed description).
Master It What are the most important properties of the TextBox control? Which ones
would you set in the Properties windows at design-time?
Solution First you must decide whether you want the control to hold a single line of text
or multiple text lines, and set the MultiLine property accordingly. You must also decide
whether the control should wrap words automatically and then set the WordWrap and
ScrollBars properties accordingly. If you want the control to display some text initially,
set the control’s Text property to the desired text. At runtime you can retrieve the text
entered by the user in the control, with the same property. Another property, the Lines

array, allows you to retrieve individual paragraphs of text. Each paragraph can be broken
into multiple text lines on the control, but each is stored in a single element of the
Lines array.
Master It How will you implement a control that suggests lists of words matching the
characters entered by the user?
Solution Use the autocomplete properties AutoCompleteMode, AutoCompleteSource,
and AutoCompleteCustomSource.TheAutoComplete property determines whether the
control will suggest the possible strings, automatically complete the current word as you
type, or do both. The AutoCompleteSource property specifies where the strings that
will be displayed will come from, and its value is a member of the AutoCompleteSource
enumeration. If this property is set to AutoCompleteSoure.CustomSource, you must set
up an AutoCompleteStringCollection collection with your custom strings and assign it to
the AutoCompleteCustomCource property.
Use the ListBox, CheckedListBox, and ComboBox controls to present lists of items. The
ListBox control contains a list of items from which the user can select one or more, depending
on the setting of the SelectionMode property.
Petroutsos bapp01.tex V3 - 01/28/2008 5:29pm Page 1007
CHAPTER 7: WORKING WITH FORMS 1007
Master It How will you locate an item in a ListBox control?
Solution To locate a string in a ListBox control, use the FindString and FindString-
Exact methods. The FindString method locates a string that partially matches the one
you’re searching for; FindStringExact finds an exact match. Both methods perform case-
insensitive searches and return the index of the item they’ve located in the list.
We usually call the FindStringExact method and then examine its return value. If an exact
match was found, we select the item with the index returned by the FindStringExact
method. If an exact match was not found, in which case the method returns −1, we call the
FindString method to locate the nearest match.
Use the ScrollBar and TrackBar controls to enable users to specify sizes and positions with
the mouse. The ScrollBar and TrackBar controls let the user specify a magnitude by scrolling
a selector between its minimum and maximum values. The ScrollBar control uses some visual

feedback to display the effects of scrolling on another entity, such as the current view in a
long document.
Master It Which event of the ScrollBar control would you code to provide visual feedback
to the user?
Solution The ScrollBar control fires two events: the Scroll event and the ValueChanged
event. They’re very similar, and you can program either event to react to the changes in
the ScrollBar control. The advantage of the Scroll event is that it reports the action that
caused it through the e.Type property. You can examine the value of this property in your
code and react to actions such as the end of the scroll:
Private Sub blueBar Scroll(
ByVal sender As System.Object,
ByVal e As System.Windows.Forms.ScrollEventArgs)
Handles blueBar.Scroll
If e.Type = ScrollEventType.EndScroll Then
’ perform calculations and provide feedback
End If
End Sub
Chapter 7: Working with Forms
Use forms’ properties. Forms expose a lot of trivial properties for setting their appearance.
In addition, they expose a few properties that simplify the task of designing forms that can be
resized at runtime. The Anchor property causes a control to be anchored to one or more edges
of the form to which it belongs. The Dock property allows you to place on the form controls that
are docked to one of its edges. To create forms with multiple panes that the user can resize at
runtime, use the SplitContainer control. If you just can’t fit all the controls in a reasonably sized
form, use the AutoScroll properties to create a scrollable form.
Master It You’ve been asked to design a form with three distinct sections. You should
also allow users to resize each section. How will you design this form?
Solution The type of form required is easily designed with visual tools and the help
of the SplitContainer control. Place a SplitContainer control on the form and set its Dock
Petroutsos bapp01.tex V3 - 01/28/2008 5:29pm Page 1008

1008 APPENDIX A THE BOTTOM LINE
property to Fill. You’ve just created two vertical panes on the form, and users can change
their relative sizes at any time. To create a third pane, place another SplitContainer con-
trol on one of the first control’s panes and set its Dock property to Fill, and its Orientation
property to Horizontal. At this point, the form is covered by three panes, and users can
change each pane’s size at the expense of its neighboring panes.
Design applications with multiple forms. Typical applications are made up of multiple
forms: the main form and one or more auxiliary forms. To show an auxiliary form from within
the main form’s code, call the auxiliary form’s Show method, or the ShowDialog method if you
want to display the auxiliary form modally (as a dialog box).
Master It How will you set the values of selected controls in a dialog box, display them,
and then read the values selected by the user from the dialog box?
Solution Create a Form variable that represents the dialog box and then access any con-
trol in the dialog box through its name as usual, prefixed by the form’s name:
Dim Dlg As AuxForm
Dlg.txtName = ”name”
Then call the form’s ShowDialog method to display it modally and examine the
DialogResult property returned by the method. If this value is OK, process the data in the
dialog box, or else ignore them:
If Dlg.ShowDialog = DialogResult.OK Then
UserName = Dlg.TxtName
End If
To display an auxiliary form, just call the Show method. This method doesn’t return a value,
and you can read the auxiliary form’s contents from within the main form’s code at any
time. You can also access the controls of the main form from within the auxiliary
form’s code.
Design dynamic forms. You can create dynamic forms by populating them with controls
at runtime through the form’s Controls collection. First, create instances of the appropriate
controls by declaring variables of the corresponding type. Then set the properties of the vari-
able that represents the control. Finally, place the control on the form by adding it to the form’s

Controls collection.
Master It How will you add a TextBox control to your form at runtime and assign a
handler to the control’s TextChanged event?
Solution Create an instance of the TextBox control, set its Visible property, and add it to
the form’s Controls collection:
Dim TB As New TextBox
TB.Visible = True
’ statements to set other properties,
’ including the control’s location on the form
Me.Controls.Add(TB)
Then write a subroutine that will handle the TextChanged event. This subroutine, let’s call
it TBChanged(), should have the same signature as the TextBox control’s TextChanged
Petroutsos bapp01.tex V3 - 01/28/2008 5:29pm Page 1009
CHAPTER 8: MORE WINDOWS CONTROLS 1009
event. Use the AddHandler statement to associate the TBChanged() subroutine with the new
control’s TextChanged event:
AddHandler TB.TextChanged,
New SystemEventHandler(AddressOf TBChanged)
Design menus. Both form menus and context menus are implemented through the Menu-
Strip control. The items that make up the menu are ToolStripMenuItem objects. The
ToolStripMenuItem objects give you absolute control over the structure and appearance of the
menus of your application.
Master It What are the two basic events fired by the ToolStripMenuItem object?
Solution When the user clicks a menu item, the DropDownOpened and Click events are
fired, in this order. The DropDownOpened event gives you a chance to modify the menu that’s
about to be opened. After the execution of the DropDownOpened event handler, the Click
event takes place to indicate the selection of a menu command. We rarely program the
DropDownOpened event, but every menu item’s Click event handler should contain some
code to react to the selection of the item.
Chapter 8: More Windows Controls

Use the OpenFileDialog and SaveFileDialog controls to prompt users for filenames. Win-
dows applications use certain controls to prompt users for common information, such as
filenames, colors, and fonts. Visual Studio provides a set of controls, which are grouped in
the Dialogs section of the Toolbox. All common dialog controls provide a ShowDialog method,
which displays the corresponding dialog box in a modal way. The ShowDialog method returns
a value of the DialogResult type, which indicates how the dialog box was closed, and you
should examine this value before processing the data.
Master It Your application needs to open an existing file. How will you prompt users for
the file’s name?
Solution First you must drop an instance of the OpenFileDialog control on the form. To
limit the files displayed in the Open dialog box, use the Filter property to specify the rel-
evant file type(s). To display text files only, set the Filter property to Text files|*.txt.
If you want to display multiple extensions, use a semicolon to separate extensions with the
Filter property; for example, the string Images|*.BMP;*.GIF;*.JPGwill cause the con-
trol to select all the files of these three types and no others. The first part of the expression
(Images) is the string that will appear in the drop-down list with the file types. You should
also set the CheckFileExists property to True to make sure that the file specified on the
control exists. Then display the Open dialog box by calling its ShowDialog method, as
shown here:
If FileOpenDialog1.ShowDialog =
Windows.Forms.DialogResult.OK
{process file FileOpenDialog1.FileName}
End If
To retrieve the selected file, use the control’s FileName property, which is a string with the
selected file’s path.
Petroutsos bapp01.tex V3 - 01/28/2008 5:29pm Page 1010
1010 APPENDIX A THE BOTTOM LINE
Master It You’re developing an application that encrypts multiple files (or resizes many
images) in batch mode. How will you prompt the user for the files to be processed?
Solution There are two techniques to prompt users for multiple filenames. Both tech-

niques, however, are limited in the sense that all files must reside in the same folder. The
first technique is to set the MultiSelect property of the OpenFileDialog control to True.
Users will be able to select multiple files by using the Ctrl and Shift keys. The selected files
will be reported to your application through the FileNames property of the control, which is
an array of strings.
OpenFileDialog1.Multiselect = True
OpenFileDialog1.ShowDialog()
Dim filesEnum As IEnumerator
ListBox1.Items.Clear()
filesEnum =
OpenFileDialog1.FileNames.GetEnumerator()
While filesEnum.MoveNext
’ current file’s name is filesEnum.Current
End While
The other technique is to use the FolderBrowserDialog control, which prompts users to
select a folder, not individual files. Upon return, the control’s SelectedPath property con-
tains the pathname of the folder selected by the user from the dialog box, and you can use
this property to process all files of a specific type in the selected folder.
Listing 8.2 earlier in this chapter shows you how to iterate through the files of the selected
folder and all its subfolders.
Use the ColorDialog and FontDialog controls to prompt users for colors and typefaces.
The Color and Font dialog boxes allow you to prompt users for a color value and a font, respec-
tively. Before showing the corresponding dialog box, set its Color or Font property according
to the current selection, and then call the control’s ShowDialog method.
Master It How will you display color attributes in the Color dialog box when you open
it? How will you display the attributes of the selected text’s font in the Font dialog box when
you open it?
Solution To prompt users to specify a different color for the text on a TextBox control,
execute the following statements:
ColorDialog1.Color = TextBox1.ForeColor

If ColorDialog1.ShowDialog = DialogResult.OK Then
TextBox1.ForeColor = ColorDialog1.Color
End If
To populate the Font dialog box with the font in effect, assign the control’s Font prop-
erty to the FontDialog control’s Font property by using the following statements:
FontDialog1.Font = TextBox1.Font
If FontDialog1.ShowDialog = DialogResult.OK Then
TextBox1.Font = FontDialog1.Font
End If
Petroutsos bapp01.tex V3 - 01/28/2008 5:29pm Page 1011
CHAPTER 8: MORE WINDOWS CONTROLS 1011
Use the RichTextBox control as an advanced text editor to present richly formatted text.
The RichTextBox control is an enhanced TextBox control that can display multiple fonts and
styles, format paragraphs with different styles, and provide a few more advanced text-editing
features. Even if you don’t need the formatting features of this control, you can use it as an
alternative to the TextBox control. At the very least, the RichTextBox control provides more
editing features, a more-useful undo function, and more-flexible search features.
Master It You want to display a document with a title in large, bold type, followed by a
couple of items in regular style. How will you create a document like the following one on a
RichTextBox control?
Document’s Title
Item 1
Description for item 1
Item 2
Description for item 2
Solution To append text to a RichTextBox control, use the AppendText method. This
method accepts a string as an argument and appends it to the control’s contents. The text is
formatted according to the current selection’s font, which you must set accordingly through
the SelectionFont property. To switch to a different font set the SelectionFont again and
call the AppendText method.

Assuming that the form contains a control named RichTextBox1, the following statements
will create a document with multiple formats. In this sample I’m using three different type-
faces for the document.
Dim fntTitle As
New Font(”Verdana”, 12, FontStyle.Bold)
Dim fntItem As
New Font(”Verdana”, 10, FontStyle.Bold)
Dim fntText As
New Font(”Verdana”, 9, FontStyle.Regular)
Editor.SelectionFont = fntTitle
Editor.AppendText(”Document’s Title” & vbCrLf)
Editor.SelectionFont = fntItem
Editor.SelectionIndent = 20
Editor.AppendText(”Item 1” & vbCrLf)
Editor.SelectionFont = fntText
Editor.SelectionIndent = 40
Editor.AppendText(
”Description for item 1” & vbCrLf)
Editor.SelectionFont = fntItem
Editor.SelectionIndent = 20
Editor.AppendText(”Item 2” & vbCrLf)
Editor.SelectionFont = fntText
Editor.SelectionIndent = 40
Editor.AppendText(
”Description for item 2” & vbCrLf)
Petroutsos bapp01.tex V3 - 01/28/2008 5:29pm Page 1012
1012 APPENDIX A THE BOTTOM LINE
Chapter 9: The TreeView and ListView Controls
Create and present hierarchical lists by using the TreeView control. The TreeView control
is used to display a list of hierarchically structured items. Each item in the TreeView con-

trol is represented by a TreeNode object. To access the nodes of the TreeView control, use
the TreeView.Nodes collection. The nodes under a specific node (in other words, the child
nodes) form another collection of Node objects, which you can access by using the expression
TreeView.Nodes(i).Nodes. The basic property of the Node object is the Text property, which
stores the node’s caption. The Node object exposes properties for manipulating its appearance
(its foreground/background color, its font, and so on).
Master It How will you set up a TreeView control with a book’s contents at design time?
Solution Place an instance of the TreeView control on the form and then locate its Nodes
property in the Properties Browser. Click the ellipsis button to open the TreeNode Editor
dialog box, where you can enter root nodes by clicking the Add Root button, and child
nodes under the currently selected node by clicking the Add Child button. The book’s
chapters should be the control’s root nodes, and the sections should be child nodes of those
chapter nodes. If you have nested sections, add them as child nodes of the appropriate node.
While a node is selected in the left pane of the dialog box, you can specify its appearance in
the right pane by setting the font, color, and image-related properties.
Create and present lists of structured items by using the ListView control. The ListView
control stores a collection of ListViewItem objects, the Items collection, and can display them
inseveralmodes,asspecifiedbytheView property. Each ListViewItem object has a Text prop-
erty and the SubItems collection. The subitems are not visible at runtime unless you set the
control’s View property to Details and set up the control’s Columns collection. There must be a
column for each subitem you want to display on the control.
Master It How will you set up a ListView control with three columns to display names,
emails, and phone numbers at design time?
Solution Drop an instance of the ListView control on the form and set its View prop-
erty to Details. Then locate the control’s Columns property in the Properties Browser and
add three columns to the collection through the ColumnHeader Collection Editor dialog
box. Don’t forget to set their headers and their widths for the fields they will display.
To populate the control at design time, locate its Items property in the Properties window
and click the ellipsis button to open the ListViewItem Collection Editor dialog box (see
Figure 9.11). Add a new item by clicking the Add button. When the new item is added to

the list in the left pane of the dialog box, set its Text property to the desired caption. To add
subitems to this item, locate the SubItems property in the ListViewItem Collection Editor
dialog box and click the ellipsis button next to its value. This will open another dialog box,
the ListViewSubItems Collection, where you can add as many subitems under the current
item as you wish. You can also set the appearance of the subitems (their font and color) in
the same dialog box.
Master It How would you populate the same control with the same data at runtime?
Petroutsos bapp01.tex V3 - 01/28/2008 5:29pm Page 1013
CHAPTER 10: BUILDING CUSTOM CLASSES 1013
Solution The following code segment adds two items to the ListView1 control at
runtime:
Dim LItem As New ListViewItem()
LItem.Text = ”Alfred’s Futterkiste”
LItem.SubItems.Add(”Anders Maria”)
LItem.SubItems.Add(”030-0074321”)
LItem.SubItems.Add(”030-0076545”)
LItem.ImageIndex = 0
ListView1.Items.Add(LItem)
LItem = New ListViewItem()
LItem.Text = ”Around the Horn”
LItem.SubItems.Add(”Hardy Thomas”)
LItem.SubItems.Add(”(171) 555-7788”)
LItem.SubItems.Add(”(171) 555-6750”)
LItem.ImageIndex = 0
ListView1.Items.Add(LItem)
Chapter 10: Building Custom Classes
Build your own classes. Classes contain code that executes without interacting with the
user. The class’s code is made up of three distinct segments: the declaration of the private vari-
ables, the property procedures that set or read the values of the private variables, and the meth-
ods, which are implemented as Public subroutines or functions. Only the Public entities (prop-

erties and methods) are accessible by any code outside the class. Optionally, you can imple-
ment events that are fired from within the class’s code. Classes are referenced through vari-
ables of the appropriate type, and applications call the members of the class through these vari-
ables. Every time a method is called, or a property is set or read, the corresponding code in the
class is executed.
Master It How do you implement properties and methods in a custom class?
Solution Any variable declared with the Public access modifier is automatically a prop-
erty. As a class developer, however, you should be able to validate any values assigned
to your class’s properties. To do so, you can implement properties by using a special type
of procedure, the Property procedure, which has two distinct segments: a Set segment
that’s invoked when an application attempts to set a property, and a Get segment that’s
invoked when an application attempts to read a property’s value. The Property has the
following structure:
Private m property As type
Property Property() As type
Get
Property = m
property
Petroutsos bapp01.tex V3 - 01/28/2008 5:29pm Page 1014
1014 APPENDIX A THE BOTTOM LINE
End Get
Set (ByVal value As type)
’ your validation code goes here
’ If validation succeeds, set the local var
m
property = value
End Set
End Property
The local variable m property must be declared with the same type as the property. The
Get segment returns the value of the local variable that stores the property’s value. The Set

segment validates the value passed by the calling application and either rejects it or sets the
local variable to this value.
Master It How would you use a constructor to allow developers to create an instance of
your class and populate it with initial data?
Solution Each class has a constructor, which is called every time a new instance of the
class is created with the New keyword. The constructor is implemented with the New() sub-
routine. To allow users to set certain properties of the class when they instantiate it, create as
many New() subroutines as you need. Each version of the New() subroutine should accept
different arguments. The following sample lets you create objects that represent books,
passing the book’s ISBN and/or title.
Public Sub New(ByVal ISBN As String)
MyBase.New()
Me.ISBN = ISBN
End Sub
Public Sub New(ByVal ISBN As String,
ByVal Title As String)
MyBase.New()
Me.ISBN = ISBN
Me.Title = Title
End Sub
Use custom classes in your projects. To use a custom class in your project, you must add
to the project a reference to the class you want to use. If the class belongs to the same project,
you don’t have to do anything. If the class belongs to another project, you must right-click the
project’s name in the Solution Explorer and select Add Reference from the shortcut menu. In
the Add Reference dialog box that appears, switch to the Browse tab and locate the DLL file
with the class’s implementation (it will be a DLL file in the project’s Bin folder). Select the name
of this file and click OK to add the reference and close the dialog box.
Master It How will you call the two constructors of the preceding Master It sections in an
application that uses the custom class to represent books?
Solution There are three ways to create new Book objects:

1. Call the parameterless constructor to create books without ISBNs or titles:
Dim book As New Book
Petroutsos bapp01.tex V3 - 01/28/2008 5:29pm Page 1015
CHAPTER 11: WORKING WITH OBJECTS 1015
2. Call the New() constructor, passing the book’s ISBN as a parameter:
Dim book As New Book(”9780213324543”)
3. Call the New() constructor, passing the book’s ISBN and title:
Dim book As New Book(”9780213324543”,
”Mastering Visual Studio”)
Customize the usual operators for your classes. Overloading is a common theme in coding
classes (or plain procedures) with Visual Basic. In addition to overloading methods, you can
overload operators. In other words, you can define the rules for adding or subtracting two
custom objects, if this makes sense for your application.
Master It When should you overload operators in a custom class, and why?
Solution Sometimes it makes sense to apply common operations, such as the addition
and subtraction operations, to instances of a custom class. However, the addition oper-
ator doesn’t work with custom classes. To redefine the addition operator so that it will
add two instances of your custom class, you must override the addition operator with an
implementation that adds two instances of a custom class. The following is the signature
of a function that overloads the addition operator:
Public Shared Operator + (
ByVal object1 As customType,
ByVal object2 As customType)
As customType
Dim result As New customType
’ Insert the code to ”add” the two
’ arguments and store the result to
’ the result variable and return it.
Return result
End Operator

The function that overrides the addition operator accepts two arguments, which are the
two values to be added, and returns a value of the same type. The operator is usually over-
loaded, because you may wish to add an instance of the custom class to one of the built-in
data types or objects. In addition to the usual math operators, you should also consider
overloading some basic functions that act like operators, especially the CType() function.
Chapter 11: Working with Objects
Use inheritance. Inheritance, which is the true power behind OOP, allows you to create
new classes that encapsulate the functionality of existing classes without editing their code.
To inherit from an existing class, use the Inherits statement, which brings the entire class into
your class.
Petroutsos bapp01.tex V3 - 01/28/2008 5:29pm Page 1016
1016 APPENDIX A THE BOTTOM LINE
Master It Explain the inheritance-related attributes of a class’s members.
Solution Any class can be inherited by default. However, you can prevent developers
from inheriting your class with the NonInheritable keyword, or create an abstract class
with the MustInherit attribute. Classes marked with this attribute can’t be used on their
own; they must be inherited by another class. The parent class’s members can be optionally
overridden if they’re marked with the Overridable keyword. To prevent derived classes
from overriding specific members, use the NotOverridable attribute. Finally, methods that
override the equivalent methods of the base class must be prefixed with the
Overrides keyword.
Use polymorphism. Polymorphism is the ability to write members that are common to a
number of classes but behave differently, depending on the specific class to which they apply.
Polymorphism is a great way of abstracting implementation details and delegating the imple-
mentation of methods with very specific functionality to the derived classes.
Master It The parent class Person represents parties, and it exposes the GetBalance
method, which returns the outstanding balance of a person. The Customer and Supplier
derived classes implement the GetBalance method differently. How will you use this
method to find out the balance of a customer and/or supplier?
Solution If you have Customer or Supplier object, you can call the GetBalance method

directly. If you have a collection of objects of both types, you must cast them to their parent
type and then call the GetBalance method.
Chapter 12: Building Custom Windows Controls
Extend the functionality of existing Windows Forms controls with inheritance. The sim-
plest type of control you can build is one that inherits an existing control. The inherited control
includes all the functionality of the original control plus some extra functionality that’s specific
to an application and that you implement with custom code.
Master It Describe the process of designing an inherited custom control.
Solution To enhance an existing Windows Forms control, insert an Inherits statement
with the name of the control you want to enhance in the project’s Designer.vb file. The
inherited control’s interface can’t be altered; it’s determined by parent control. However,
you can implement custom properties and methods, react to events received by the parent
control, or raise custom events from within your new control.
The process of implementing custom properties and methods is identical to building custom
classes. The control’s properties, however, can be prefixed by a number of useful attributes,
such as the <Category> and <Description> attributes, which determine the category
of the Properties window where the property will appear, and the control’s description
that will be shown in the Properties window when the custom property is selected.
Build compound controls that combine multiple existing controls. A compound control
provides a visible interface that combines multiple Windows controls. As a result, this type
of control doesn’t inherit the functionality of any specific control; you must expose its prop-
erties by providing your own code. The UserControl object, on which the compound control
Petroutsos bapp01.tex V3 - 01/28/2008 5:29pm Page 1017
CHAPTER 12: BUILDING CUSTOM WINDOWS CONTROLS 1017
is based, already exposes a large number of members, including some fairly advanced ones
such as the Anchoring and Docking properties, and the usual mouse and key events.
Master It How will you map certain members of a constituent control to custom members
of the compound control?
Solution If the member is a property, you simply return the constituent control’s prop-
erty value in the Get section of the Property procedure, and set the constituent control’s

property to the specified value in the Set section of the same procedure. The following
Property procedure maps the WordWrap property of the TextBox1 constituent control to
the TextWrap property of the custom compound control:
Public Property TextWrap() As Boolean
Get
Return TextBox1.WordWrap
End Get
Set(ByVal value As Boolean)
TextBox1.WordWrap = value
End Set
End Property
If the member is a method, you just call it from within one of the compound control’s meth-
ods. To map the ResetText method of the TextBox constituent control to the Reset method
of the compound control, add the following method definition:
Public Sub Reset()
TextBox1.ResetText
End Sub
Build custom controls from scratch. User-drawn controls are the most flexible custom con-
trols,becauseyou’reinchargeofthecontrol’s functionality and appearance. Of course, you
have to implement all the functionality of the control from within your code, so it takes sub-
stantial programming effort to create user-drawn custom controls.
Master It Describe the process of developing a user-drawn custom control.
Solution Because you are responsible for updating the control’s visible area from within
your code, you must provide the code that redraws the control’s surface and insert it in the
UserControl object’s Paint event handler. In drawing the control’s surface, you must take
into consideration the settings of the control’s properties.
The e argument of the Paint event handler exposes the Graphics property, which you must
use from within your code to draw on the control’s surface. You can use any of the drawing
methods you’d use to create shapes, gradients, and text on a Form or PictureBox control.
Because custom controls aren’t redrawn by default when they’re resized, you must also

insert the following statement in the control’s Load event handler:
Me.SetStyle(ControlStyles.ResizeRedraw, True)
Petroutsos bapp01.tex V3 - 01/28/2008 5:29pm Page 1018
1018 APPENDIX A THE BOTTOM LINE
If the control’s appearance should be different at design time than at runtime, use the
Me.DesignMode property to distinguish between runtime and design time.
Customize the rendering of items in a ListBox control. To create an owner-drawn list
control, you must set the DrawMode property to a member of the DrawMode enumeration and
program two events: MeasureItem and DrawItem.
Master It Outline the process of creating a ListBox control that wraps the contents of
lengthy items.
Solution By default, all items in a ListBox control have the same height, which is the
height of a single line of text in the control’s font. To display selected items in cells of
varying height, do the following:
1. Set the control’s DrawMode property to OwnerDrawnVariable
2. In the control’s MeasureItem event handler, which is invoked every time the control is
about to display an item, insert the statements that calculate the desired height of the
current item’s cell and set the e.Height property. You will most likely call the Measure-
String method of the control’s Graphics object to retrieve the height of the item from its
text.
3. In the control’s DrawItem event handler, which displays the current item, insert the
statements to print the item in a cell with the dimensions calculated in step 2 via the
DrawString method. These dimensions are given by the Bounds property of the event
handler’s e argument.
Chapter 13: Handling Strings, Characters, and Dates
Use the Char data type to handle characters. The Char data type, which is implemented
with the Char class, exposes methods for handling individual characters (IsLetter, IsDigit,
IsSymbol, and so on). We use the methods of the Char class to manipulate users’ keystrokes as
they happen in certain controls (mostly the TextBox control) and to provide immediate
feedback.

Master It You want to develop an interface that contains several TextBox controls that
accept numeric data. How will you intercept the user’s keystrokes and reject any characters
that are not numeric?
Solution You must program the control’s KeyPress event handler, which reports the
character that was pressed. The following event handler rejects any non-numeric characters
entered in the TextBox1 control:
Private Sub TextBox1 KeyPress(
ByVal sender As Object, ByVal e As
System.Windows.Forms.KeyPressEventArgs)
Handles TextBox1.KeyPress
Dim c As Char
c = e.KeyChar
If Not (Char.IsDigit(c) or
Char.IsControl(c)) Then
e.Handled = True
End If
End Sub
Petroutsos bapp01.tex V3 - 01/28/2008 5:29pm Page 1019
CHAPTER 13: HANDLING STRINGS, CHARACTERS, AND DATES 1019
Actually, you learned in the previous chapter how to implement custom TextBox controls
that inherit existing Windows controls. You can build a custom TextBox control that accepts
numeric data along the lines of the design of the FocusedTextBox custom control, discussed
in the preceding chapter.
Use the String data type to handle strings. The String data type represents strings and
exposes members for manipulating them. Most of the String class’s methods are equivalent to
the string-manipulation methods of Visual Basic. The members of the String class are shared:
they do not modify the string to which they’re applied. Instead, they return a new string.
Master It How would you extract the individual words from a large text document?
Solution Start by setting up an array with all possible delimiters. The delimiters array
should contain all symbols that separate words, including parentheses, brackets, and so

on. Here’s the definition of such an array that works even with program listings:
Dim delimiters() As Char =
{” ”c, ”.”c, ”,”c, ”!”c, ”;”c, ”:”c,
”(”c, ”)”c, ”*”c, ””””c, ”;”c, ”{”c,
”}”c, Convert.ToChar(vbTab),
Convert.ToChar(vbCr),
Convert.ToChar(vbLf)}
Notice that vbTab, vbCr,andvbLf constants are strings, and they must be converted implic-
itly into characters.
Then pass this array as an argument to the Split method and retrieve the method’s results
in an array of strings. These are the words extracted from the text by the Split method:
Dim words() As String
words = text.Split(delimiters)
Dim word As String
For Each word In words
If word.Length > 0 Then
’ process current word
End If
Next
Use the StringBuilder class to manipulate large or dynamic strings. The StringBuilder
class is very efficient at manipulating long strings, but it doesn’t provide as many methods
for handling strings. The StringBuilder class provides a few methods to insert, delete, and
replace characters within a string. Unlike the equivalent methods of the String class, these
methods act directly on the string stored in the current instance of the StringBuilder class.
Master It Assuming that you have populated a ListView control with thousands of lines
of data from a database, how will you implement a function that copies all the data to
the Clipboard?
Solution To copy the ListView control’s data, you must create a long string that contains
tab-delimited strings and then copy it to the Clipboard. Each cell’s value must be converted
to a string and then appended to a StringBuilder variable. Consecutive rows will be sepa-

rated by a carriage return/line feed character. Start by declaring a StringBuilder variable:
Dim SB As New System.Text.StringBuilder
Petroutsos bapp01.tex V3 - 01/28/2008 5:29pm Page 1020
1020 APPENDIX A THE BOTTOM LINE
Then write a loop that iterates through the items of the ListView control:
Dim LI As ListViewItem
For Each LI In ListView1.Items
’ append current row’s cell values to SB
SB.Append(vbcrlf)
Next
In the loop’s body, insert another loop to iterate through the subitems of the LI item:
Dim LI As ListViewItem
For Each LI In ListView1.Items
Dim SLI As ListViewItem.ListViewSubItem
For Each SLI In LI.SubItems
SB.Append(SLI.Text & vbTab)
Next
SB.Remove(SB.Length - 1, 1) ’ remove last tab
SB.Append(vbCrLf)
Next
And finally, put the string to the Clipboard by using the following statement:
Clipboard.SetText(SB.ToString)
One of this chapter’s projects is the SBDemo project, which populates a ListView control
with data (it’s the same few rows repeated over and over). Click the Populate List button
several times to create a long list of items and then one of the other two buttons on the form
that copy the data to the Clipboard either through a String or through a StringBuilder vari-
able. As you will see, the StringBuilder class runs circles around the String data type when it
comes to manipulating dynamic strings.
Petroutsos bapp01.tex V3 - 01/28/2008 5:29pm Page 1021
CHAPTER 14: STORING DATA IN COLLECTIONS 1021

Use the DateTime and TimeSpan classes to handle dates and times. The Date class
represents dates and time, and it exposes many useful shared methods (such as the IsLeap
method, which returns True if the year passed to the method as an argument is leap; the
DaysInMonth method; and so on). It also exposes many instance methods (such as AddYears,
AddDays, AddHours, and so on) for adding time intervals to the current instance of the Date
class, as well as many options for formatting date and time values.
The TimeSpan class represents time intervals — from milliseconds to days — with the From-
Days, FromHours, and even FromMilliseconds methods. The difference between two date
variables is a TimeSpan value, and you can convert this value to various time units by using
methods such as TotalDays, TotalHours, TotalMilliseconds, and so on. You can also add a
TimeSpan object to a date variable to obtain another date variable.
Master It How will you use the TimeSpan class to accurately time an operation?
Solution To time an operation, you must create a DateTime variable and set it to the cur-
rent date and time right before the statements you want to execute:
Dim T1 As DateTime = Now
Right after the statements you want to execute, create a new TimeSpan object that represents
the time it took the statements to complete. This duration is the difference between the cur-
rent time and the time value stored in the variable T1:
Dim duration As New TimeSpan
duration = Now.Subtract(T1)
The duration variable is a time interval, and you can use the methods of the TimeSpan class
to express this interval in various units: duration.MilliSeconds, Duration.Seconds,and
so on.
Chapter 14: Storing Data in Collections
Make the most of arrays. The simplest method of storing sets of data is to use arrays. They’re
very efficient and they provide methods to perform advanced operations such as sorting and
searching their elements. Use the Sort method of the Array class to sort an array’s elements. To
search for an element in an array, use the IndexOf and LastIndexOf methods, or the Binary-
Search method if the array is sorted. The BinarySearch method always returns an element’s
index, which is a positive value for exact matches and a negative value for near matches.

Master It Explain how you can search an array and find exact and near matches.
Solution The most efficient method of searching arrays is the BinarySearch method,
which requires that the array is sorted. The simplest form of the BinarySearch method is
the following:
Dim idx As Integer
idx = System.Array.BinarySearch(arrayName, object)
Petroutsos bapp01.tex V3 - 01/28/2008 5:29pm Page 1022
1022 APPENDIX A THE BOTTOM LINE
The BinarySearch method returns an integer value, which is the index of the object you’re
searching for in the array. If the object argument is not found, the method returns a nega-
tive value, which is the negative of the index of the next larger item minus one. The
following statements return an exact or near match for the word srchWord in the words
array:
Dim wordIndex As Integer =
Array.BinarySearch(words, srchWord)
If wordIndex >= 0 Then ’ exact match!
MsgBox(”An exact match was found for ” &
” at index ” & wordIndex.ToString)
Else ’ Near match
MsgBox(”The nearest match is the word ” &
words(-wordIndex - 1) &
” at ” & (-wordIndex - 1).ToString)
End If
Store data in specialized collections such as ArrayLists and HashTables. In addition to
arrays, the Framework provides collections, which are dynamic data structures. The most
commonly used collections are the ArrayList and the HashTable. ArrayLists are similar to
arrays, but they’re dynamic structures. ArrayLists store lists of items, whereas HashTables
store key-value pairs and allow you to access their elements via a key. You can add elements
by using the Add method and remove existing elements by using the Remove and RemoveAt
methods.

HashTables provide the ContainsKey and ContainsValue methods to find out whether the
collection already contains a specific key or value, and the GetKeys and GetValues methods to
retrieve all the keys and values from the collection, respectively.
Master It How will you populate a HashTable with a few pairs of keys/values and then
iterate though the collection’s items?
Solution To populate the HashTable, call its Add method, passing as an argument the
item’s key and value:
Dim HTable As New HashTable
HTable.Add(”key1”, item1)
HTable.Add(”key2”, item2)
To iterate through the items of a HashTable collection, you must first extract all the keys
and then use them to access the collection’s elements. The following code segment prints the
keys and values in the HTable variable:
Dim element, key As Object
For Each key In HTable.keys
element = HTable.Item(key)
Debug.WriteLine(”Item type = ” element.GetType.ToString
Debug.WriteLine(”Key= ” & Key.ToString)
Denug.WritrLine(”Value= ” & element.ToString)
Next
Petroutsos bapp01.tex V3 - 01/28/2008 5:29pm Page 1023
CHAPTER 14: STORING DATA IN COLLECTIONS 1023
Sort and search collections. Collections provide the Sort method for sorting their
items and several methods to locate items: IndexOf, LastIndexOf,andBinarySearch.Both
sort and search operations are based on comparisons, and the Framework knows how to com-
pare values types only (Integers, Strings, and the other primitive data types). If a collection con-
tains objects, you must provide a custom function that knows how to compare two objects of
the same type.
Master It How do you specify a custom comparer function for a collection that contains
Rectangle objects?

Solution First you must decide how to compare two Rectangle objects. Let’s consider two
rectangles equal if their perimeters are equal. To implement a custom comparer, you must
write a class that implements the IComparer interface:
Class RectangleComparer : Implements IComparer
Public Function Compare(
ByVal o1 As Object, ByVal o2 As Object)
As Integer Implements IComparer.Compare
Dim R1, R2 As Rectangle
Try
R1 = CType(o1, Rectangle)
R2 = CType(o2, Rectangle)
Catch compareException As system.Exception
Throw (compareException)
Exit Function
End Try
Dim perim1 As Integer =
2 * (R1.Width+R1.Height)
Dim perim2 As Integer =
2 * (R2.Width+R2.Height)
If perim1 < perim2 Then
Return -1
Else
If perim1 > perim2 Then
Return 1
Else
Return 0
End If
End If
End Function
End Class

The following statement sorts the items of the Rects ArrayList collection, assuming that it
contains only Rectangles:
Rects.Sort(New RectangleComparer)
To call the BinarySearch method for the same ArrayList, use the following statement:
Rects.BinarySearch(
New Rectangle(0, 0, 33, 33), comparer)
Petroutsos bapp01.tex V3 - 01/28/2008 5:29pm Page 1024
1024 APPENDIX A THE BOTTOM LINE
Chapter 15: Accessing Folders and Files
Handle Files with the My object. The simplest method of saving data to a file is to call one of
the WriteAllBytes or WriteAllText methods of the My.Computer.FileSystem object. You can
also use the IO namespace to set up a Writer object to send data to a file, and a Reader object to
read data from the file.
Master It Show the statements that save a TextBox control’s contents to a file and the
statements that reload the same control from the data file. Use the My.Computer.FileSystem
component.
Solution The following statement saves the control’s Text property to a file whose path is
stored in the filename variable. Prompt users with the Open dialog box control for the path
of the file and use it in your code.
My.Computer.FileSystem.WriteAllText(
fileName, TextBox1.Text, True)
To read the data back and place it in the TextBox1 control again, use the following
statement:
TextBox1.Text = My.Computer.FileSystem.ReadAllText(fileName)
Write data to a file with the IO namespace To send data to a file you must set up a File-
Stream object, which is a channel between the application and the file. To send data to a file,
create a StreamWriter or BinaryWriter object on the appropriate FileStream object. Likewise, to
read from a file, create a StreamReader or BinaryReader on the appropriate FileStream object.
To send data to a file, use the Write and WriteString methods of the appropriate Stream-
Writer object. To read data from the file, use the Read, ReadBlock, ReadLine,andReadToEnd

methods of the StreamReader object.
Master It Write the contents of a TextBox control to a file using the methods of the IO
namespace.
Solution Begin by setting up a FileStream object to connect your application to a data file.
Then create a StreamWriter object on top of the FileStream object and use the Write method
to send data to the file:
Dim FS As FileStream
FS = New FileStream(fileName, FileMode.Create)
Dim SW As StreamWriter(FS)
SW.Write(TextBox1.Text)
SW.Close
FS.Close
To read the data back and reload the TextBox control, set up an identical FileStream object,
then create a StreamReader object on top of it, and finally call the ReadToEnd method:
Dim FS As New FileStream(fileName,
System.IO.FileMode.OpenOrCreate,
System.IO.FileAccess.Write)
Dim SR As New StreamReader(FS)

×