Standard ComponentsThe following is a quick survey of the components immediately available in the Windows Forms Toolbox by default. [1]
Standard DialogsWinForms provides several standard dialog components that act as wrappers on the standard dialogs in the Windows shell. [2] All the standard print- related components ”PageSetupDialog, PrintDialog, PrintPreviewDialog, and PrintDocument ”are covered in Chapter 7: Printing.
Figures D.2, D.3, D.4, D.5, and D.6 show the four remaining standard dialogs as provided by the ColorDialog, FontDialog, OpenFileDialog, SaveFileDialog, and FolderBrowserDialog components. Figure D.2. A Sample Usage of the ColorDialog Component
Figure D.3. A Sample Usage of the FontDialog Component
Figure D.4. A Sample Usage of the OpenFileDialog Component
Figure D.5. A Sample Usage of the SaveFileDialog Component
Figure D.6. A Sample Usage of the FolderBrowserDialog Component
Generally, you're most concerned with the Color property of ColorDialog, the Font property of FontDialog, and the FileName property of OpenFileDialog and SaveFileDialog. However, all the dialogs provide other properties you'll want to examine, including the DefaultExt and Filter properties of OpenFileDialog and SaveFileDialog. Here's an example of the standard dialog components assuming that each of the components was created by dropping it onto a design surface: ' Use the ColorDialog Sub chooseColor_Click(sender As Object, e As EventArgs) ' Set initial color Me.colorDialog1.Color = Me.BackColor ' Ask the user to pick a color If (Me.colorDialog1.ShowDialog() = DialogResult.OK) Then ' Pull out the user's choice Me.BackColor = Me.colorDialog1.Color End If End Sub ' Use the FontDialog Sub chooseFont_Click(sender As Object, e As EventArgs) ' Set initial font Me.fontDialog1.Font = Me.Font ' Ask the user to pick a font If (Me.fontDialog1.ShowDialog() = DialogResult.OK) Then ' Pull out the user's choice Me.Font = Me.fontDialog1.Font End If End Sub ' Use the OpenFileDialog (used just like the SaveFileDialog) Sub chooseFileToOpen_Click(sender As Object, e As EventArgs) ' Set the initial file name Me.openFileDialog1.FileName = Me.fileNameTextBox.Text ' Ask the user to pick a file If (Me.openFileDialog1.ShowDialog() = DialogResult.OK) Then ' Pull out the user's choice Me.fileNameTextBox.Text = Me.openFileDialog1.FileName End If End Sub All the standard dialogs, including the print-related dialogs (except for PrintPreviewDialog), are limited to only modal operation via ShowDialog because that's what the shell dialogs support. Only PrintPreviewDialog, which is provided in WinForms but not provided in the shell, supports the modeless Show method. Notify IconsAnother service provided by the Windows shell is the notification tray, where applications can put their own icons to interact with the user, as shown in Figure D.7. The NotifyIcon component has the following interesting properties and events:
Figure D.7. A Sample Usage of the NotifyIcon Component (See Plate 26)
Figure D.7 shows a notify icon implemented with the following code. It sets the tooltip text and icon at run time based on the user clicking and choosing menu items from a context menu: ' Icons Dim northIcon As Icon = New Icon("C:\...\ARW02UP.ICO") Dim eastIcon As Icon = New Icon("C:\...\ARW02RT.ICO") Dim southIcon As Icon = New Icon("C:\...\ARW02DN.ICO") Dim westIcon As Icon = New Icon("C:\...\ARW02LT.ICO") ' Helper Sub SetDirection(direction As String) ' Set the tooltip compassNotifyIcon.Text = direction ' Set the icon Select Case direction Case "North" compassNotifyIcon.Icon = northIcon Case "East" compassNotifyIcon.Icon = eastIcon Case "South" compassNotifyIcon.Icon = southIcon Case "West" compassNotifyIcon.Icon = westIcon End Select End Sub ' Context menu item: North Sub northMenuItem_Click(sender As Object, e As EventArgs) SetDirection("North") End Sub ' Context menu item: East, South, and West elided ' Click handler Sub compassNotifyIcon_Click(sender As Object, e As EventArgs) Select Case compassNotifyIcon.Text Case "North" SetDirection("East") Case "East" SetDirection("South") Case "South" SetDirection("West") Case "West" SetDirection("North") End Select End Sub TimerThe example in Figure D.7 uses mouse clicks to do some primitive animation of the notify icon, but a more fun implementation would do animation itself. Because animation is simply a matter of changing an image after a certain amount of time has elapsed, our code to change the image is halfway there. All that's needed is a way to be notified when a certain amount of time has elapsed, a perfect job for the Timer component. The Timer component notifies a listener via the Tick event based on two properties. The Enabled property must be set to true for the Tick event to fire. The Interval property is the number of milliseconds to wait between Tick events. The following example uses a timer to animate the notify icon: ' Notify icon click handler Sub compassNotifyIcon_Click(sender As Object, e As EventArgs) ' Toggle animation timer1.Enabled = Not(timer1.Enabled) timer1.Interval = 1000 ' Animate once/second End Sub ' Timer Tick event handler Sub timer1_Tick(sender As Object, e As EventArgs) Select Case compassNotifyIcon.Text Case "North" SetDirection("East") Case "East" SetDirection("South") Case "South" SetDirection("West") Case "West" SetDirection("North") End Select End Sub Image ListAll this animation brings to mind the task of managing images, and that's what the ImageList component is for. An image list is a collection of images of the same size, color depth, and transparency color (as determined by the Size, ColorDepth, and TransparencyColor properties). The images themselves are in the Images collection and can contain any number of Image objects. You can edit the Images collection directly using Image Collection Editor, as shown in Figure D.8. Figure D.8. Image Collection Editor
To use the ImageList after the images have been populated in the collection editor, you pull them by index from the Images collection property: Sub SetDirection(direction As String) Dim index As Integer = -1 Select Case direction Case "North" index = 0 Case "East" index = 1 Case "South" index = 2 Case "West" index = 3 End Select ' Set background from the image list Me.BackgroundImage = backgroundImageList.Images(index) End Sub What's nice about this code is that all the related images come from a single place. However, the ImageList component has some limitations:
These limitations, however, don't stop the image list from being useful. Chapter 8: Controls explains how to use image lists to set the images for toolbars and other controls that need small, fixed-size , related images. Main Menus and Context MenusThe MainMenu component shows the menu at the top of a form and provides events when the user selects an item. The ContextMenu component, shown in Figure D.9, provides a menu to be associated with a control and invoked using the context mouse button (most often the right mouse button). Both the MainMenu and the ContextMenu components are covered in Chapter 2: Forms. Figure D.9. A Sample Usage of the ContextMenu Component
Error Provider, Help Provider, and TooltipsThe ErrorProvider component gives you a way to notify users that a control has invalid data currently entered, as shown in Figure D.10. Figure D.10. A Sample Usage of the ErrorProvider Component
HelpProvider supports the handling of the F1 key and ? button on a dialog. Figure D.11 shows an example of the pop-up help supported by the help provider. Figure D.11. A Sample Usage of the HelpProvider Component
As yet another way to show pop-up information, Figure D.12 shows the ToolTip component, which pops up when a user hovers the mouse over a control. Figure D.12. A Sample Usage of the ToolTip Component
The error provider, the help provider, and the tooltip component are especially useful when you're building forms to be used as dialogs, so these components are discussed in detail in Chapter 3: Dialogs. |