
- •Using Your Sybex Electronic Book
- •Acknowledgments
- •Contents at a Glance
- •Introduction
- •Who Should Read This Book?
- •How About the Advanced Topics?
- •The Structure of the Book
- •How to Reach the Author
- •The Integrated Development Environment
- •The Start Page
- •Project Types
- •Your First VB Application
- •Making the Application More Robust
- •Making the Application More User-Friendly
- •The IDE Components
- •The IDE Menu
- •The Toolbox Window
- •The Solution Explorer
- •The Properties Window
- •The Output Window
- •The Command Window
- •The Task List Window
- •Environment Options
- •A Few Common Properties
- •A Few Common Events
- •A Few Common Methods
- •Building a Console Application
- •Summary
- •Building a Loan Calculator
- •How the Loan Application Works
- •Designing the User Interface
- •Programming the Loan Application
- •Validating the Data
- •Building a Math Calculator
- •Designing the User Interface
- •Programming the MathCalculator App
- •Adding More Features
- •Exception Handling
- •Taking the LoanCalculator to the Web
- •Working with Multiple Forms
- •Working with Multiple Projects
- •Executable Files
- •Distributing an Application
- •VB.NET at Work: Creating a Windows Installer
- •Finishing the Windows Installer
- •Running the Windows Installer
- •Verifying the Installation
- •Summary
- •Variables
- •Declaring Variables
- •Types of Variables
- •Converting Variable Types
- •User-Defined Data Types
- •Examining Variable Types
- •Why Declare Variables?
- •A Variable’s Scope
- •The Lifetime of a Variable
- •Constants
- •Arrays
- •Declaring Arrays
- •Initializing Arrays
- •Array Limits
- •Multidimensional Arrays
- •Dynamic Arrays
- •Arrays of Arrays
- •Variables as Objects
- •So, What’s an Object?
- •Formatting Numbers
- •Formatting Dates
- •Flow-Control Statements
- •Test Structures
- •Loop Structures
- •Nested Control Structures
- •The Exit Statement
- •Summary
- •Modular Coding
- •Subroutines
- •Functions
- •Arguments
- •Argument-Passing Mechanisms
- •Event-Handler Arguments
- •Passing an Unknown Number of Arguments
- •Named Arguments
- •More Types of Function Return Values
- •Overloading Functions
- •Summary
- •The Appearance of Forms
- •Properties of the Form Control
- •Placing Controls on Forms
- •Setting the TabOrder
- •VB.NET at Work: The Contacts Project
- •Anchoring and Docking
- •Loading and Showing Forms
- •The Startup Form
- •Controlling One Form from within Another
- •Forms vs. Dialog Boxes
- •VB.NET at Work: The MultipleForms Project
- •Designing Menus
- •The Menu Editor
- •Manipulating Menus at Runtime
- •Building Dynamic Forms at Runtime
- •The Form.Controls Collection
- •VB.NET at Work: The DynamicForm Project
- •Creating Event Handlers at Runtime
- •Summary
- •The TextBox Control
- •Basic Properties
- •Text-Manipulation Properties
- •Text-Selection Properties
- •Text-Selection Methods
- •Undoing Edits
- •VB.NET at Work: The TextPad Project
- •Capturing Keystrokes
- •The ListBox, CheckedListBox, and ComboBox Controls
- •Basic Properties
- •The Items Collection
- •VB.NET at Work: The ListDemo Project
- •Searching
- •The ComboBox Control
- •The ScrollBar and TrackBar Controls
- •The ScrollBar Control
- •The TrackBar Control
- •Summary
- •The Common Dialog Controls
- •Using the Common Dialog Controls
- •The Color Dialog Box
- •The Font Dialog Box
- •The Open and Save As Dialog Boxes
- •The Print Dialog Box
- •The RichTextBox Control
- •The RTF Language
- •Methods
- •Advanced Editing Features
- •Cutting and Pasting
- •Searching in a RichTextBox Control
- •Formatting URLs
- •VB.NET at Work: The RTFPad Project
- •Summary
- •What Is a Class?
- •Building the Minimal Class
- •Adding Code to the Minimal Class
- •Property Procedures
- •Customizing Default Members
- •Custom Enumerations
- •Using the SimpleClass in Other Projects
- •Firing Events
- •Shared Properties
- •Parsing a Filename String
- •Reusing the StringTools Class
- •Encapsulation and Abstraction
- •Inheritance
- •Inheriting Existing Classes
- •Polymorphism
- •The Shape Class
- •Object Constructors and Destructors
- •Instance and Shared Methods
- •Who Can Inherit What?
- •Parent Class Keywords
- •Derived Class Keyword
- •Parent Class Member Keywords
- •Derived Class Member Keyword
- •MyBase and MyClass
- •Summary
- •On Designing Windows Controls
- •Enhancing Existing Controls
- •Building the FocusedTextBox Control
- •Building Compound Controls
- •VB.NET at Work: The ColorEdit Control
- •VB.NET at Work: The Label3D Control
- •Raising Events
- •Using the Custom Control in Other Projects
- •VB.NET at Work: The Alarm Control
- •Designing Irregularly Shaped Controls
- •Designing Owner-Drawn Menus
- •Designing Owner-Drawn ListBox Controls
- •Using ActiveX Controls
- •Summary
- •Programming Word
- •Objects That Represent Text
- •The Documents Collection and the Document Object
- •Spell-Checking Documents
- •Programming Excel
- •The Worksheets Collection and the Worksheet Object
- •The Range Object
- •Using Excel as a Math Parser
- •Programming Outlook
- •Retrieving Information
- •Recursive Scanning of the Contacts Folder
- •Summary
- •Advanced Array Topics
- •Sorting Arrays
- •Searching Arrays
- •Other Array Operations
- •Array Limitations
- •The ArrayList Collection
- •Creating an ArrayList
- •Adding and Removing Items
- •The HashTable Collection
- •VB.NET at Work: The WordFrequencies Project
- •The SortedList Class
- •The IEnumerator and IComparer Interfaces
- •Enumerating Collections
- •Custom Sorting
- •Custom Sorting of a SortedList
- •The Serialization Class
- •Serializing Individual Objects
- •Serializing a Collection
- •Deserializing Objects
- •Summary
- •Handling Strings and Characters
- •The Char Class
- •The String Class
- •The StringBuilder Class
- •VB.NET at Work: The StringReversal Project
- •VB.NET at Work: The CountWords Project
- •Handling Dates
- •The DateTime Class
- •The TimeSpan Class
- •VB.NET at Work: Timing Operations
- •Summary
- •Accessing Folders and Files
- •The Directory Class
- •The File Class
- •The DirectoryInfo Class
- •The FileInfo Class
- •The Path Class
- •VB.NET at Work: The CustomExplorer Project
- •Accessing Files
- •The FileStream Object
- •The StreamWriter Object
- •The StreamReader Object
- •Sending Data to a File
- •The BinaryWriter Object
- •The BinaryReader Object
- •VB.NET at Work: The RecordSave Project
- •The FileSystemWatcher Component
- •Properties
- •Events
- •VB.NET at Work: The FileSystemWatcher Project
- •Summary
- •Displaying Images
- •The Image Object
- •Exchanging Images through the Clipboard
- •Drawing with GDI+
- •The Basic Drawing Objects
- •Drawing Shapes
- •Drawing Methods
- •Gradients
- •Coordinate Transformations
- •Specifying Transformations
- •VB.NET at Work: Plotting Functions
- •Bitmaps
- •Specifying Colors
- •Defining Colors
- •Processing Bitmaps
- •Summary
- •The Printing Objects
- •PrintDocument
- •PrintDialog
- •PageSetupDialog
- •PrintPreviewDialog
- •PrintPreviewControl
- •Printer and Page Properties
- •Page Geometry
- •Printing Examples
- •Printing Tabular Data
- •Printing Plain Text
- •Printing Bitmaps
- •Using the PrintPreviewControl
- •Summary
- •Examining the Advanced Controls
- •How Tree Structures Work
- •The ImageList Control
- •The TreeView Control
- •Adding New Items at Design Time
- •Adding New Items at Runtime
- •Assigning Images to Nodes
- •Scanning the TreeView Control
- •The ListView Control
- •The Columns Collection
- •The ListItem Object
- •The Items Collection
- •The SubItems Collection
- •Summary
- •Types of Errors
- •Design-Time Errors
- •Runtime Errors
- •Logic Errors
- •Exceptions and Structured Exception Handling
- •Studying an Exception
- •Getting a Handle on this Exception
- •Finally (!)
- •Customizing Exception Handling
- •Throwing Your Own Exceptions
- •Debugging
- •Breakpoints
- •Stepping Through
- •The Local and Watch Windows
- •Summary
- •Basic Concepts
- •Recursion in Real Life
- •A Simple Example
- •Recursion by Mistake
- •Scanning Folders Recursively
- •Describing a Recursive Procedure
- •Translating the Description to Code
- •The Stack Mechanism
- •Stack Defined
- •Recursive Programming and the Stack
- •Passing Arguments through the Stack
- •Special Issues in Recursive Programming
- •Knowing When to Use Recursive Programming
- •Summary
- •MDI Applications: The Basics
- •Building an MDI Application
- •Built-In Capabilities of MDI Applications
- •Accessing Child Forms
- •Ending an MDI Application
- •A Scrollable PictureBox
- •Summary
- •What Is a Database?
- •Relational Databases
- •Exploring the Northwind Database
- •Exploring the Pubs Database
- •Understanding Relations
- •The Server Explorer
- •Working with Tables
- •Relationships, Indices, and Constraints
- •Structured Query Language
- •Executing SQL Statements
- •Selection Queries
- •Calculated Fields
- •SQL Joins
- •Action Queries
- •The Query Builder
- •The Query Builder Interface
- •SQL at Work: Calculating Sums
- •SQL at Work: Counting Rows
- •Limiting the Selection
- •Parameterized Queries
- •Calculated Columns
- •Specifying Left, Right, and Inner Joins
- •Stored Procedures
- •Summary
- •How About XML?
- •Creating a DataSet
- •The DataGrid Control
- •Data Binding
- •VB.NET at Work: The ViewEditCustomers Project
- •Binding Complex Controls
- •Programming the DataAdapter Object
- •The Command Objects
- •The Command and DataReader Objects
- •VB.NET at Work: The DataReader Project
- •VB.NET at Work: The StoredProcedure Project
- •Summary
- •The Structure of a DataSet
- •Navigating the Tables of a DataSet
- •Updating DataSets
- •The DataForm Wizard
- •Handling Identity Fields
- •Transactions
- •Performing Update Operations
- •Updating Tables Manually
- •Building and Using Custom DataSets
- •Summary
- •An HTML Primer
- •HTML Code Elements
- •Server-Client Interaction
- •The Structure of HTML Documents
- •URLs and Hyperlinks
- •The Basic HTML Tags
- •Inserting Graphics
- •Tables
- •Forms and Controls
- •Processing Requests on the Server
- •Building a Web Application
- •Interacting with a Web Application
- •Maintaining State
- •The Web Controls
- •The ASP.NET Objects
- •The Page Object
- •The Response Object
- •The Request Object
- •The Server Object
- •Using Cookies
- •Handling Multiple Forms in Web Applications
- •Summary
- •The Data-Bound Web Controls
- •Simple Data Binding
- •Binding to DataSets
- •Is It a Grid, or a Table?
- •Getting Orders on the Web
- •The Forms of the ProductSearch Application
- •Paging Large DataSets
- •Customizing the Appearance of the DataGrid Control
- •Programming the Select Button
- •Summary
- •How to Serve the Web
- •Building a Web Service
- •Consuming the Web Service
- •Maintaining State in Web Services
- •A Data-Driven Web Service
- •Consuming the Products Web Service in VB
- •Summary

