Boxing and Unboxing

Value types are optimized for size and speed. They don't carry around the same amount of overhead as reference types. It would not be very efficient if every four-byte integer also carried around a four-byte reference. There are times, however, when treating value types and reference types in a polymorphic way would be nice. Consider this method declaration, which takes any number of arguments of any type and processes them in some way Public Shared Sub Print(ParamArray ByVal objArray( ) As...

Example 57 Clipping the area of a control

' Assumes Imports System.Drawing.Drawing2D Public Class MyControl Inherits UserControl Private Function CreateRegion( ) As Region Dim gp As New GraphicsPath( ) gp.AddEllipse(Me.ClientRectangle) Dim rgn As New Region(gp) Return rgn End Function Protected Overrides Sub OnResize(ByVal e As EventArgs) Me.Region Me.CreateRegion( ) End Sub Protected Overrides Sub OnPaint(ByVal e As PaintEventArgs) e.Graphics.FillEllipse(New SolidBrush(Me.ForeColor), Me.ClientRectangle) End Sub The CreateRegion method...

Example 612 Setting Forms authentication

< xml version 1.0 encoding utf-8 > < configuration> < system.web> < authentication mode Forms> < forms loginUrl SignIn.aspx name myWebApplication > < authentication> < deny users > < authorization> < system.web> < configuration> The mode attribute of the < authentication> element controls the kind of authentication to be performed. For Forms authentication, set this to Forms. When using Forms authentication, the < forms> element must also be...

Example 82 Allowing an Sql DataAdapter object to infer Sql Update Insert and Delete statements from a Select statement

Dim strConnection As String Data Source localhost Initial Catalog Northwind amp Integrated Security True Dim cn As SqlConnection New SqlConnection strConnection cn.Open ' Create a data adapter object and set its SELECT command. Dim strSelect As String SELECT FROM Categories Dim da As SqlDataAdapter New SqlDataAdapter strSelect, cn ' Set the data adapter object's UPDATE, INSERT, and DELETE ' commands. Use the SqlCommandBuilder class's ability to auto' generate these...

Finding Rows

The DataTable object's Rows property holds a DataRowCollection object that in turn holds the table's DataRow objects. Each DataRow object holds the data for that particular row. The following code loops through all the rows in the DataTable and displays the value of the first column column 0 in the row This code does the same thing, using a numeric index on the RowsCollection object Console.WriteLine dt.Rows n 0 Next To assist with locating specific rows within a table, the DataTable class...

Hello Printer

Example 4-13 shows a minimal printing example. Example 4-13. Hello, Printer ' These two lines initiate printing. Place this code in an ' appropriate place in the application. Dim pd As New HelloPrintDocument pd.Print ' This class manages the printing process. Public Class HelloPrintDocument Inherits PrintDocument Protected Overrides Sub OnPrintPage ByVal e As PrintPageEventArgs MyBase.OnPrintPage e ' Draw text to the printer graphics device. Dim fnt As New Font Arial, 10, FontStyle.Regular,...

The OnBegin Print and OnEnd Print Methods

The PrintDocument class provides the OnBeginPrint and OnEndPrint methods for managing the start and finish of print jobs. The OnBeginPrint method is called prior to the first call to OnPrintPage, and the OnEndPrint method is called after the final call to OnPrintPage. The OnBeginPrint method is a good place to set up objects that will be used throughout the life of the print job pens, brushes, and fonts, for example. The HelloPrintDocument class in Example 4-13 instantiates a Font object during...

The Printer Settings Class

The PrinterSettings class holds values that describe the capabilities and settings of a specific printer. It exposes these properties Indicates whether the printer can print on both sides of the paper. The syntax of the CanDuplex property is Public ReadOnly Property CanDuplex As Boolean Indicates whether the document being printed will be collated. The syntax of the Collate property is Public Property Collate As Boolean Indicates the number of copies to print. The syntax of the Copies property...

Detecting MDI Child Window Activation

Code in the MDI parent form class can be notified when an MDI child form becomes active inside an MDI parent form. Active means that the child form receives the input focus after another MDI child form or the MDI parent form had the input focus. To receive such notification, the MDI parent form must override the OnMdiChildActivate method defined in the Form class . For example ' Place this within the class definition of the MDI parent form. Protected Overrides Sub OnMdiChildActivate ByVal e As...

The List Box Class

The ListBox class represents a box that contains a list of items. The following are its more important properties This is a Boolean that indicates whether the listbox has more than one column. Its default value is False. In a multicolumn listbox, this property represents the width of each column in pixels. By default, the value of this property is zero, which makes each column have a default width. This is the most important property of the ListBox class. It returns the ListBox.ObjectCollection...

Handling Page Events

The base Page class may at times raise events. These events can be handled by the derived Page class the code-behind class or by code embedded in the web page. Although it's possible to define an event-handler subroutine, the preferred response to events raised by the Page class is to override the protected methods provided by the Page class. For example, the following method could be placed in the code-behind class, providing a way to respond to the loading of the page Protected Overrides Sub...

Visual Basic Event Handler Scope

Scope refers to the so-called visibility of identifiers within source code. That is, given a particular identifier declaration, the scope of the identifier determines where it is legal to reference that identifier in code. For example, these two functions each declare a variable CoffeeBreaks. Each declaration is invisible to the code in the other method. The scope of each variable is the method in which it is declared. Public Sub MyFirstMethod Dim CoffeeBreaks As Integer Public Sub...

Connecting to a SQL Server Database

To read and write information to and from a SQL Server database, it is necessary first to establish a connection to the database. This is done with the SqlConnection object, found in the System.Data.SqlClient namespace. Here's an example ' Open a database connection. Dim strConnection As String Data Source localhost Initial Catalog Northwind amp Integrated Security True Dim cn As SqlConnection New SqlConnection strConnection cn.Open This code fragment instantiates an object of type...

The Date TimePicker Class

The DateTimePicker class represents a control that allows users to select a date in the calendar, just like the MonthCalendar control. Unlike MonthCalendar, however, the DateTimePicker control only displays a box, which looks like a combo box, containing the selected date. When the user clicks the arrow, the control displays a drop-down calendar similar to the MonthCalendar control, from which the user can select a date. This drop-down portion closes as soon as the user selects a date. The user...

Example 61 The Web Forms Designers boilerplate HTML for a blank web form

lt Page Language vb AutoEventWireup false lt DOCTYPE HTML PUBLIC - W3C DTD HTML 4.0 Transitional EN gt lt html gt lt meta name GE gt ERATOR content Microsoft Visual Studio .NET 7.0 gt lt meta name CODE_LANGUAGE content Visual Basic 7.0 gt lt meta name vs defaultClientScript content JavaScript gt lt meta name vs targetSchema lt head gt lt body lt form id Form1 method post runat server gt lt form gt lt body gt lt html gt The code in Example 6-1 has the following qualities The Page directive at...

The Graphics Class

Objects of type Graphics defined in the System.Drawing namespace represent two-dimensional surfaces on which to draw. A Graphics object must be obtained before any drawing can be done. A common way to obtain a Graphics object is to override the OnPaint method of a form or user control, as shown in the following code fragment Public Class MyControl Inherits UserControl Protected Overrides Sub OnPaint ByVal e As PaintEventArgs e.Graphics.FillEllipse New SolidBrush Me.ForeColor ,...