- •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
Since it is possible to have more than one handler for an event, the += notation is used to add a new event handler without removing any existing handlers. When multiple event handlers are registered, the handlers are typically called sequentially in the same order in which they were added. The System.EventHandler is a delegate in C#, and specifies the format required to process the event. In this case, EventHandler is defined internally by the .NET Framework as
public delegate void EventHandler(object sender, EventArgs e);
A delegate is similar to a function pointer in C or C++ except that delegates are typesafe. The term type-safe means that code is specified in a well-defined manner that can be recognized by a compiler. In this case, it means that an incorrect use of a delegate is a compile-time error. This is quite different than in C++, where an incorrect use of a function pointer may not cause an error until the program is running.
By convention, and to ensure interoperability with other languages, event delegates in .NET accept an object parameter and an event data parameter. The object parameter receives the source, or sender, of the event, while the event data parameter receives any additional information for the event. Typically, the sender parameter receives the control that received the event. In our case, this is the actual Button instance. The e parameter receives an EventArgs instance, which does not by default contain any additional information.
We will discuss events and delegates in more detail later in the book, most notably in chapters 3 and 9. For now, simply recognize that OnLoadClick is an event handler that is invoked whenever the user clicks the Load button.
The next section looks at the implementation of the OnLoadClick method in more detail.
1.3.2The OpenFileDialog class
Once our OnLoadClick event handler is registered, we are ready to load a new image into the application. The signature of the OnLoadClick method must match the signature of the EventHandler delegate by being a void function that accepts an object and EventArgs parameter. Note how this is a private method so that it is not available except within the MyForm class.
private void OnLoadClick(object sender, System.EventArgs e)
{
OpenFileDialog dlg = new OpenFileDialog();
dlg.Title = "Open Photo";
dlg.Filter = "jpg files (*.jpg)|*.jpg|All files (*.*)|*.*" ;
if (dlg.ShowDialog() == DialogResult.OK)
{
pboxPhoto.Image = new Bitmap(dlg.OpenFile());
}
dlg.Dispose();
}
22 |
CHAPTER 1 GETTING STARTED WITH WINDOWS FORMS |
The System.Windows.Forms.OpenFileDialog class is used to prompt the user to select an image to display. This class inherits from the more generic FileDialog class, which provides a standard framework for reading and writing files. A summary of this class is given in .NET Table 1.2.
OpenFileDialog dlg = new OpenFileDialog();
dlg.Title = "Open Photo";
dlg.Filter = "jpg files (*.jpg)|*.jpg|All files (*.*)|*.*" ;
The Title property for this class sets the string displayed in the title bar of the dialog, while the Filter property defines the list of file types that can be seen in the dialog. The format of the Filter property matches the one used for file dialogs in previous Microsoft environments. The vertical bar character ‘|’ separates each part of the string. Each pair of values in the string represents the string to display in the dialog and the regular expression to use when displaying files, respectfully. In our example, the dialog box presents two options for the type of file to select. This first is “jpg files (*.jpg)” which will match all files of the form *.jpg; while the second is “All files (*.*)” which will match all files of the form *.*.
Once the OpenFileDialog object is created and initialized, the ShowDialog method displays the dialog and waits for the user to select a file. This method returns a member of the DialogResult enumeration, which identifies the button selected by the user.
if (dlg.ShowDialog() == DialogResult.OK)
{
pboxPhoto.Image = new Bitmap(dlg.OpenFile());
}
If the user clicks the OK button, the ShowDialog method returns the value DialogResult.OK. If the user clicks the Cancel button, the ShowDialog method returns the value DialogResult.Cancel. When the OK button has been clicked, the selected file is loaded as a Bitmap object, which is our next topic.
TRY IT! Note that no error handling is performed by our code. Try selecting a nonimage file in the dialog to see how the program crashes and burns. We will talk about handling such errors in the next chapter.
Before we move on, note the final line of our OnLoadClick handler.
dlg.Dispose();
While the garbage collector frees us from worrying about memory cleanup, nonmemory resources are still an issue. In this case, our OpenFileDialog object allocates operating system resources to display the dialog and file system resources to open the file via the OpenFile method. While the garbage collector may recover these resources eventually, such resources may be limited and should always be reclaimed manually by calling the Dispose method.
LOADING FILES |
23 |
.
.NET Table 1.2 FileDialog class
The FileDialog class is a common dialog that supports interacting with files on disk. This class is abstract, meaning you cannot create an instance of it, and serves as the base class for the OpenFileDialog and SaveFileDialog class. The FileDialog class is part of the System.Windows.Forms namespace and inherits from the CommonDialog class.
Note that a FileDialog object should call the Dispose method when finished to ensure that nonmemory resources such as file and window handles are cleaned up properly.
|
AddExtension |
Gets or sets whether the dialog box automatically |
|
|
adds the file extension if omitted by the user. |
|
CheckFileExists |
Gets or sets whether the dialog box displays a |
|
|
warning if the specified file does not exist. |
|
FileName |
Gets or sets the string containing the selected file |
|
|
name. |
|
FileNames |
Gets the array of strings containing the set of files |
|
|
selected (used when the |
|
|
OpenFileDialog.Multiselect property is true). |
Public Properties |
Filter |
Gets or sets the file name filter string, which |
|
||
|
|
determines the file type choices for a file dialog box. |
|
InitialDirectory |
Gets or sets the initial directory displayed by the file |
|
|
dialog box. |
|
RestoreDirectory |
Gets or sets whether the dialog box restores the |
|
|
current directory to its original value before closing. |
|
ShowHelp |
Gets or sets whether the Help button appears on the |
|
|
dialog. |
|
Title |
Gets or sets the title bar string for the dialog box. |
|
|
|
|
Reset |
Resets all properties for the dialog box to their |
|
|
default values. |
Public Methods |
ShowDialog |
Displays a common dialog box and returns the |
|
||
|
(inherited from |
DialogResult enumeration value of the button |
|
CommonDialog) |
selected by the user. |
|
|
|
|
FileOk |
Occurs when the Open or Save button is clicked on a |
|
|
file dialog box. |
Public Events |
HelpRequested |
Occurs when the Help button is clicked on a |
|
||
|
(inherited from |
common dialog box. |
|
CommonDialog) |
|
|
|
|
The Dispose method is the standard mechanism for cleaning up such resources. We will discuss this method in more detail in chapter 6.
1.3.3Bitmap images
So far we have discussed how our application responds to a click of the Load button and enables the user to select an image file. When the user clicks the OK button in the open file dialog box, the OnLoadClick method loads an image into the
24 |
CHAPTER 1 GETTING STARTED WITH WINDOWS FORMS |
PictureBox control. It does this by creating a new Bitmap object for the selected file and assigning it to the Image property of the PictureBox control.
if (dlg.ShowDialog() == DialogResult.OK)
{
pboxPhoto.Image = new Bitmap(dlg.OpenFile());
}
The support for image files has been steadily improving with each new development environment from Microsoft, and the .NET Framework is no exception. While the
.NET classes do not provide all the functionality you might like (as we shall see), it does provide a number of improvements over the previous support provided by the MFC (Microsoft Foundation Class) library. One of them is the PictureBox control to make image display a little easier. All we have to do is set the Image property to a bitmap image and the framework takes care of the rest.
Our friend, the new keyword, creates the Bitmap. Once again, we see how garbage collection makes our life easier. In C++, the memory allocated for this Bitmap would need to be tracked and eventually freed with a call to delete. In C#, we create the object and forget about it, relying on the garbage collector to clean it up when a new image is loaded by the OnLoadClicked method and the existing Bitmap replaced.
The OpenFileDialog class provides a couple of ways to access the selected file. The FileName property retrieves the path to the selected file. In our code, we opt for the OpenFile method to open this file with read-only permission. The open file is passed to the Bitmap constructor to load the image.
The constructed bitmap is assigned to the Image property of our pboxPhoto variable. This property can hold any object which is based on the Image class, including bitmaps, icons, and cursors.
How this image appears within the picture box control depends on the tureBox.SizeMode property. In our case, we set this property so that the image is shrunk and/or expanded to fit the boundaries of the PictureBox control.
pboxPhoto.SizeMode = PictureBoxSizeMode.StretchImage;
TRY IT! If you’re feeling slightly adventurous, you should now be able to add a second Button and second PictureBox to the form. Label the second button “Load2” and implement an OnLoad2Click event handler that loads a second image into the second PictureBox control.
As an alternate modification, change the Main method to receive the array of command-line arguments passed to the program in an args variable. Load the first parameter in args[0] as a Bitmap object and assign it to the PictureBox control for the MyForm class. To do this, you will need to add a new constructor to the MyForm class that receives the name of an image file.
LOADING FILES |
25 |