THE SCROLLBAR AND TRACKBAR CONTROLS 283
Listing 6.17: Programming the ScrollBar Control’s Scroll Event
Private Sub redBar_Scroll(ByVal sender As System.Object, _
ByVal e As System.Windows.Forms.ScrollEventArgs) Handles redBar.Scroll If e.Type = ScrollEventType.EndScroll Then ColorBox1()
End Sub
Private Sub redBar_ValueChanged(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles redBar.ValueChanged ColorBox2()
End Sub
The ColorBox1() and ColorBox2() subroutines update the color of the two PictureBox controls and their listings are shown in Listing 6.18. The code creates a new color value based on the values of the three scroll bars and uses it to set the BackColor property of the appropriate control.
Listing 6.18: Updating the Color of the Two TextBox Controls
Sub ColorBox1() Dim clr As Color
clr = Color.FromARGB(redBar.Value, greenBar.Value, blueBar.Value) PictureBox1.BackColor = clr
End Sub
Sub ColorBox2() Dim clr As Color
clr = Color.FromARGB(redBar.Value, greenBar.Value, blueBar.Value) PictureBox2.BackColor = clr
End Sub
The TrackBar Control
The TrackBar control is similar to the ScrollBar control, but it lacks the granularity of ScrollBar. Suppose you want the user of an application to supply a value in a specific range, such as the speed of a moving object. Moreover, you don’t want to allow extreme precision; you need only a few settings, such as slow, fast, and very fast. A TrackBar control with just a few stops, such as the one shown in Figure 6.12, will suffice. The user can set the control’s value by sliding the indicator or by clicking on either side of the indicator.
Note Granularity is how specific you want to be in measuring. In measuring distances between towns, a granularity of a mile is often adequate. In measuring (or specifying) the dimensions of a building, the granularity could be on the order of a foot or an inch. The TrackBar control lets you set the type of granularity that’s necessary for your application.
Similar to the ScrollBar control, SmallChange and LargeChange properties are available. SmallChange is the smallest increment by which the Slider value can change. The user can only change the slider by the SmallChange value by sliding the indicator (unlike the ScrollBar control, there are no arrows at the two ends of the Slider control). To change the Slider’s value by LargeChange, the user can click on either side of the indicator.
Copyright ©2002 SYBEX, Inc., Alameda, CA |
www.sybex.com |

284 Chapter 6 BASIC WINDOWS CONTROLS
VB.NET at Work: The Inches Project
Figure 6.15 demonstrates a typical use of the TrackBar control. The form in the figure is an element of a program’s user interface that lets the user specify a distance between 0 and 10 inches in increments of 0.2 inches. As the user slides the indicator, the current value displays on a Label control above the TrackBar. If you open the Inches application, you’ll notice that there are more stops than there are tick marks on the control. This is made possible with the TickFrequency property, which determines the frequency of the visible tick marks.
Figure 6.15
This TrackBar control lets users specify a distance.
You may specify that the control has 50 stops (divisions) but that only 10 of them will be visible. The user can, however, position the indicator on any of the 40 invisible tick marks. You can think of the visible marks as the major tick marks and the invisible ones as the minor tick marks. If the TickFrequency property is 5, only every fifth mark will be visible. The slider’s indicator, however, will stop at all tick marks.
Tip When using the TrackBar control on your interfaces, you should set the TickFrequency property to a value that helps the user select the desired setting. Too many tick marks are confusing and difficult to read. Without tick marks, the control isn’t of much help. You might also consider placing a few labels to indicate the value of selected tick marks, as I have done in this example.
The properties of the TrackBar control in the Inches application are as follows:
Minimum = 0
Maximum = 50
SmallChange = 1
LargeChange = 5
TickFrequency = 5
The TrackBar needs to cover a range of 10 inches in increments of 0.2 inches. If you set the SmallChange property to 1, you have to set LargeChange to 10 (there’s a total of 10 intervals of 0.2 inches in 10 inches). Moreover, the TickFrequency is set to 5, so there will be a total of 10 divisions in every inch. The numbers below the tick marks were placed there with properly aligned Label controls.
The label at the bottom needs to be updated as the TrackBar’s value changes. This is signaled to the application with the Change event, which occurs every time the value of the control changes, either through scrolling or from within your code. The ValueChanged event handler of the TrackBar control is shown next:
Private Sub TrackBar1_ValueChanged(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles TrackBar1.ValueChanged lblInches.Text = “Length in inches = “ & Format(TrackBar1.Value / 5, “#.00”)
End Sub
Copyright ©2002 SYBEX, Inc., Alameda, CA |
www.sybex.com |

THE SCROLLBAR AND TRACKBAR CONTROLS 285
The Label controls below the tick marks can also be used to set the value of the control. Every time you click one of the labels, the following statement sets the TrackBar control’s value. Notice that all the Label controls’ Click events are handled by a common handler:
Private Sub Label_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Label1.Click, Label2.Click, _ Label3.Click, Label4.Click, Label5.Click, Label6.Click, _ Label7.Click, Label8.Click, Label9.Click
TrackBar1.Value = sender.text * 5 End Sub
VB.NET at Work: The TextMargin Project
To see the TrackBar control in use, let’s review a segment of another application, the RTFPad application, which will be covered in Chapter 7. The Form shown in Figure 6.16 contains a RichTextBox control and two sliders. The RichTextBox control will be explained in Chapter 7. All you need to know about the control to follow the code is that the RichTextBox control is similar to a TextBox control, but provides many more editing and formatting options. Two of the control’s properties we’ll use in this example are the SelectionIndent and SelectionHangingIndent properties, and their functions are as follows:
Figure 6.16
The two TrackBar controls let the user format the paragraphs in
a RichTextBox control.
SelectionIndent Specifies the amount by which the currently selected paragraphs are indented from the left side of the control.
SelectionHangingIndent Specifies the amount of the hanging indentation (that is, the indentation of all paragraph lines after the first line).
The two TrackBar controls above the RichTextBox control let the user manipulate these two indentations. Because each paragraph in a RichTextBox control is a separate entity, it can be
Copyright ©2002 SYBEX, Inc., Alameda, CA |
www.sybex.com |

286 Chapter 6 BASIC WINDOWS CONTROLS
formatted differently. The upper slider controls the paragraph’s indentation, and the lower slider controls the paragraph’s hanging indentation.
You can open the TextMargin application in this chapter’s folder on the CD and check it out. Enter a few paragraphs of text and experiment with it to see how the sliders control the appearance of the paragraphs.
To create the form shown in Figure 6.16, the left edge of the RichTextBox control must be perfectly aligned with the TrackBar control’s indicators at their leftmost position. When both sliders are at the far left, the SelectionIndent and SelectionHangingIndent properties are zero. As the indicators are scrolled, these two properties change value, and the text is reformatted instantly. All the action takes place in the Slider controls’ Scroll event.
Listing 6.18 presents the code of the Scroll event handlers for the two TrackBars: TrackBar1, on top, determines the paragraph’s indentation, and TrackBar2, on the bottom, controls the hanging or “first-line” indentation of the current paragraph.
Listing 6.18: Scroll Event Handlers of the TrackBars
Private Sub TrackBar1_Scroll(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles TrackBar1.Scroll RichTextBox1.SelectionIndent = _
CInt(RichTextBox1.Width * (TrackBar1.Value / TrackBar1.Maximum)) TrackBar2_Scroll(sender, e)
End Sub
Private Sub TrackBar2_Scroll(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles TrackBar2.Scroll RichTextBox1.SelectionHangingIndent = CInt(RichTextBox1.Width * _
(TrackBar2.Value / TrackBar2.Maximum)) - RichTextBox1.SelectionIndent End Sub
The paragraph’s hanging indentation is not the distance of the text from the left edge of the control, but the distance from the leftmost character of the first line. That’s why every time the paragraph’s indentation changes, the program calls the Scroll event of the second TrackBar control to adjust the hanging indentation, even though the second control hasn’t been moved. The hanging indentation is expressed as a percentage, and we get the ratio of the difference between the two controls and their maximum value.
Every time you move the pointer in another paragraph in the text, the two TrackBar controls must be set to reflect the margins of the current paragraph. The selection of a new paragraph is reported to the application through the SelectionChanged event of the RichTextBox control. In the event handler shown in Listing 6.19 are two lines that set the Value property of the two TrackBar controls to the appropriate values.
Copyright ©2002 SYBEX, Inc., Alameda, CA |
www.sybex.com |