- •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
AN HTML PRIMER 1009
Inserting Graphics
Graphics play an important role in Web page design. Almost every page on the World Wide Web uses graphics, and some pages contain hardly any text. Graphics are not inserted in the HTML document directly. The document itself contains special tags that reference the image to be inserted by the browser when the page is opened. Because of this, graphics files are downloaded separately and placed on the page by the browser.
On the Web, where every byte counts and downloads must be fast, images must contain as much information in as few bytes as possible. Despite the large number of graphics formats available today, two formats have dominated the Web:
JPEG (Joint Photographic Experts Group)
GIF (Graphics Interchange Format)
These formats are used because they compress graphics files to a manageable size. JPEG files can be compressed a good deal (albeit with some loss of detail), but they maintain a good image quality overall. The problems become evident when the compressed image is enlarged, but the graphics on Web pages are meant to be viewed in the context of the Web page to which they belong. The GIF file format is an old one, and it supports only 256-color images, but it has a few really handy features. It’s the only format that supports transparency, and its compression ratio is even better than JPEG without losing detail.
To insert an image at the current location in the document, use the <IMG> tag with the SRC attribute, which specifies the image to be displayed. Figure 23.2 shows a page with a simple graphic, centered across the page.
Figure 23.2
Inserting a simple graphic file
Here is the HTML source code that produced the page in Figure 23.2:
<HTML>
<HEAD>
<TITLE>Graphics on Web pages</TITLE> </HEAD>
Copyright ©2002 SYBEX, Inc., Alameda, CA |
www.sybex.com |
1010 Chapter 23 INTRODUCTION TO WEB PROGRAMMING
<BODY>
<CENTER>
<H1>Placing an Image on a Web page</H1> <IMG SRC=”earth1.jpg” ALT=”Earth photo”><BR> Our small planet, centered on the page
</CENTER>
</BODY>
</HTML>
The <IMG> tag has the following syntax:
<IMG SRC=”picture.jpg” ALT=”alt text”>
The <IMG> tag recognizes additional attributes, but you must include the SRC attribute, which is the location of an image file on the server or any URL on the Web. When you use the following attributes with the <IMG> tag, the browser can manipulate the image in several ways:
ALIGN aligns the image to the left, right, center, top, bottom, or middle of the screen.
WIDTH and HEIGHT specify the width and height of the image.
BORDER adds a border to the image, which is visible only if the image is a hyperlink.
VSPACE and HSPACE clear space around the image vertically or horizontally. The empty space is specified in pixels.
ALT includes a text message to be displayed if the user has turned off graphics.
If you want to change the size of an image, you can specify the size with the WIDTH and HEIGHT attributes, and the browser will size the existing file to the new values. For instance, to create a straight vertical line two pixels wide, simply use a square image two pixels on each side, and set the <IMG> tag’s WIDTH and HEIGHT properties:
<IMG WIDTH=”2” HEIGHT=”200” SRC=”picture.jpg”>
Your image will stretch 200 pixels high. You can also distort bitmaps with the WIDTH and HEIGHT attributes.
The BORDER attribute specifies the width of the border to appear around an image. Borders two pixels wide automatically surround any image used as a hyperlink. You may want to eliminate this automatic border with the BORDER=”none” attribute.
One aspect affecting the appearance of images, especially when they are surrounded by text, is the amount of space between the image and surrounding text. Space can be cleared horizontally and vertically with the HSPACE and VSPACE attributes. Simply specify the amount of space in pixels, for
example, HSPACE=”10” or VSPACE=”20”.
The ALT attribute displays alternative text for users whose browsers don’t display images (to speed up the loading of the page or perhaps to use special software to accommodate a disability). The attribute ALT=”Company Logo” tells the user that the image is not displayed in the browser. In addition, if the image takes a long time to download, the message “Company Logo” is displayed in the image’s space on the page. If for some reason your images are not transmitted or don’t show up, the user can still navigate your Web site and get the picture, so to speak.
Copyright ©2002 SYBEX, Inc., Alameda, CA |
www.sybex.com |
AN HTML PRIMER 1011
Tables
Tables are invaluable tools for organizing and presenting data in a grid or matrix. Tables are used in an HTML document for the same reasons they are used in any other document. There is, however, one more reason for using tables with an HTML document: to align the elements on a page. A table’s cell may contain text, hyperlinks, or images, and you can use the cell to align these elements on the page in ways that are simply impossible with straight HTML or even other tables. You can even use tables without borders, so your audience doesn’t see how you accomplished your amazing (for HTML) feats of graphic design.
The Basic Table Tags
Every table begins with the <TABLE> tag and ends with the </TABLE> tag. The attributes of the <TABLE> tag allow you to specify whether the table has borders, the width of borders, the distance between cells, and the proximity of cell contents to the edge of the cell. You can specify the width and height of the table either in pixels or as a percentage of total screen size.
Within the <TABLE> tags, each table row is marked by the <TR> tag. Each row’s cells are marked by the <TD> tag. Here’s the structure of a simple table. If you create an HTML file with the following lines and open it with your browser, you will see the items arranged as a table without any lines around them.
<HTML>
<TABLE>
<TR>
<TD> Row 1, Column 1 </TD> <TD> Row 1, Column 2 </TD> <TD> Row 1, Column 3 </TD>
</TR>
<TR>
<TD> Row 2, Column 1 </TD> <TD> Row 2, Column 2 </TD> <TD> Row 2, Column 3 </TD>
</TR>
<TR>
<TD> Row 3, Column 1 </TD> <TD> Row 3, Column 2 </TD> <TD> Row 3, Column 3 </TD>
</TR>
</TABLE>
</HTML>
Aligning Cell Contents
The ALIGN and VALIGN attributes specify the alignment of the cell’s contents. The ALIGN attribute is used for horizontal alignment and can have the value LEFT, CENTER, or RIGHT. The VALIGN attribute specifies the vertical alignment of the text, and it can have the value TOP, MIDDLE, or BOTTOM. The default alignment is LEFT (horizontal alignment) and MIDDLE (vertical alignment).
Copyright ©2002 SYBEX, Inc., Alameda, CA |
www.sybex.com |
1012 Chapter 23 INTRODUCTION TO WEB PROGRAMMING
A great deal of control over the alignment, spacing, and placement of cell contents within tables translates directly into excellent formatting capability for documents that would not ordinarily be built as tables. In fact, in HTML there are some effects you just can’t get (in a practical way) without the effective use of tables.
Table Width and Alignment
All the examples we have looked at so far use the default table width, which is determined by the entries of the individual cells. If a column contains a very long entry, the browser will wrap its contents to make sure that all columns are visible. However, it is possible to specify the width of the entire table with the WIDTH attribute of the <TABLE> tag.
The WIDTH attribute can be a value that specifies the table’s width in pixels or as a percentage of the window’s width. The table defined as <TABLE WIDTH=”50%”> occupies one-half of the window’s width.
The table defined as <TABLE WIDTH=”200”> will be 200 pixels wide, regardless of its contents and/or the window’s size. If the window is less than 200 pixels wide, part of the table will be invisible. To display the part of the table that’s outside the window, you’ll have to use the horizontal scroll bar.
The <TABLE> tag can also take the ALIGN attribute, but instead of aligning the table contents, this aligns the table itself. If you don’t include an ALIGN attribute, the table will be leftaligned in the browser’s window.
Tip This is true of most elements; paragraphs (<P>) and images (<IMG>) can also use ALIGN, and if it’s not present they default to aligning left.
Multiple Row and Multiple Column Cells
Quite often, tables don’t contain identically sized rows and columns. Some rows may contain fewer and wider cells than the others, and some columns may span multiple rows. The figures in this section contain tables with peculiar formatting.
Figure 23.3 shows a table with cells that span multiple columns and rows. These cells use the ROWSPAN and COLSPAN attributes, which let you create really elaborate tables. Either or both can appear in a <TD> tag, and they merge the current cell with one or more of its adjacent cells on the same row (in the case of the COLSPAN attribute) or column (in the case of the ROWSPAN attribute). The number of adjacent cells to be merged is the value of the COLSPAN and ROWSPAN attributes; COLSPAN=”2” means that the current cell covers two columns.
The table in Figure 23.3 was created with the HTML lines shown in Listing 23.1. The only thing I’ve done differently here is add the COLSPAN attribute in the appropriate <TD> tags to force some cells of the first row to span two columns, and I’ve added the ROWSPAN attribute to force some cells in the first column to span multiple rows. (The <TH> tag is simply a special table cell that indicates a table heading.) Other than that, the new table is as simple as those in the previous examples.
Copyright ©2002 SYBEX, Inc., Alameda, CA |
www.sybex.com |
AN HTML PRIMER 1013
Figure 23.3
This table contains cells that span multiple rows and columns.
Listing 23.1: The ROWSPAN and COLSPAN Attributes
<HTML>
<HEAD>
<TITLE>ROWSPAN - COLSPAN Examples</TITLE> </HEAD>
<BODY>
<TABLE BORDER=”1” ALIGN=”CENTER”> <CAPTION><B>ROWSPAN & COLSPAN Demo</B></CAPTION> <TR>
<TD COLSPAN=”2” ROWSPAN=”2”>Source:<BR>1991 Census</TD> <TH COLSPAN=”2”>Average</TH>
</TR>
<TR>
<TH>Height</TH>
<TH>Weight</TH>
</TR>
<TR>
<TH ROWSPAN=”2”>Gender</TH> <TH>Males</TH> <TD>5.83</TD> <TD>195.5</TD>
</TR>
<TR>
<TH>Females</TH>
<TD>5.22</TD>
<TD>167.8</TD>
</TR>
</TABLE>
</BODY>
</HTML>
Copyright ©2002 SYBEX, Inc., Alameda, CA |
www.sybex.com |