Cover | Table of Contents
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.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.600,300. Click on your form and change the caption (using the Text property) to Customer Details.
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.
http://www.Microsoft.com).frmCustomerDetails and give each text box a reasonable name (e.g. txtCustomerID, txtCity, and so on).frmCustomerDetails and give each text box a reasonable name (e.g. txtCustomerID, txtCity, and so on).
NorthWindDataSet. To do so, choose the menu selections Data → ShowDataSources. Right-click on the NorthWindDataSet and choose Edit Data Source With Designer.
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.
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.
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.frmSuppliers_Load is shown in Example 2-9.
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
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.mnuEmployeesShowAll, and set its Find sub-menu to mnuEmployeeFind, as shown in Figure 3-1.
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.mnuEmployeesShowAll, and set its Find sub-menu to mnuEmployeeFind, as shown in Figure 3-1.
btnAllClick).frmWeb.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.txtURL) and four buttons (btnGo, btnHome, btnPrevious, and btnNext), as shown in Figure 3-5.
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.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
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.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.
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.
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
frmText to the project. Resize the new form to 700,600 and set its text attribute to "Text Form."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.
RichTextBox control onto the form and set its size to 668,512 and its location to 12,42.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.
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
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.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.
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.CheckBoxes property on tvwSource to true, and on tvwTarget to false.
Private Sub btnCancel_Click( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnCancel.Click
Me.Close()
End Sub
TreeView controls
work identically, except that the left control, tvwSource
frmRolodex. The job of frmRolodex will be to hold the code and design common to all the derived forms.
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.frmRolodex
NorthWindControls. Be sure to set the template to Windows Control Library, as shown in Figure 4-3.
NorthWindControls) and within that project adds a UserControl, which it names UserControl1. Begin by renaming its file to RolodexPanel.vb.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
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)
EntrySelected is a public event that will be handled by a method that takes two parameters: one of type 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).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.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.Protected orderedBy As String Protected infoTable As Data.DataTable
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).RowFillEvent is fired by the RolodexPanel, and the third for when the ButtonSelectedEvent is fired by the RolodexPanel.LoadRolodex, a helper method, as shown in Example 4-12.
Private Sub frmRolodex_Load( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
LoadRolodex()
End Sub
Protected Overridable Sub LoadRolodex() End Sub Protected Overridable Sub LoadRolodex(ByVal letter As Char) End Sub
Graphics class.
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.Imports statement for the Drawing
namespace:Imports System.Drawing
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.|
Class
|
Description
|
|---|---|
Bitmap
|
Encapsulates a GDI+ bitmap—i.e., pixel data representing an image.
|
|
|
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.Graphics class provides a great many properties. The most commonly used ones are listed in Table 5-2.|
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
|
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
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.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