- •brief contents
- •about this book
- •The Windows Forms namespace
- •Part 1: Hello Windows Forms
- •Part 2: Basic Windows Forms
- •Part 3: Advanced Windows Forms
- •Who should read this book?
- •Conventions
- •Action
- •Result
- •Source code downloads
- •Author online
- •acknowledgments
- •about .NET
- •Casting the .NET
- •Windows Forms overview
- •about the cover illustration
- •Hello Windows Forms
- •1.1 Programming in C#
- •1.1.1 Namespaces and classes
- •1.1.2 Constructors and methods
- •1.1.3 C# types
- •1.1.4 The entry point
- •1.1.5 The Application class
- •1.1.6 Program execution
- •1.2 Adding controls
- •1.2.1 Shortcuts and fully qualified names
- •1.2.2 Fields and properties
- •1.2.3 The Controls property
- •1.3 Loading files
- •1.3.1 Events
- •1.3.2 The OpenFileDialog class
- •1.3.3 Bitmap images
- •1.4 Resizing forms
- •1.4.1 Desktop layout properties
- •1.4.2 The Anchor property
- •1.4.3 The Dock property
- •1.5 Recap
- •2.1 Programming with Visual Studio .NET
- •2.1.1 Creating a project
- •Action
- •Result
- •2.1.2 Executing a program
- •Action
- •Result
- •2.1.3 Viewing the source code
- •View the code generated by Visual Studio .NET
- •Action
- •Result
- •2.2 Adding controls
- •2.2.1 The AssemblyInfo file
- •Action
- •Results
- •2.2.2 Renaming a form
- •Action
- •Result
- •2.2.3 The Toolbox window
- •Action
- •Result
- •2.3 Loading files
- •2.3.1 Event handlers in Visual Studio .NET
- •Action
- •Result
- •2.3.2 Exception handling
- •Action
- •Result
- •Action
- •Results and Comments
- •2.4 Resizing forms
- •2.4.1 Assign the Anchor property
- •Action
- •Result
- •2.4.2 Assign the MinimumSize property
- •Action
- •Result
- •2.5 Recap
- •Basic Windows Forms
- •Menus
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •3.3 Click events
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •3.5 Context menus
- •Action
- •Result
- •Action
- •Result
- •3.6 Recap
- •Status bars
- •4.1 The Control class
- •4.2 The StatusBar class
- •Action
- •Result
- •Action
- •Result
- •4.3.1 Adding panels to a status bar
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •4.5 Recap
- •Reusable libraries
- •5.1 C# classes and interfaces
- •5.2 Class libraries
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •5.3 Interfaces revisited
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •5.4 Robustness issues
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Common file dialogs
- •Action
- •Results
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •6.3 Paint events
- •Action
- •Result
- •Action
- •Result
- •6.4 Context menus revisited
- •Action
- •Result
- •Action
- •Result
- •6.5 Files and paths
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •6.6 Save file dialogs
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •6.7 Open file dialogs
- •Action
- •Result
- •Action
- •Result
- •6.8 Recap
- •Drawing and scrolling
- •7.1 Form class hierarchy
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •7.4 Panels
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Dialog boxes
- •8.1 Message boxes
- •Action
- •Result
- •Action
- •Result
- •8.1.4 Creating A YesNoCancel dialog
- •Action
- •Result
- •Action
- •Result
- •8.2 The Form.Close method
- •8.2.1 The relationship between Close and Dispose
- •Action
- •Result
- •8.3 Modal dialog boxes
- •Action
- •Result
- •Action
- •Result
- •8.3.2 Preserving caption values
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Basic controls
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •9.1.2 Creating a derived form
- •Action
- •Result
- •9.2 Labels and text boxes
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •9.3.6 Adding AlbumEditDlg to our main form
- •Action
- •Result
- •Action
- •Result
- •9.4 Recap
- •List controls
- •10.1 List boxes
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •10.2 Multiselection list boxes
- •10.2.1 Enabling multiple selection
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •10.3 Combo boxes
- •Action
- •Result
- •Action
- •Result
- •10.4 Combo box edits
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •10.5 Owner-drawn lists
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •More controls
- •Action
- •Result
- •Action
- •Result
- •11.2 Tab pages
- •Action
- •Result
- •Action
- •Result
- •11.3.1 Dates and times
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •11.5 Recap
- •A .NET assortment
- •12.1 Keyboard events
- •Action
- •Result
- •Action
- •Result
- •12.2 Mouse events
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •12.3 Image buttons
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •12.4 Icons
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •12.5 Recap
- •Toolbars and tips
- •13.1 Toolbars
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •13.4.2 Creating tool tips
- •Action
- •Result
- •Action
- •Result
- •Advanced Windows Forms
- •List views
- •14.2 The ListView class
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •14.2.3 Populating a ListView
- •Action
- •Result
- •Action
- •14.3 ListView columns
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •14.6 Recap
- •Tree views
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •15.3 Dynamic tree nodes
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •15.4 Node selection
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •15.5 Fun with tree views
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Multiple document interfaces
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •16.3 Merged menus
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •16.4 MDI children
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •16.5 MDI child window management
- •Action
- •Result
- •Action
- •Result
- •16.6 Recap
- •Data binding
- •17.1 Data grids
- •Action
- •Result
- •Action
- •Result
- •17.2 Data grid customization
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Odds and ends .NET
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •18.2 Timers
- •Action
- •Result
- •Action
- •Result
- •18.3 Drag and drop
- •Action
- •Result
- •Action
- •Result
- •18.4 ActiveX controls
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •Action
- •Result
- •18.5 Recap
- •C# primer
- •A.1 C# programs
- •A.1.1 Assemblies
- •A.1.2 Namespaces
- •A.2 Types
- •A.2.1 Classes
- •A.2.2 Structures
- •A.2.3 Interfaces
- •A.2.4 Enumerations
- •A.2.5 Delegates
- •A.3 Language elements
- •A.3.1 Built-in types
- •A.3.2 Operators
- •A.3.3 Keywords
- •A.4 Special features
- •A.4.1 Exceptions
- •A.4.2 Arrays
- •A.4.3 Main
- •A.4.4 Boxing
- •A.4.5 Documentation
- •.NET namespaces
- •B.1 System.Collections
- •B.2 System.ComponentModel
- •B.3 System.Data
- •B.4 System.Drawing
- •B.5 System.Globalization
- •B.6 System.IO
- •B.7 System.Net
- •B.8 System.Reflection
- •B.9 System.Resources
- •B.10 System.Security
- •B.11 System.Threading
- •B.12 System.Web
- •B.13 System.Windows.Forms
- •B.14 System.XML
- •Visual index
- •C.1 Objects
- •C.2 Marshal by reference objects
- •C.3 Components
- •C.4 Common dialogs
- •C.7 Event data
- •C.8 Enumerations
- •For more information
- •bibliography
- •Symbols
- •Index
•Automate scrolling when the image is larger than the form.
•Examine the Panel class, and draw our image in a Panel object instead of directly onto the form.
As we have done in previous chapters, we begin by looking at the class hierarchy for the Form object in more detail.
7.1FORM CLASS HIERARCHY
If you have actually read this book from the beginning, you will recall that we looked at the Menu class hierarchy in chapter 3 and the StatusBar hierarchy in chapter 4. Chapter 3 introduced some of the low-level classes used by Windows Forms, and chapter 4 extended this hierarchy to include the Control class.
The Form class is based on additional extensions to enable scrolling and containment. The complete hierarchy is shown in figure 7.1.
Figure 7.1 The Form class hierarchy extends the Control class discussed in chapter 4 with the functionality required for various kinds of application windows.
As you can see in the figure, the ScrollableControl and ContainerControl classes extend the Control class discussed in chapter 4 to support functionality required by the Form class. The ScrollableControl class adds auto-scrolling capabilities, while the ContainerControl adds focus management on the contained set of controls, even when the container itself does not have the focus.
FORM CLASS HIERARCHY |
195 |
7.1.1THE SCROLLABLECONTROL CLASS
You might think that all classes with scrolling support inherit from the ScrollableControl object. In fact, this class is only for objects which support automated scrolling for a contained set of Control objects. Controls such as ListBox, TextBox, and other controls that provide scrolling of their drawing area do so indepen-
dently of this object using the ScrollBar class, which is unrelated to the
ScrollableControl class.
The TextBox and ListBox classes are discussed in chapters 9 and 10, respectively. The point here is that the ScrollableControl class is specifically designed for the scrolling support required by container objects such as forms and panels. A summary of this class is provided in the .NET Table 7.1.
We will see the members of this class in action later in the chapter when we enable scrolling within our application.
.NET Table 7.1 ScrollableControl class
The ScrollableControl class represents a control that supports automated scrolling. This class is part of the System.Windows.Forms namespace and inherits from the System.Windows.Forms.Control class. This class is not typically used directly. Instead, derived classes such as Form and Panel are used. See .NET Table 4.1 on page 104 for a list of members inherited from the Control class.
|
AutoScroll |
Gets or sets whether the user can scroll the container |
|
|
|
to any contents placed outside of its visible boundaries. |
|
|
AutoScrollMargin |
Gets or sets the extra margin to add to the container’s |
|
|
|
contents for scrolling purposes. This ensures that the |
|
|
|
scrollable area goes slightly beyond the edge of any |
|
|
|
contained controls. |
|
Public |
AutoScrollMinSize |
Gets or sets the Size object representing the minimum |
|
Properties |
|||
|
height and width of the scrollbars in pixels. |
||
|
|
||
|
AutoScrollPosition |
Gets or sets the Point within the virtual display area to |
|
|
|
appear in the upper left corner of the visible portion of |
|
|
|
the control. |
|
|
DockPadding |
Gets or sets the extra padding for the inside border of |
|
|
|
this control when it is docked. |
|
|
|
|
|
Public |
SetAutoScrollMargin |
Sets the AutoScrollMargin property. |
|
Methods |
|
|
|
|
|
|
|
Protected |
HScroll |
Gets or sets whether the horizontal scroll bar is visible. |
|
Properties |
VScroll |
Gets or sets whether the vertical scroll bar is visible. |
|
|
|||
|
|
|
7.1.2THE FORM CLASS
We will skip over the ContainerControl class from figure 7.1 and jump straight to the Form class. The ContainerControl class simply adds functionality for focus management on the contained controls. For example, an ActiveControl property
196 |
CHAPTER 7 DRAWING AND SCROLLING |
sets or gets the Control object that has the focus; while the ActivateControl method sets the focus to a specific control. The functionality is useful, but not as glamorous as scroll bars or forms, so we skip it.
.NET Table 7.2 Form class
The Form class represents any window that can be displayed by an application, including standard windows as well as modal or modeless dialog boxes and multiple document interface (MDI) windows. This class is part of the System.Windows.Forms namespace and inherits from the ContainerControl class. The contents of a form can be drawn directly by a program, consist of a collection of controls, or some combination of the two. These contents can also be larger than the visible area, with scrolling supported by the ScrollableControl class (see .NET Table 7.1).
Public Static |
ActiveForm |
Gets the Form currently active in the application, |
Properties |
|
or null if no Form is active. |
|
|
|
|
AcceptButton |
Gets or sets the button to invoke when the Enter |
|
|
key is pressed. |
|
ControlBox |
Gets or sets whether a control box appears at |
|
|
the left of the title bar. |
|
DialogResult |
Gets or sets the dialog result to return when the |
|
|
form is a modal dialog box. |
|
Icon |
Gets or sets the icon for the form. |
|
IsMdiChild |
Gets whether this form is an MDI child form. |
|
|
MDI forms are discussed in chapter 16. |
Public Properties |
MaximizeBox |
Gets or sets whether a Maximize button appears |
|
in the title bar of the form. |
|
|
MaximumSize |
Gets or sets the maximum size for a form. |
|
Menu |
Gets or sets the MainMenu object for this form. |
|
Modal |
Gets whether this form is displayed modally. |
|
ShowInTaskBar |
Gets or sets whether the form is displayed in the |
|
|
Windows task bar. |
|
StartPosition |
Gets or sets the initial position of the form when |
|
|
it is displayed. |
|
WindowState |
Gets or sets how the form is displayed on the |
|
|
desktop (normal, maximized, or minimized). |
|
|
|
|
Activate |
Activates the form and gives it focus. |
Public Methods |
Close |
Closes the form. |
|
ShowDialog |
Displays this form as a modal dialog box. |
|
|
|
|
Closing |
Occurs when the form is about to close. |
Public Events |
Deactivate |
Occurs when the form has lost the focus. |
|
Load |
Occurs before a form is initially displayed. |
|
|
|
FORM CLASS HIERARCHY |
197 |
On to our friend the Form class. This class can be used for just about any application window, including borderless, floating, and dialog box windows. The Control class we discussed in chapter 4 provides a number of useful members for dealing with forms. For example, the Width and Height properties determine the size of the form, the DisplayRectangle property holds the drawable area of the form, and the Cursor property gets or sets the current cursor to display. A number of common events such as Click, KeyDown, MouseUp, and Paint are also inherited from this class. Scrolling, of course, is provided by the ScrollableControl class. An overview of the members specific to the Form class is shown in .NET Table 7.2.
Once you have perused the Form overview and memorized its contents, go on to the next section where we will make some practical use of some of these members.
7.2IMAGE DRAWING
Well, we are ready to utilize the Form class members, but where to begin? We will avoid dialog boxes and other new windows for the time being, and stick with the single form in our application. A good first topic we can cover here is drawing on a form.
As we have seen, drawing in .NET is performed using the System.Drawing namespace. We used classes from this namespace in chapter 4 when we created an owner-drawn status bar panel. Here, we continue to use the Graphics class for drawing, but will make use of some alternate members. Before we can do this, we need a place to draw. To generate such a place, we will remove the PictureBox object from our application.
7.2.1DELETING THE PICTUREBOX CONTROL
Beside the need for a place to draw, the PictureBox control just isn’t working out here. Like the Load button before it, it is just the wrong control for the task at hand. In chapter 2 we saw how this control stretched and distorted our image, and in chap-
ter 3 we saw that scrolling was not supported when the displayed image exceeded the size of the control. So, it was nice while it lasted, but off it goes.1
Let’s get this task out of the way so we can draw with a somewhat freer hand.
1 It is, in fact, possible to extend the PictureBox control to provide this support. You can override the Paint event to draw an image with the proper aspect ratio much like we do in this chapter, and scroll bars can be added using the ScrollBar class. We do not take this approach here since we want to discuss forms and panels, so the PictureBox control is no longer needed. Chapter 15 displays a properly proportioned image within a PictureBox control, and chapter 18 provides a short discussion on how to create a custom control incorporating this functionality.
198 |
CHAPTER 7 DRAWING AND SCROLLING |