BUY THIS BOOK
Add to Cart

Print Book $39.95


Add to Cart

Print+PDF $51.94

Add to Cart

PDF $31.99

Safari Books Online

What is this?

Add to UK Cart

Print Book £28.50

What is this?

Looking to Reprint or License this content?


Programming Visual Basic 2005
Programming Visual Basic 2005 By Jesse Liberty
September 2005
Pages: 568

Cover | Table of Contents


Table of Contents

Chapter 1: Design and First Forms
In this chapter you will begin to create a Windows application. You will find that we get down to business immediately with as little fuss as possible. The introductory comments are intended to set the stage for everything else we're doing. I'll keep them short.
The requirements for a meaningful Windows application will be spelled out in this chapter, and the rest of the book will focus on implementing that application. We will finesse the design, exploring design decisions as we go, and our general approach will be that of successive approximation; that is: get it working and keep it working, as you add new functionality.
At times, this approach will cause us to write and rewrite the same section. One could argue that had we designed in advance we would avoid those cul-de-sacs, but it is exploring these dead ends, and the improvements we can make as we progressively improve our product, that will bring out essential aspects of Visual Basic 2005 programming.
Over the course of part one of this book, we will create an application based on a real-world application I recently built for one of my clients. We will use the application to explore retrieving and updating data from the Northwind Database that comes with SqlServer and SqlExpress.
At the time of this writing, Microsoft is urging that .NET applications use SqlExpress in preference to Access, and so we shall in this book, though converting the code to support an Access database should not be difficult.
The opening form for the application allows you to search for or display all the Customers, Orders, Suppliers, and Employees listed in the Northwind tables, as shown in Figure 1-1.
Figure 1-1: Initial form
The details page provides details on one company. It is tabbed and has a menu item, as shown in Figure 1-2. (Actual fields to be displayed will correspond to what is in the database.)
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
The Requirements
Over the course of part one of this book, we will create an application based on a real-world application I recently built for one of my clients. We will use the application to explore retrieving and updating data from the Northwind Database that comes with SqlServer and SqlExpress.
At the time of this writing, Microsoft is urging that .NET applications use SqlExpress in preference to Access, and so we shall in this book, though converting the code to support an Access database should not be difficult.
The opening form for the application allows you to search for or display all the Customers, Orders, Suppliers, and Employees listed in the Northwind tables, as shown in Figure 1-1.
Figure 1-1: Initial form
The details page provides details on one company. It is tabbed and has a menu item, as shown in Figure 1-2. (Actual fields to be displayed will correspond to what is in the database.)
Figure 1-2: Customer detail
This form will be used to demonstrate menus, events, data display, data binding, data updating, and so on.
If you click All Customers from the main menu, you will be brought to a custom Rolodex®, as shown in Figure 1-3.
Figure 1-3: All Customers Rolodex
The user can scroll through all the customers or click on a letter to advance immediately to customers whose name begins with that letter. This will show a fairly sophisticated custom control, as well as advanced use of inheritance and polymorphism, as we reuse the basic structure of the Rolodex to be able to scroll through both customers and suppliers.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Getting Started
The hardest part of any project, for me, is getting started. There is a simple problem of mental inertia that is overcome only by firing up Visual Studio 2005 and dragging some controls onto a form.
To begin, start Visual Studio 2005 and create a Visual Basic 2005 Windows application, as shown in Figure 1-4. (If you are already in Visual Studio 2005, choose File New project.)
Figure 1-4: Creating the new project
Visual Studio 2005 has short-cut keys for almost every important command. These key combinations are configurable, however, and it would be confusing to include short-cut keys in this book when yours may be different. The best way to learn the short-cut key combinations is to look at the menu choices as you go.
Once the project is created, you are put into the Designer, with a blank form (titled Form1). The Toolbox is typically on the left, the Solution explorer and Properties windows are usually on the right, and a number of other useful windows may be minimized on the bottom, as shown in Figure 1-5.
You can rearrange all of these Visual Studio elements by dragging and dropping the various windows. While you are in the editor, drag one of the windows from its docked position. As soon as you begin to move the window around, the docking diamond appears, as seen in Figure 1-6.
As you move the window, the four arrows of the diamond point to where you may dock. If you place the cursor over one of the arrows, it darkens and the placement for the window is previewed. If the window can join a tabbed group, the center of the diamond darkens as you pass the cursor over it. Hover over the darkened center and release the mouse and your window is automatically added to the tabbed group.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Creating the Customer Detail Page
Once the program is fully functional, the Find button will examine the contents of the text boxes and seek to find all the customers that match the text provided. If a single match is found, a form will open with details about that customer.
For now, we'll bypass the issue of what happens when multiple matches are found, and we'll even bypass the database search, and just build the form that will be filled in with the customer's details.
To get started, you need to create a second form, frmCustomerDetails. Right-click on the solution and choose Add Class. From within the dialog, choose Windows Form as the type of item you wish to add, and name the form frmCustomerDetails.vb, as shown in Figure 1-16. Click Add to add the new form.
Resize your form to 600,300. Click on your form and change the caption (using the Text property) to Customer Details.
Our task for this form is to add a menu, tab controls, and the controls necessary to display and edit the Customer information. The specifications call for this form to open in Read Only mode; the user must explicitly choose the menu item Edit to make the fields editable, and then Save to save the changes made in Edit mode (which returns the user to Read Mode).
Figure 1-16: Add second form
You'll begin by adding a menu to the form. To do so, first drag a menu strip onto the form. Two things will happen: your menu will be represented by a MenuStrip instance in the "tray" at the bottom of the form, and the menu itself will be docked to the top of the form. Notice that the menu has an area that says Type Here. Enter the text for the top-level menu, Customer, and notice that when you hit enter, two more Type Here boxes appear, one for a second top-level menu item (we won't need this for now) and one for a sub-menu. In that sub-menu enter the word Edit. As you do, another box will open below it. Enter the three remaining choices (Save, Cancel, Close) one by one.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Summary
In this chapter you've seen how to create a form, how to connect that form to other forms, and how to handle events by posting message boxes. In the next chapter, you will see how to connect data stored in a database to the fields in your form, and how to edit, update, and delete that data.
Figure 1-21: Customer info tab design
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Chapter 2: Data Access
In Chapter 1, you created a couple of forms and some Find buttons, but your forms were left more or less inert; they do not interact with real data.
In this chapter you'll begin to extract data from the Northwind database that comes both with SQL Server and SQL Server Express. You'll use that data to fill in your forms. You'll do this incrementally, adding complexity as you go. You'll put a premium on using data controls provided with Visual Basic 2005 and letting the controls manage the "plumbing" of database interaction for you.
If you do not have either SQL Server or SQLServer Express you will need to download a copy from Microsoft. There is usually some form of demonstration version available, though the names and conditions change from time to time. As of this writing, SQL Server Express is bundled with all versions of Visual Studio 2005 or is available as a free download on the Microsoft site (http://www.Microsoft.com).
Return to the first tab of frmCustomerDetails and give each text box a reasonable name (e.g. txtCustomerID, txtCity, and so on).
This chapter picks up on the code from the previous chapter. If you download the source, however, you'll find that we've created folders with snapshots that represent the state of the code at the end of each chapter.
You need a connection to the database. Before you begin, open the Data Sources Window (Data Show Data Sources). From this floating, dockable window there are a number of ways to create a new data source. The two simplest are either to click the hyperlink Add New Data Source ... or to click the Add New Data Source button , as shown in Figure 2-1.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Adding Data to the Customer Page
Return to the first tab of frmCustomerDetails and give each text box a reasonable name (e.g. txtCustomerID, txtCity, and so on).
This chapter picks up on the code from the previous chapter. If you download the source, however, you'll find that we've created folders with snapshots that represent the state of the code at the end of each chapter.
You need a connection to the database. Before you begin, open the Data Sources Window (Data Show Data Sources). From this floating, dockable window there are a number of ways to create a new data source. The two simplest are either to click the hyperlink Add New Data Source ... or to click the Add New Data Source button , as shown in Figure 2-1.
Figure 2-1: Add New Data Source button
Clicking this button opens the Data Source Configuration wizard. On the first tab, you can indicate the type of DataSource you wish to use; in this case, you'll pick Database. The next step is to create a Connection to do that data source.
You can use an existing connection (if you have one) or click New Connection... to create a new connection object. This opens the connection Properties modal dialog box. The first step is to select the server, the second step is to choose between Windows Integrated Security (trusted connection) or a specific database user ID. The third and final step is to choose the database. Be sure to click the Test Connection button to ensure that the connection is working, as shown in Figure 2-2.
Figure 2-2: Configure and test the connection
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Using the Details View to Create the Detail Form
In the same way that you want to allow the user to search for a customer, you'd like to be able to search for a supplier. In addition, when looking at the supplier, you'd like to see which products that supplier offers.
The first step is to add new tables to the NorthWindDataSet. To do so, choose the menu selections Data ShowDataSources. Right-click on the NorthWindDataSet and choose Edit Data Source With Designer.
This opens the NorthwindDataSet.xsd designer. Right-click anywhere in the designer (except on the Customers table) and choose Add DataTable, as shown in Figure 2-11.
Figure 2-11: Adding a new data table adapter
This opens the DataComponents Configuration Wizard. The first step is to choose the connection you want to use (in this case, you can continue to use the NorthWind Connection).
The next step is to choose whether you are using SQL Statements, new stored procedures, or existing stored procedures. In this case, you'll use SQL Statements.
In the next step, click on the Query Builder window . The Add Table dialog will open, as shown in Figure 2-12.
Figure 2-12: Adding a table
Select the Suppliers table. Click Add and then click Close. When you return to the Query Builder window, select the checkbox beside All Columns, to select all the columns in the Suppliers table, as shown in Figure 2-13.
Figure 2-13: Query Builder window
Click OK and then Next, to open the Choose Methods to Generate dialog box, as shown in Figure 2-14.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Modify the Display with Events
Let's modify the spec to say that the Suppliers form will come up in display mode (with editing disabled), and the user will have the ability to make a menu choice to edit the form, and then save or cancel the edits.
Figure 2-25: Customer Details updated
To accomplish this, you'll want to add a menu to the form, and an indication (perhaps in the form title bar) as to which mode you are in: Read, Edit, or Unsaved. In Read mode, the text boxes and grid will be disabled. In Edit mode the controls will be enabled. Once you've made changes to the form, but not yet saved them, you'll be in Unsaved mode. The advantage of distinguishing between Edit and Unsaved mode is that if Cancel is selected or there is an attempt to close the form, you can put up a reminder that the changes have not been saved.
To begin, add a menu strip control to frmSuppliers.vb, as shown in Figure 2-26.
Figure 2-26: Add Editing Menu to frmSuppliers
The code in the frmSuppliers_Load event handler, as it now stands, loads the data from the database. You need to change it to first disable the text boxes and the datagrid, and then add event handlers to detect when the user makes changes.
The new implementation of frmSuppliers_Load is shown in Example 2-9.
Example 2-9. New Suppliers form Load event handler
Private Sub frmSuppliers_Load( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load

    Me.SuppliersTableAdapter.FillByCompanyName( _
    Me.NorthwindDataSet.Suppliers, Me.m_CompanyNameParameter)
    Me.ProductsTableAdapter.Fill(Me.NorthwindDataSet.Products)

   Dim ctrl As Control
   Dim txtbox As TextBox = Nothing
   Dim dgv As DataGridView = Nothing
   For Each ctrl In Me.Controls
      If TypeOf ctrl Is TextBox Then
         txtbox = CType(ctrl, TextBox)
         txtbox.Enabled = False
         AddHandler txtbox.ModifiedChanged, AddressOf TextBoxChanged
      ElseIf TypeOf ctrl Is DataGridView Then
         dgv = CType(ctrl, DataGridView)
         dgv.Enabled = False
         AddHandler dgv.CellValueChanged, AddressOf DataGridChanged
      End If
   Next
   Me.Text = formName + " Read only"
End Sub
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Chapter 3: Cool Controls
In this chapter you'll explore the tools available for building advanced forms with Visual Basic 2005.
Start by changing the title displayed on the Welcome Form you created in Chapter 2. Click on the form, and set the Text property (in the Properties Windows) to Welcome.
Okay, that was pretty easy. Have a cookie and let's move on.
To navigate to the new pages that you'll be adding to the application in this chapter, you'll need to add a menu to the Welcome page. Lengthen the form and drag all the controls down (including the images) to make room for the menu. To do so, click in the form, click Control-A to mark all the images, then grab a move-handle and drag them in unison.
Drag a menu strip control from the Toolbox to the top of the Welcome page. Notice that "MenuStrip1" is added. Rename this to mnuWelcome. Click on the Menu, and add four top-level menus : Employees, Customers, Orders, and Suppliers. For each, create two sub-menu choices: Show All and Find.
To move from one top-level menu item to the next, use tab. Within a menu, to move from one sub-item to the next, use Enter.
You can now move all the other controls back up into position below the menu.
Before proceeding, rename the various menu choices by clicking on each and setting its Name property in the Properties window. For example, click on Employees Show All and set its name to mnuEmployeesShowAll, and set its Find sub-menu to mnuEmployeeFind, as shown in Figure 3-1.
Figure 3-1: Set the menu choice name in the Properties window
Setting the event handlers for the menu choices is also pretty easy, as you want them to do the same thing their related buttons specify. Thus, click on the All Employees button, and in the Properties window, click the Events button (the lightning bolt) to see the names of the predefined event handlers for this control. Copy the button's Click event handler (
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Adding a Menu and Toolbar
To navigate to the new pages that you'll be adding to the application in this chapter, you'll need to add a menu to the Welcome page. Lengthen the form and drag all the controls down (including the images) to make room for the menu. To do so, click in the form, click Control-A to mark all the images, then grab a move-handle and drag them in unison.
Drag a menu strip control from the Toolbox to the top of the Welcome page. Notice that "MenuStrip1" is added. Rename this to mnuWelcome. Click on the Menu, and add four top-level menus : Employees, Customers, Orders, and Suppliers. For each, create two sub-menu choices: Show All and Find.
To move from one top-level menu item to the next, use tab. Within a menu, to move from one sub-item to the next, use Enter.
You can now move all the other controls back up into position below the menu.
Before proceeding, rename the various menu choices by clicking on each and setting its Name property in the Properties window. For example, click on Employees Show All and set its name to mnuEmployeesShowAll, and set its Find sub-menu to mnuEmployeeFind, as shown in Figure 3-1.
Figure 3-1: Set the menu choice name in the Properties window
Setting the event handlers for the menu choices is also pretty easy, as you want them to do the same thing their related buttons specify. Thus, click on the All Employees button, and in the Properties window, click the Events button (the lightning bolt) to see the names of the predefined event handlers for this control. Copy the button's Click event handler (btnAllClick).
Now click on the Employees menu to open it, and select Show All. In the Properties window, click on the Event button (the one with the lightning bolt) to reveal the events for this menu choice, and in the Click event, paste the
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Displaying Web Documents
Create a new form and name it frmWeb.
Resize the form to 800,700 and drag a Web Browser from the Toolbox onto the new form. You'll find that it fills the form. Click on the smart tab and click on the "Undock in parent container" link, as shown in Figure 3-4.
Figure 3-4: Undock the web form
Shrink the web form down just enough to add a text box (which you'll name txtURL) and four buttons (btnGo, btnHome, btnPrevious, and btnNext), as shown in Figure 3-5.
Figure 3-5: Designing the web browser
It would be useful to disable the Previous button when it is not possible to go back any further, and to disable the Next button when there is no next page. The Web Browser has two properties (CanGoBack and CanGoForward ) that you can test. Rather than testing these every time the form is navigated, it's more efficient to respond to the events that fire when these properties change—CanGoBackChanged and CanGoForwardChanged—as shown in Example 3-4.
Example 3-4. CanGoBackChanged and CanGoForward event handlers
Private Sub WebBrowser1_CanGoBackChanged( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles WebBrowser1.CanGoBackChanged
   btnPrevious.Enabled = WebBrowser1.CanGoBack
End Sub

Private Sub WebBrowser1_CanGoForwardChanged( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles WebBrowser1.CanGoForwardChanged
   
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Masked Text Box
A very handy advanced control provided by Visual Basic 2005 is the MaskedTextBox control. A Masked Text Box only allows data to be entered if it matches a particular pattern. For example, you might provide a telephone mask, if the user enters 6175551212, the mask will render the input as (617) 555-1212.
The mask can block invalid characters (such as the % sign) and can signal to the user what is expected (e.g., the parentheses indicate that an area code is required).
To see this at work, return to frmSuppliers and delete the txtPhone text box. Drag into its place a MaskedTextBox control, and name it mtbPhone. Click on its smart tag and click the Set Mask link, bringing up the Input Mask dialog that allows you to pick one of the existing masks for your control, as shown in Figure 3-11.
Figure 3-11: Input Mask dialog
While it is possible to create a custom mask, in this case, the Phone number mask is just what you want. The mask itself is shown at the bottom of the dialog, and you have a chance to "try" the mask before accepting it. Click OK to accept the mask.
What you want, however, is a MaskedTextBox that is bound to your data. There are many ways to accomplish this, but the easiest is to drag one from the Suppliers table.
Delete the MaskedTextBox on your form and its associated label, and open the Data Source View (Data Show Data Sources). Expand the Suppliers table and click on the drop down next to phone. Click on Customize. This opens the Options Dialog, as shown in Figure 3-12.
Figure 3-12: Options dialog for customizing data controls
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Printing a Document
One of the key features of any full-fledged desktop application is the ability to print. Let's add another choice to the Welcome Form menu, mnuFile with the text File. While creating the menu choice, use the Properties window to create mnuFile_Click and add a single line:
Private Sub mnuFile_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles mnuFile.Click
   frmText.Show()
End Sub
As you can guess, you'll now want to add a new form, frmText to the project. Resize the new form to 700,600 and set its text attribute to "Text Form."
This form will have two controls, a RichTextBox and a menu. The menu name will be "File" (mnuFile) and will have sub-menu items of Open, Save, and Print, as shown in Figure 3-14.
Figure 3-14: Items Collection Editor Items Collection Editor
After your menu is set up, drag a RichTextBox control onto the form and set its size to 668,512 and its location to 12,42.
Drag an OpenFileDialog and a SaveFileDialog onto your tool strip, along with a PrintDocument and a PrintDialog control, leaving their default names as they are. Implement the mnuFileOpen_Click event handler first, as shown in Example 3-15.
Example 3-15. File menu Open item Click event handler
Private Sub mnuFileOpen_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles mnuFileOpen.Click
    ' set the initial directory in which to look for files
    Me.OpenFileDialog1.InitialDirectory = "C:\Temp"

    'set the file filter
    Me.OpenFileDialog1.Filter = "Text files (*.txt) | *.txt"

    ' check to see if the user clicked ok, if so, load the
    ' file into the rich text box, setting the file type to
    ' plain text, and set the font
    Dim result As DialogResult = Me.OpenFileDialog1.ShowDialog()
    If result = Windows.Forms.DialogResult.OK Then
        
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Copying Files Using Tree Views
Let's try something a bit fancier. Add a menu choice to the Welcome form's menu named mnuFilesFileCopier. Set its Text to File Copier. The event handler for that menu choice will open the frmFilesCopier form that you'll create to copy files from a group of directories selected by the user to a single target directory or device, such as a floppy or backup hard drive.
Although you won't implement every possible feature, you can imagine programming this form so that you can mark dozens of files and have them copied to multiple disks.
Begin by creating the frmFilesCopier form, then extending it to a size of 570,740. Next, drag on three labels, a text box, two tree view controls, four buttons, and a checkbox, as shown in Figure 3-15.
Figure 3-15: File Copier design
Drag a StatusStrip on to the form at the bottom. Click on the status strip's drop down (on the form) and chose StatusLabel. Set the label's name to lblStatus and set its Text to Ready.
You want checkboxes next to the directories and files in the source selection window but not in the target (where only one directory will be chosen). Set the CheckBoxes property on tvwSource to true, and on tvwTarget to false.
Once you've done this, double-click the Cancel button to create its event handler. The entire implementation for this event handler is to close the form without taking further action, as shown in Example 3-21.
Example 3-21. Cancel button Click event handler
Private Sub btnCancel_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnCancel.Click
    Me.Close()
End Sub
The two TreeView controls work identically, except that the left control, tvwSource
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Chapter 4: Custom Controls
When the user clicks on the All Customers button of the Welcome page you've been building in previous chapters, a Rolodex of all the customers is displayed, as shown in Figure 4-1.
Figure 4-1: Complete Customer Rolodex
Unfortunately, Microsoft neglected to include a Rolodex control in Visual Studio 2005. No problem, though; in this chapter, you'll implement your own as a custom control.
This code builds on the project started in the previous chapter. You can download the source code completed in Chapter 3 if you would like to start here.
Custom controls come in three flavors:
A derived control
With a derived control, you take an existing control (e.g., a button) and give it new capabilities. For example, you might create a button that knows how many times it has been clicked.
A composite control
In a composite control, you take existing controls (whether provided by the Framework, or ones you've created) and you package them together into a single control.
A from-scratch control
Creating a custom control from scratch requires that you draw the control yourself using the GDI+ capabilities covered in the next chapter.
There are two ways to approach a custom control of the complexity of the Rolodex. One is to build it incrementally; the other is to design it up front. I typically build incrementally, factoring out common code as I go. However, to present all the myriad iterations as functionality is added one step at a time would be a book in itself.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Custom Controls
Custom controls come in three flavors:
A derived control
With a derived control, you take an existing control (e.g., a button) and give it new capabilities. For example, you might create a button that knows how many times it has been clicked.
A composite control
In a composite control, you take existing controls (whether provided by the Framework, or ones you've created) and you package them together into a single control.
A from-scratch control
Creating a custom control from scratch requires that you draw the control yourself using the GDI+ capabilities covered in the next chapter.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Design
There are two ways to approach a custom control of the complexity of the Rolodex. One is to build it incrementally; the other is to design it up front. I typically build incrementally, factoring out common code as I go. However, to present all the myriad iterations as functionality is added one step at a time would be a book in itself.
Thus, as an expedient, I'm going to build this as if I were omniscient, anticipating in advance a complete design that I can then implement.
Your Rolodex will be housed in forms. You will have a Rolodex form for Customers, a second Rolodex form for Suppliers, and one each for Employees and Orders (you'll only implement Customers here; the others will be left as an exercise).
These four forms will all derive from a common base form, frmRolodex. The job of frmRolodex will be to hold the code and design common to all the derived forms.
Within each of these forms will be a Rolodex panel. The job of the panel will be to:
  • Display all the buttons (a-z)
  • Display twelve Rolodex entries at a time
  • Display the scrollbars
The Rolodex panel is shown in Figure 4-2.
Figure 4-2: Rolodex panel
Within the panel are Rolodex entries. You'll design a MustInherit base class, RolodexEntry, and then you'll derive classes (like RolodexCustomerEntry) from it. These classes will specialize what information goes in the entry. For Customers, you want the customer name, contract, phone, and fax. For suppliers, Employees, and Orders, you'll want different information.
In summary, for this chapter, you'll build the following:
frmRolodex
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Building the Controls
Add a new project to your existing solution. To do so, right-click on the solution and choose Add New project. Leave the Location the same as the location for your previous project, but name the new project NorthWindControls. Be sure to set the template to Windows Control Library, as shown in Figure 4-3.
Figure 4-3: Create the new Windows Control Library project
Visual Studio 2005 creates a new project (NorthWindControls) and within that project adds a UserControl, which it names UserControl1. Begin by renaming its file to RolodexPanel.vb.
To use the Rolodex panel, however, you need Rolodex entries. Therefore, you need to create a couple of additional custom controls.
To start, right-click on the new project and choose Add New User Control and name it RolodexEntry.vb. This will serve as the base class for all the specialized RolodexEntry controls. You will create a derived RolodexEntry type for customers (RolodexCustomerEntry), on which the discussion in this chapter will focus. If you like, you can also create RolodexEntry types for Suppliers, Employees, and Orders.
RolodexEntry will have a Boolean member: chosen.
Public MustInherit Class RolodexEntry
    Protected chosen As Boolean
In addition, you will define an event for this user control. As you remember, all controls can publish events. The controls you've used so far have had such events as Click. You can create your own custom event for your new control just by declaring it with the Event keyword. In this case, you'll want to define an EntrySelected event, as follows:
Public Event EntrySelected(ByVal sender As Object, ByVal e As EventArgs)
You read this line of code as follows: EntrySelected is a public event that will be handled by a method that takes two parameters: one of type
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Using the Custom Controls
Your custom control (RolodexPanel) will be housed within a form. The base form will be frmRolodex, whose job will be to provide common code for all the specialized forms (e.g., frmCustomerRolodex).
Back in the NorthWindWindows project, add a new form, frmRolodex. Set its size to 976,615. Open the Toolbox and expand the NorthWindControls Components section. Drag a RolodexPanel onto the new form, and drag a label named lblDisplay above it, as shown in Figure 4-6.
Everything in frmRolodex will be shared by all its derived types. You want to factor all the elements common to the derived forms into this form, so they will be as simple (and maintainable) as possible.
You need two members:
Protected orderedBy As String
Protected infoTable As Data.DataTable
The first, orderedBy, will keep track of the sort order for the data table. The second, infoTable, will hold a reference to a DataTable (e.g., the Customers table).
There are three event handlers you must create: one for when the form is loaded, the second for when the RowFillEvent is fired by the RolodexPanel, and the third for when the ButtonSelectedEvent is fired by the RolodexPanel.
When the form is loaded, you'll call LoadRolodex, a helper method, as shown in Example 4-12.
Figure 4-6: Adding RolodexPanel to frmRolodex
Example 4-12. Rolodex form Load event handler
Private Sub frmRolodex_Load( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
    LoadRolodex()
End Sub
This method will not be implemented in the base class, but will be implemented in the derived forms:
Protected Overridable Sub LoadRolodex()
End Sub
Protected Overridable Sub LoadRolodex(ByVal letter As Char)
End Sub
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Chapter 5: GDI+ and Drawing
In the previous chapter, you created custom controls by combining already existing controls. As noted in that chapter, there are times when you want to take over the entire responsibility of drawing a custom control. In fact, there are times when you want to take over the entire display on a form.
To do so, you'll need the tools made available through GDI+ and the Graphics class.
The GDI+-managed class interface is part of the Microsoft .NET Framework and is implemented as a set of wrappers over Windows objects.
The control you will create is shown in Figure 5-1.
Figure 5-1: Clock Face Custom Control
To begin, right-click on NorthWindControls and choose Add New Item. In the Add New Item dialog box, choose Custom Control and name your new Custom Control ClockFaceCtrl.vb. Switch to code view, since you'll be creating this control entirely through code, from scratch.
To create this control, you will be responsible for every aspect of drawing the clock and animating it. To simplify your coding, you'll want to add an Imports statement for the Drawing namespace:
Imports System.Drawing
The Drawing namespace includes a number of classes and structures that we'll use in this program. Some of the most important members of this class are summarized briefly in Table 5-1.
Table 5-1: Members of the Drawing class
Class
Description
Bitmap
Encapsulates a GDI+ bitmap—i.e., pixel data representing an image.
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
The Graphics Class
The Graphics class represents a GDI+ drawing surface. A Graphics object maintains the state of the drawing surface, including the scale and units, as well as its orientation.
The Graphics class provides a great many properties. The most commonly used ones are listed in Table 5-2.
Table 5-2: Graphics class members Graphics class members
Property
Type
Description
Clip
Region
Read/write. Specifies the area available for drawing.
DpiX
Float/single
Read/write. The horizontal and vertical resolution (respectively) of the Graphics object in dots per inch.
DpiY
PageScale
Float/single
Read/write. The scaling between world units and page units for this Graphics object.
PageUnit
GraphicsUnit
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Implementing the Control
Now, you are ready to create your control. First, add the following members to the ClockFaceCtrl class. (You'll implement the StringDraw class shortly.)
Private Shared offset As Integer = 0
Private b24Hours As Boolean = False
Private Const FaceRadius As Integer = 700
Private Const DateRadius As Integer = 900
Private currentTime As DateTime
Private myFont As New Font("Arial", 80)
Private sdToday As StringDraw
Private bForceDraw As Boolean = True
Second, you'll need a timer to drive the automatic updating of the clock. Drag a Timer control onto the ClockFaceCtrl window. Visual Basic automatically creates an object of the System.Windows.Forms.Timer class, and names it Timer1. You don't need to do anything else with it for now; you'll set its Elapsed event in the initialization code.
Third, there are two helper methods you'll need to handle some trigonometry for you, GetCos and GetSin. I'll explain what they do later; for now, you can just type them in.
Private Shared Function _
   GetCos(ByVal degAngle As Single) As Single
    Return CSng(Math.Cos((Math.PI * degAngle / 180.0F)))
End Function 'GetCos

Private Shared Function _
   GetSin(ByVal degAngle As Single) As Single
    Return CSng(Math.Sin((Math.PI * degAngle / 180.0F)))
End Function 'GetSin
Additional content appearing in this section has been removed.
Purchase this book now or read it online at Safari to get the whole thing!
Chapter 6: Mice and Fonts
Content preview·Buy PDF of this chapter|